MacOS にMySQL データベースを導入する

ちょっと躓いたのでメモしておきます。

1. MariaDB の導入に失敗した話

文献管理システムを作るのに必要なデータベースを Mac でも導入したいと考え、MariaDB を入れようとしました。

brew install mariadb

初期設定をしようとしましたが...

takunorojiinoMac:~ takunology$ mysql_secure_installation
/usr/local/Cellar/mariadb/10.4.6/bin/my_print_defaults: Can't read dir of '/usr/local/etc/my.cnf.d' (Errcode: 2 "No such file or directory")
Fatal error in defaults handling. Program aborted

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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Enter current password for root (enter for none): 

こんなエラーが続くばかりです。どうやら mysql.sock が接続できないようなので導入してみました。なんか権限付けるといいらしいです...。

takunorojiinoMac:~ takunology$ sudo touch /tmp/mysql.sock
takunorojiinoMac:~ takunology$ sudo chown takunology /tmp/mysql.sock

再び初期設定コマンドを打ち込むと

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)
Enter current password for root (enter for none): 

数字がちょっと変わったくらいで変化なし。もしかして「サービスを起動していないから?」と、思ったので起動してみます。

takunorojiinoMac:~ takunology$ sudo mysql.server start
/usr/local/Cellar/mariadb/10.4.6/bin/my_print_defaults: Can't read dir of '/usr/local/etc/my.cnf.d' (Errcode: 2 "No such file or directory")
Fatal error in defaults handling. Program aborted
Starting MariaDB
./usr/local/Cellar/mariadb/10.4.6/bin/my_print_defaults: Can't read dir of '/usr/local/etc/my.cnf.d' (Errcode: 2 "No such file or directory")
Fatal error in defaults handling. Program aborted
/usr/local/Cellar/mariadb/10.4.6/bin/my_print_defaults: Can't read dir of '/usr/local/etc/my.cnf.d' (Errcode: 2 "No such file or directory")
Fatal error in defaults handling. Program aborted
190625 00:51:22 mysqld_safe Logging to '/usr/local/var/mysql/takunorojiinoMac.local.err'.
190625 00:51:22 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
/usr/local/bin/mysql.server: line 264: kill: (3013) - No such process
 ERROR! 

エラーなんですよねw もうヤダw
そういえば初期設定の際に、/usr/local/etc/my.cnf.d がないと書いてあったので、もしかしてと思って追加してみます。

mkdir /usr/local/etc/my.cnf.d

そして再起動

takunorojiinoMac:~ takunology$ sudo mysql.server start
Starting MariaDB
.190625 00:52:26 mysqld_safe Logging to '/usr/local/var/mysql/takunorojiinoMac.local.err'.
190625 00:52:26 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
/usr/local/bin/mysql.server: line 264: kill: (3093) - No such process
 ERROR! 

(゚∀゚)アヒャ (゚∀゚)アヒャ
何かもうできる気がしないので、MariaDBをあきらめてアンインストールしました。その際に作成された mysql ディレクトリも消します。

takunorojiinoMac:~ takunology$ sudo rm -rf /usr/local/var/mysql
takunorojiinoMac:~ takunology$ brew uninstall mariadb
Uninstalling /usr/local/Cellar/mariadb/10.4.6... (745 files, 173.4MB)

はい、白紙に戻りました。

2. MySQL を導入した話

このままあきらめると文献管理システムを作れないので、MariaDB ではなくて MySQL を入れることにしました。

takunorojiinoMac:~ takunology$ brew install mysql

さて、初期設定をしてみます。

takunorojiinoMac:~ takunology$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)

これはひどいですねw 見たことあるエラーです。また mysql.sock に接続できないらしいので、再び用意します。

takunorojiinoMac:~ takunology$ sudo touch /tmp/mysql.sock

そして何度目かの初期設定

takunorojiinoMac:~ takunology$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)

やっぱり怒られますね。てかエラーが同じですしお寿司。
色々調べていたら参考になるサイトを見つけました。

qiita.com

どうやらこのコマンドをいれて mysql.sock に権限を与えないといけないとのこと。

takunorojiinoMac:~ takunology$ sudo chown -R _mysql:wheel /tmp/mysql.sock

じつはこれでもエラーだったのでこのサイトも参考にしました。

qiita.com

mysql ディレクトリ自体にも権限を与えて、

takunorojiinoMac:~ takunology$ sudo chown -R _mysql:_mysql /usr/local/var/mysql

再起動してみます。

takunorojiinoMac:~ takunology$ sudo mysql.server restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL
.Logging to '/usr/local/var/mysql/takunorojiinoMac.local.err'.
. SUCCESS! 

やっったぁ!
この勢いで初期設定します。

akunorojiinoMac:~ takunology$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: 
Please set the password for root here.

New password: 

Re-enter new password: 
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 

 ... skipping.


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? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
All done! 

よっしゃ!設定出来た!(ディスプレイの前でガッツポーズしました)
mysql にログインしてみます。

takunorojiinoMac:~ takunology$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.16 Homebrew

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

はい、無事に導入できました。あとはこれを文献管理システムから接続するだけですね。ここまで1時間くらいかかりました。