📦

MySQLのインストール後の初期設定方法

2024/12/04に公開

ログファイルからrootの初期パスワードを確認する。

[root@mizuga-education-web01 ~]# ll /var/log/mysqld.log
-rw-r----- 1 mysql mysql 2603 Dec  4 06:04 /var/log/mysqld.log

ログから初期パスワードの確認 「_Tdbkf-sd5ar」

[root@mizuga-education-web01 ~]# grep password /var/log/mysqld.log
2024-12-03T02:05:47.047783Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: _Tdbkf-sd5ar

mysqlを対話モードで設定していく。

[root@mizuga-education-web01 ~]# mysql_secure_installation

mysql_secure_installation コマンドのメモ

[root@mizuga-education-web01 ~]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

The existing password for the user account root has expired. Please set a new password.


New password:

Re-enter new password:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100      ←パスワード強度の設定 Nでよかったが下記ではy押したので再度パスワード入力の画面になった。
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y


New password:

Re-enter new password:
Sorry, passwords do not match.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : No   入力したパスワードそのまま使いますか?の意味 ここで間違えてNoとしてる。

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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) : 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : Y   リロードするか確認
Success.

All done!

クエリログを出力するようにする。

クエリログを出力する設定は、MySQLサーバーが実行するすべてのSQLクエリ(SELECT、INSERT、UPDATE、DELETEなど)を記録することを意味します。この設定を行うことで、以下の目的を達成できます:
クエリログの用途
デバッグ
実行されるクエリを確認することで、アプリケーションやスクリプトの問題を特定する。

パフォーマンスチューニング
頻繁に実行されるクエリを特定し、インデックスの追加やクエリの最適化を検討する。

セキュリティ監査
意図しないまたは不正なクエリを検出するためのログとして活用する。

クエリログの出力先の作成

[root@mizuga-education-web01 ~]# vi /etc/my.cnf
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]# cp -ip /etc/my.cnf /etc/my.cnf.20241204
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]# vi /etc/my.cnf
[root@mizuga-education-web01 ~]#

下記を追記する。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
general_log=1
general_log_file=/var/log/mysql/query.log
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー


[root@mizuga-education-web01 ~]# diff /etc/my.cnf /etc/my.cnf.20241204
32,33d31
< general_log=1
< general_log_file=/var/log/mysql/query.log
[root@mizuga-education-web01 ~]#

これで/var/log/mysql/query.logでクエリが出力されるようになった。
ただ出力先のディレクトリ、権限の設定も必要

#これはミス、作成するのはあくまでログを入れるディレクトリだけでいい。
[root@mizuga-education-web01 ~]# mkdir -p /var/log/mysql/query.log


権限変更
[root@mizuga-education-web01 ~]# chown -R mysql:mysql /var/log/mysql
[root@mizuga-education-web01 ~]#


修正、目的のディレクトリできた。
[root@mizuga-education-web01 ~]# rmdir /var/log/mysql/query.log
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]# ll -d /var/log/mysql
drwxr-xr-x 2 mysql mysql 6 Dec  4 06:40 /var/log/mysql
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]#

MySQLへのログイン

ログインコマンド後に設定したパスワードを入れる。→ログイン成功

[root@mizuga-education-web01 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.40 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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へのログインはできるため、あくまでconfの設定値の反映(たぶん)

[root@mizuga-education-web01 ~]# systemctl restart mysqld
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]#
[root@mizuga-education-web01 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
     Active: active (running) since Wed 2024-12-04 06:47:10 UTC; 2s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 1893 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   Main PID: 1917 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 10522)
     Memory: 365.8M
        CPU: 700ms
     CGroup: /system.slice/mysqld.service
             mq1917 /usr/sbin/mysqld

Dec 04 06:47:09 mizuga-education-web01 systemd[1]: Starting MySQL Server...
Dec 04 06:47:10 mizuga-education-web01 systemd[1]: Started MySQL Server.
[root@mizuga-education-web01 ~]#

Discussion