LoginSignup
17
20

More than 5 years have passed since last update.

Ubuntu 16.04におけるmysqlのインストールと設定、エラー対応のまとめ

Last updated at Posted at 2017-07-05

背景

Ubuntu 16.04のサーバーにMySQLをインストールする時に、何回も同じ問題が出ていたので、Ubuntu 16.04におけるmysqlのインストールと設定、エラー対応などをまとめておく。

インストール

sudo apt-get install mysql-server

通常、このコマンドを実行すると、MySQLの初期設定画面が出て、初期パスワードなども設定できるはずだが、出ない時もあった。というか、私の場合、ほどんど出なかった。

エラー 1 : 初期設定のパスワードがわからない

初期設定画面が出なかった場合、初期設定のパスワードがわからないので、MySQLをインストールしたものの、使えない。

この場合、mysqlのsafeモードの下で、MySQLの権限システムを使用しないで起動することが可能。従って、safeモードで起動して、rootのpasswordを変えることが考えられる。

その手順は、以下のようになる。

a. mysqlを停止

sudo /etc/init.d/mysql stop

b. safeモードで起動

mysqld_safe --skip-grant-tables &

c. 他のターミナルを開き、MySQLの権限システムを使用しないで起動する

mysql -u root

d. user tableのデータを削除し、rootユーザを作成

*このステップで、単なるrootユーザのpasswordを変えることも考えられる。

mysql> truncate table user;
mysql> flush privileges;
mysql> grant all privileges on *.* to root@localhost identified by 'root' with grant option;
mysql> flush privileges;

e. safeモードからexitし、MySQLを再起動

sudo /etc/init.d/mysql restart

エラー 2 : MySQLのsocketファイルが見つかれない

エラー 1の対処で、 以下のようなエラーが出る場合がある。

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

この場合は、見つかれないパスの下にファイルを作っておけば解決できる。

対処コマンド:

sudo touch /var/run/mysqld/mysqld.sock
sudo chown mysql:mysql /var/run/mysqld/
sudo /etc/init.d/mysql restart

備考

MySQLが正常にインストールされ、 パスワード再設定を含め、 初期設定を行いたい場合、 以下のコマンドで設定できる。

mysql_secure_installation

参照文献

  1. MySQLでの「Access denied for user ‘root’@’localhost’ (using password: NO) 」への対処
  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  3. mysqlソケットエラー解決法
17
20
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
17
20