LoginSignup
22

More than 5 years have passed since last update.

ラズベリーパイにMariadbをインストールした時のメモ

Posted at

作業内容のメモ

動作環境

raspberrypi 3 model B
raspbian 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)ではmysqlではなくmariadbが推奨されているとのこと。
先のmysqlをインストールした際の出力結果を確認してみると確かにmariadbをインストールした結果が残っていた。
そこでmysqlではなくmariadbとコマンドを変更してログインしようとすると

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:~ $ 

rootに対してパスワードの設定ができたので再度ログインしてみる。

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"でログインする方法であり、先のプラグインを無効化する方法を紹介されていた記事のように別途ユーザを作成し、作成したユーザごとに権限を与えて運用する方法がよさそう。

参考サイト(引用元)

Memo: MariaDBのインストール/初期設定
Ubunt 16.04でMariaDBをインストールするとパスワードが変 | 純規の暇人趣味ブログ

間違いご指摘等がありましたらお手数ですがお知らせいただくと大変勉強になります。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22