引き続き インストール作業を続けます。今回はMySQLです。
CentOS7の標準DBサーバーはMariaDBとなりました。基本的にMySQLとの互換性はありますが、今回は旧環境から使用しているMySQLを引き続き使用することにします。
MySQL 環境構築
MySQLのインストール
MySQLとMariaDBの競合を避けるためMariaDBをアンインストール
sudo su yum -y remove mariadb-libs
最新版のMySQL 5.7 をインストールします。こちらで RHE Linux 7のRPM Packageを確認してyum リポジトリを追加します。
rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
インストールして自動起動設定をします。
yum -y install mysql-community-server
yum -y install mysql-devel
systemctl start mysqld.service
systemctl enable mysqld.service
MySQL のセキュリティ設定
まず初期パスワードを確認しておきます。
grep password /var/log/mysqld.log
A temporary password is generated for root@localhost:ここが初期パスワード
セキュリティ設定を起動
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' 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を入力
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 -u root -p Enter password: 上で設定した新しいパスワードを入力
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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の設定
文字コードの設定やパスワード有効期限に設定などを行います。
vim /etc/my.cnf
追加設定例
[mysqld]
........................
default_password_lifetime=0 # パスワード有効期限を無効化
character_set_server=utf8
default-storage-engine=InnoDB
innodb_file_per_table
[mysql]
default-character-set=utf8
[mysqldump]
default-character-set=utf8
再起動します。
systemctl restart mysqld.service
外部からMySQLデータベースに接続したい場合は、MySQLポートを開けておきます。
iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT service iptables save iptables -L systemctl restart iptables
外部からの接続はセキュリティ上は好ましくありませんので、MySQLポートは開けておかないほうがよいでしょう。
データ移行
旧環境のMySQLデータをバックアップ
以下のコマンドでデータベース全体をバックアップします。
mysqldump -h localhost -u MySQLユーザー名 -p データベース名 >backup.dump
旧環境のbackup.dumpを、新環境へコピーします。データ移行タイミングは通常は旧環境から新環境への切り替え時となります。今回はDNSのAレコードを変更したときに旧環境のWebサーバーを停止させ、その後、旧環境のバックアップ&新環境へのリストアを行いました。
新環境へリストア
まず管理者ユーザーでデータベースを作成、旧環境と同じデータベース名とします。
mysql -u root -p
create database データベース名 default character set utf8;
続いてデータベース接続ユーザーを作成、旧環境と同じユーザー名とします。
grant all privileges on db_name.* to ユーザー名@'%' identified by 'パスワード';
バックアップデータをリストアします。
mysql -u ユーザー名 -p データベース名 < backup.dump
次回はWebサーバーのインストールです。