LoginSignup
5
4

More than 5 years have passed since last update.

CentOSにnginx, php-fpm, mysqlの環境を構築する3

Posted at

CentOSにnginx, php-fpm, mysqlの環境を構築する3

CentOSにnginx, php-fpm, mysqlの環境を構築する2の続き

すんなりいくかなーとおもいきや、おもいっきりハマりました。
予定では今回でPHPとの接続をするつもりだったんですが、、、、。
rootユーザのログイン確認で終わってしまいました。。。

mysqlの設定

まずは、初期のデータベースの確認。

[root@~] # mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.37 MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2014, 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              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql>

続いて初期設定コマンドを実行。

$ mysql_secure_installation
[root@~] # mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

現在のrootパスワードを聞かれるので、初期値はカラなのでそのままエンター

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

続いてrootのパスワードを設定するかどうかなので、もちろんYESでエンターで、
新しいワスワードを入力。

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

ここからは上から順に

  • anonymous usersの削除→YES
  • リモートでのrootログイン禁止→YES
  • テスト用データベースの削除→YES
  • 設定を読み込むために再読み込みを行うかどうか→YES
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? [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, 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? [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 MySQL
installation should now be secure.

Thanks for using MySQL!

初期設定が完了したので、この状態のデータベースと、ユーザを確認する。

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

mysql>
mysql> SELECT host,user FROM mysql.user;
+-----------+------+
| host      | user |
+-----------+------+
| 127.0.0.1 | root |
| localhost | root |
+-----------+------+
2 rows in set (0.00 sec)

mysql>

php用に新規ユーザを作る

新規ユーザを作成するコマンドを以下の通りです。

GRANT [権限] ON *.* TO [ユーザ名] IDENTIFIED BY ['パスワード'] WITH GRANT OPTION;

今回はすべての権限を持っているユーザを作って見ます。

mysql> GRANT ALL PRIVILEGES ON *.* TO puttyo@localhost IDENTIFIED BY 'XXXXXXX' WITH GRANT OPTION;
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

はいエラー。イミフ。
ぐぐる。参考URL:http://makisuke.seesaa.net/article/265392121.html

MYSQLの停止

[root@~] # service mysqld stop
Stopping mysqld: [ OK ]
[root@~] #

オプション付きで起動
MySQLを skip-grant-tablesオプション付きで起動
このオプションはMySQLの権限システムを使用しないで起動するためのもの

[root@~] # mysqld_safe --skip-grant-tables &
[1] 15276
[root@~] # 140520 10:15:18 mysqld_safe Logging to '/var/log/mysqld.log'.
140520 10:15:18 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

ログイン

mysql -u root
mysql>

現状のユーザの確認。

mysql> select User,Password,Host from mysql.user;
+------+-------------------------------------------+-----------+
| User | Password                                  | Host      |
+------+-------------------------------------------+-----------+
| root | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | localhost |
| root | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | 127.0.0.1 |
+------+-------------------------------------------+-----------+
2 rows in set (0.00 sec)

全ユーザの削除してから新規のrootユーザの作り直し。

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
mysql>
mysql>
mysql> truncate table user;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to root@localhost identified by 'XXXXXXXXX!' with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> select User,Password,Host from user;
+------+-------------------------------------------+-----------+
| User | Password                                  | Host      |
+------+-------------------------------------------+-----------+
| root | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | localhost |
+------+-------------------------------------------+-----------+
1 row in set (0.00 sec)

mysql>

mysqlの再起動して、ログインしてみる。
ぐぐぐ。いけない。

[root@~] # mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

ググりまくったけど、解決策は見つからず、
偶然ログインできました。とほほ。MYSQLのバージョンのせいでしょうか?
それともなにか設定が必要なんでしょうか?誰か教えてください。

結局ユーザを指定しない方法でのログインは成功することが判明。

[root@~] # mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.37 MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2014, 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>
mysql>
mysql>
mysql> select user, host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.37    |
+-----------+
1 row in set (0.00 sec)

mysql>

おまけ。データベースを指定してログインするには以下のようにする

mysql -u root -D test -p

次回はPHPとの接続をやります!!!

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