CentOS7.5にyumを利用してMySQL5.7をインストール、初期設定するまでを紹介します。
MariaDBを削除
CentOS7にはデフォルトでMariaDBというMySQLの派生版がインストールされている場合があるので、競合しないよう削除しておきます。
$ yum remove mariadb-libs
$ rm -rf /var/lib/mysql/
MySQL5.7をインストール
# RPMを取得
$ sudo rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
# 確認
$ sudo yum info mysql-community-server
利用可能なパッケージ
名前 : mysql-community-server
アーキテクチャ : x86_64
バージョン : 5.7.22
リリース : 1.el7
容量 : 165 M
リポジトリー : mysql57-community
要約 : A very fast and reliable SQL database server
URL : http://www.mysql.com/
ライセンス : Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Under GPLv2 license as shown in the Description field.
説明 : The MySQL(TM) software delivers a very fast, multi-threaded, multi-user,
: and robust SQL (Structured Query Language) database server. MySQL Server
: is intended for mission-critical, heavy-load production systems as well
: as for embedding into mass-deployed software. MySQL is a trademark of
: Oracle and/or its affiliates
:
: The MySQL software has Dual Licensing, which means you can use the MySQL
: software free of charge under the GNU General Public License
: (http://www.gnu.org/licenses/). You can also purchase commercial MySQL
: licenses from Oracle and/or its affiliates if you do not wish to be bound by the terms of
: the GPL. See the chapter "Licensing and Support" in the manual for
: further info.
:
: The MySQL web site (http://www.mysql.com/) provides the latest news and
: information about the MySQL software. Also please see the documentation
: and the manual for more information.
:
: This package includes the MySQL server binary as well as related utilities
: to run and administer a MySQL server.
# インストール
$ sudo yum -y install mysql-community-server
# 確認
$ mysql --version
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
# 起動
$ sudo systemctl start mysqld
初期設定
初期パスワードを確認
MySQL5.7ではインストール時に初期パスワードが設定されますのでログから確認します。ログファイルは/var/log/mysqld.logになります。
$ cat /var/log/mysqld.log | grep password
[Note] A temporary password is generated for root@localhost: ovBjPj5DBc%v # 初期パスワード
mysql_secure_installation
mysql_secure_installation
コマンドで初期設定を行います。
$ 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.
# rootのパスワード変更
New password: # 新しいパスワード
Re-enter new password: # もう一度新しいパスワード
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : # y
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
# anonymous削除
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.
# リモートホストからのrootユーザでのログインを禁止
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.
# testデータベース削除
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 -u root -p
Enter password: # 新しいパスワードを入力
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 197
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
問題なさそうですね。
設定ファイル(my.cnf)
設定ファイルで文字コードやパスワードの有効期限を設定します。
有効期限はデフォルトで360日となっており、定期的な変更を促されます。期限を超過するとログインできなくなるため無効にしておきます。
※パスワードの定期的な変更は実際の運用によるので、必要に応じて設定してください。
$ sudo vim /etc/my.cnf
: # 下記2行を末尾に追加
character-set-server=utf8
default_password_lifetime=0
# 再起動
$ sudo systemctl restart mysqld
自動起動
最後にサーバ起動時に自動的にMySQLも起動するようにします。
$ sudo systemctl enable mysqld
注意
紹介した手順は最低限の設定なので、本番環境を構築する際は運用に応じて必要な設定を追加しましょう。