LoginSignup
4
3

More than 3 years have passed since last update.

[ubuntu] mysql5.7インストールして使おうとしたら、mysqld_safeすら失敗するとき

Last updated at Posted at 2019-03-03

注意事項

今回の記事は再現性のチェックを行っていないため、あまり当てにしないでください。
使い慣れないubuntuで困ったとき用の自分のためのメモです。
同じ現象に出くわした人には多少参考になるかもしれないので、公開してみました。

ubuntuのバージョン

bash
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
# 以下略

「mysqld_safeすら失敗する」とは

以下のような状態です。

ubuntuでmysql5.7をインストールしたところ、rootの初期パスワードがわからず、何もできませんでした。

まず初めに対策として、CentOSでお馴染みの

bash
# 初期パスワードが書いているはずのファイル
/var/log/mysqld.log

を探したのですが、ファイルが存在しません。
仕方なく、ネットの海を彷徨うと、

bash
# mysqld_safe  --skip-grant-tables &

このコマンドを打つといいよと書いている記事にたどりつきました。
https://www.aiship.jp/knowhow/archives/28257

しかし、、、

bash
$ sudo mysqld_safe --skip-grant-tables &
2019-03-03T10:25:04.052990Z mysqld_safe Logging to syslog.
2019-03-03T10:25:04.060534Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2019-03-03T10:25:04.070437Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

それすらもエラーに阻まれる...(´д`)
socketファイルがない・・・?なんですかそれは?

こんな状態のことです。

対策

困った時の頼みの綱、stackoverflowに対策が書かれていました
https://stackoverflow.com/questions/42153059/mysqld-safe-directory-var-run-mysqld-for-unix-socket-file-dont-exists

bash
$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld

「ディレクトリがないんだったら、作れば?」
とMatt ClarkさんやPartha Senさんがおっしゃっています。

そのとおりに上記2つのコマンドを実行したら、mysqld-safe --skip-grant-tablesコマンドが通るようになりました。

bash
# mysqlをセーフモードで起動!
$ mysqld-safe --skip-grant-tables &

その後は

あとは、最初に紹介したサイトの通りに操作すればrootパスワード変更ができ、mysqlをrootユーザーで利用できるようになります。

bash
# 1 safeモードで起動中のmysqlにrootユーザーでログイン
$ sudo mysql -u root
mysql>

# 2 mysqlというDBを使用することを選択
mysql> 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

# 3 user情報を全削除(?)
mysql> truncate table user;
Query OK, 0 rows affected (0.08 sec)

# 4 userの削除結果をmysqlに反映させる
mysql> flush privileges;
Query OK, 0 rows affected (0.07 sec)

# 5 rootユーザーに全権限付与し、新しいパスワードを設定する
mysql> grant all privileges on *.* to root@localhost identified by 'rootの新たなパスワード' with grant option;
Query OK, 0 rows affected, 1 warning (0.05 sec)

# 6 rootユーザーへの権限付与をmysqlに反映させる
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

# 7 safeモードのmysqlから離脱
mysql> \quit
Bye

# 8 セーフモード中のmysqlをkillするためにプロセス番号を確認
$ ps aux | grep mysql

# 9 mysqlプロセスを停止させる
$ sudo kill mysqlのプロセス番号

# 10 通常モードでmysql起動
$ sudo service mysql start
$ mysql -u root -p
# '5'で設定したrootの新たなパスワードを入力
mysql> 

# これでmysqlのrootユーザーが使用できるようになったことがわかる

このままだと、パスワードなしでmysqldにログインできてしまうので、設定を元に戻しておく

$ sudo systemctl set-environment MYSQLD_OPTS=""

mysqlのインストール状態・バージョンについて補足

mysqlインストール直後は、この状態でした。

$ sudo dpkg -l | grep mysql
rc  mysql-apt-config                      0.8.10-1                                   all          Auto configuration for MySQL APT Repo.
ii  mysql-client-5.7                      5.7.25-0ubuntu0.16.04.2                    amd64        MySQL database client binaries
ii  mysql-client-core-5.7                 5.7.25-0ubuntu0.16.04.2                    amd64        MySQL database core client binaries
ii  mysql-common                          5.7.25-0ubuntu0.16.04.2                    all          MySQL database common files, e.g. /etc/mysql/my.cnf
rc  mysql-community-client                5.7.22-1ubuntu16.04                        amd64        MySQL Client
rc  mysql-community-server                5.7.22-1ubuntu16.04                        amd64        MySQL Server
iF  mysql-server-5.7                      5.7.25-0ubuntu0.16.04.2                    amd64        MySQL database server binaries and system database setup
ii  mysql-server-core-5.7                 5.7.25-0ubuntu0.16.04.2                    amd64        MySQL database server binaries

この状態になっていれば、対策やその後1〜10の操作結果が私と同じになる可能性が高いと思われます。

bash
$ sudo mysql_secure_installation

という有名なコマンドがあるのですが、こいつを使わなくても動いてしまう。

気味が悪い・・・・。
なんでcentosとubuntuでインストール方法が違うんだ…。
ubuntu上のmysqlログファイルは一体どこに消えてしまったのだろう(´・д・`)

続報・謎1つ解決

こんなところにmysqlログを発見しました

bash
$ cat /var/log/mysql/error.log | grep password
2018-03-20T23:59:40.930989Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
...以下略

ログによると、rootユーザーは初期パスワード無しで作成されていたらしい。

関連情報?

mysqlインストール時にこのエラーが出ていたので、

Access denied for user 'debian-sys-maint'@'localhost' (using password: YES)

対策を確認したところ、次のリンク先の通りにすることで解決できました。
https://stackoverflow.com/questions/11644300/access-denied-for-user-debian-sys-maint

あくまでも参考程度に

4
3
0

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
4
3