在安装Mariadb到树莓派上时的备忘录
作業内容的笔记
行动环境
树莓派3型B
树莓派操作系统Stretch
pi@raspberrypi:~ $ cat /etc/debian_version
9.1
pi@raspberrypi:~ $ cat /etc/issue
Raspbian GNU/Linux 9 \n \l
pi@raspberrypi:~ $
概括
用树莓派登录MariaDB的root用户的记事本备忘录。
任务描述
最初打算在树莓派上安装MySQL,但和往常一样
pi@raspberrypi:~ $ sudo apt-get install mysql-server
而我执行了指令,但密码设置界面没有显示出来,导致安装无法完成。
为了确保,我执行了mysql指令。
pi@raspberrypi:~ $ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
因为要求输入了一个我没有设置的密码,所以我放弃了。
查了一下,最新的Raspbian(Stretch)推荐使用的是Mariadb而不是Mysql。
我检查了之前安装Mysql时的输出结果,发现确实留下了安装Mariadb的结果。
因此,我尝试使用Mariadb而不是Mysql作为命令来登录。
pi@raspberrypi:~ $ mariadb -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
同样地,我收到了一个密码要求。
由于我感到这可能是权限的问题,所以我执行了以下操作。
pi@raspberrypi:~ $ sudo mariadb
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
我成功地登录了。
由于没有密码设置画面,我无法登录,于是参考了这篇文章来设置密码。
pi@raspberrypi:~ $ sudo systemctl start mariadb.service
pi@raspberrypi:~ $ sudo /usr/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
# rootに対してパスワードを設定していない場合はEnterで進む
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
# rootに対して新しいパスワードを設定する
Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
pi@raspberrypi:~ $
我设定了根目录的密码,所以尝试重新登录。
pi@raspberrypi:~ $ mariadb -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
由于登录失败,请确认是否已设置密码。
pi@raspberrypi:~ $ sudo mariadb
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> select user,password,plugin from user;
+------+-------------------------------------------+-------------+
| user | password | plugin |
+------+-------------------------------------------+-------------+
| root | *7F0E37FBE843C587C23B67BD18E48B163E32F245 | unix_socket |
+------+-------------------------------------------+-------------+
1 row in set (0.01 sec)
根据密码的正确设置,我进行了调查,并在这篇文章中找到了介绍。据说,通过禁用unix_socket插件可以登录,所以我尝试了以下操作。
pi@raspberrypi:~ $ sudo mariadb
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 37
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#データベースを変更
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> select user,password,plugin from user;
+------+-------------------------------------------+-------------+
| user | password | plugin |
+------+-------------------------------------------+-------------+
| root | *7F0E37FBE843C587C23B67BD18E48B163E32F245 | unix_socket |
+------+-------------------------------------------+-------------+
1 row in set (0.00 sec)
# rootユーザに対するunix_socketプラグインを無効化する
MariaDB [mysql]> update user set plugin='' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> select user,password,plugin from user;
+------+-------------------------------------------+--------+
| user | password | plugin |
+------+-------------------------------------------+--------+
| root | *7F0E37FBE843C587C23B67BD18E48B163E32F245 | |
+------+-------------------------------------------+--------+
1 row in set (0.00 sec)
# 変更した更新内容を反映
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> exit;
Bye
# 設定したパスワードを入力するとログインできた
pi@raspberrypi:~ $ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 43
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
然而,这个方法仅仅只是通过”$ mysql -u root -p”来登录的方式,与之前介绍禁用插件的方法不同,应该额外创建用户并为每个创建的用户分配权限来运行。
参考网站 (引用来源)
备忘录:安装/初始化MariaDB
在Ubuntu 16.04上安装MariaDB会更改密码 | 純規的闲人爱好博客
如果有错误或者需要指正的地方,请麻烦您告知我,非常感谢,这对我来说会很有学习价值。