Ubuntu 18 で MySQL 5.7 インストール
WSL 2 の Ubuntu 18.04.4 LTS に MySQL 5.7 をセットアップした。
パッケージをインストール
aptで入れます。Ubuntuは新しいのが揃ってるのがいいですね。
$ sudo apt update
$ sudo apt install mysql-server mysql-client libmysqlclient-dev
すんなり入りました。
$ mysql --version
mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper
MySQL 起動
mysqlの起動を確認してみましょう。
$ sudo service mysql status
* MySQL is stopped.
お、起動していません。特に問題ありません。起動すればいいだけです。
$ sudo service mysql start
* Starting MySQL database server mysqld
No directory, logging in with HOME=/
mkdir: cannot create directory ?//.cache?: Permission denied
-su: 19: /etc/profile.d/wsl-integration.sh: cannot create //.cache/wslu/integration: Directory nonexistent
[ OK ]
なにやらエラーが出ていますが、起動はしたようです。
$ sudo service mysql status
* /usr/bin/mysqladmin Ver 8.42 Distrib 5.7.30, for Linux on x86_64
Copyright (c) 2000, 2020, 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.
Server version 5.7.30-0ubuntu0.18.04.1
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 24 sec
Threads: 1 Questions: 8 Slow queries: 0 Opens: 107 Flush tables: 1 Open tables: 26 Queries per second avg: 0.333
起動はしてる。
しかし、エラーが気になります。
No directory, logging in with HOME=/
mkdir: cannot create directory ?//.cache?: Permission denied
-su: 19: /etc/profile.d/wsl-integration.sh: cannot create //.cache/wslu/integration: Directory nonexistent
HOME=/
がないと言われています。権限問題でディレクトリが作れないと言われていて、 Directory nonexistent
だと。
mysqlユーザ
mysqlユーザの権限を確認します。
$ cat /etc/passwd | grep mysql
mysql:x:111:115:MySQL Server,,,:/nonexistent:/bin/false
エラーにあった通り、 :/nonexistent
ですね。
mysqlユーザにホームディレクトリ(/var/lib/mysql)を付与します。
$ sudo usermod -d /var/lib/mysql mysql
再度、起動します。
$ sudo service mysql start
* Starting MySQL database server mysqld [ OK ]
無事起動しました。
MySQLセットアップ
rootで入ってみます。
$ mysql -uroot
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
初期化する必要があるようです。
$ sudo mysqld --initialize
2020-07-25T17:22:33.552095Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-25T17:22:33.552931Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2020-07-25T17:22:33.552967Z 0 [ERROR] Aborting
the data directory has files in it
と言われているので、最初に起動したときに作られた /var/lib/mysql
がダメっこのようです。
$ sudo rm -rf /var/lib/mysql
ふたたび初期化。
$ sudo mysqld --initialize
設定されたパスワードはログに吐かれていいるらしいので grep して見つけます。
$ grep 'temporary password' /var/log/mysql/error.log
2020-07-25T17:26:48.535178Z 1 [Note] A temporary password is generated for root@localhost: XxXx_xxxxX2_
サービスを再び起動。
$ sudo service mysql start
* Starting MySQL database server mysqld
grepして見つけたパスワードでログインします。
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.30-0ubuntu0.18.04.1
Copyright (c) 2000, 2020, 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.
root@localhost:(none)>
パスワードを変更します。
set password for root@localhost=password('foobarbaz');
Access denied for user 'debian-sys-maint'@'localhost'
service mysql restart するとなにやら fail してしまいます。
Access denied for user 'debian-sys-maint'@'localhost'
Debian系のディストリビューションだと、debian-sys-maint というユーザがserviceコマンド経由の操作に使われる(普通rootで行うような操作はほぼ全て)。
以下のように権限とパスワードを設定する。
GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY 'SOME_PASSWORD';
mysqlのcnfファイルに、/etc/mysql/debian.cnf
があるのでここに debian-sys-maint
ユーザのパスワードを書く。(パスワードを生で書くの抵抗あるけどなんかそういうものらしい。ハッシュ化する方法がないこともないらしいけど。)
これで無事起動する。
$ sudo service mysql start
* Starting MySQL database server mysqld [ OK ]
無事起動したときのログ
$ sudo tail /var/log/mysql/*
2020-07-25T21:36:31.380382Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2020-07-25T21:36:31.380404Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2020-07-25T21:36:31.380778Z 0 [Warning] CA certificate ca.pem is self signed.
2020-07-25T21:36:31.380804Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2020-07-25T21:36:31.380842Z 0 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
2020-07-25T21:36:31.380856Z 0 [Note] - '127.0.0.1' resolves to '127.0.0.1';
2020-07-25T21:36:31.380867Z 0 [Note] Server socket created on IP: '127.0.0.1'.
2020-07-25T21:36:31.384459Z 0 [Note] Event Scheduler: Loaded 0 events
2020-07-25T21:36:31.384831Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.30-0ubuntu0.18.04.1' socket: '/var/run/mysqld/mysqld.sock' port: 3306 (Ubuntu)
文字コード設定
ついでに文字コードの設定変更
root@localhost:(none)> show variables like "chara%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
utf8mb4
に設定。
[mysqld]
character-set-server = utf8mb4
ファイル構成
/etc/mysql
$ sudo tree /etc/mysql/
/etc/mysql/
├── conf.d
│ ├── mysql.cnf
│ └── mysqldump.cnf
├── debian-start
├── debian.cnf
├── my.cnf -> /etc/alternatives/my.cnf
├── my.cnf.fallback
├── mysql.cnf
└── mysql.conf.d
├── mysqld.cnf
└── mysqld_safe_syslog.cnf