MySQLレプリケーション設定

IPTABLES設定 (マスター側)
[root@centos ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
my.cnf設定 (マスター側)
[root@centos ~]# vi /etc/my.cnf
[mysqld]
character-set-server=utf8
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id = 1 ←追加
log-bin = mysql-bin ←追加
[root@centos ~]# systemctl restart mariadb
レプリケーション専用ユーザー登録 (マスター側)
[root@centos ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.28-log MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2012, 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> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.server-manual.com' IDENTIFIED BY 'replpassword';
ヒント
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'slave_host' IDENTIFIED BY 'slave_pass'; slave_user:スレーブユーザーの名前 slave_host:アクセスを許可するスレーブのホスト名 slave_pass:スレープユーザーのパスワード
バックアップを作成 (マスター側)
mysql> FLUSH TABLES WITH READ LOCK;
mysql> quit;
[root@centos ~]# tar cvfz /home/mysql-backup.tar.gz /home/mysql
[root@centos ~]# mysql -u root -p
mysql> UNLOCK TABLES;
マスターの情報確認 (マスター側)
mysql> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      107 |              |                  | ←FileとPositionを確認
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MySQLを停止してリストア (スレーブ側)
[root@centos ~]# systemctl stop mariadb
[root@centos ~]# cd /home
[root@centos ~]# rm -fr mysql
[root@centos ~]# tar zxvf mysql-backgup.tar.gz mysql
[root@centos ~]# systemctl start mariadb
my.cnf設定 (スレーブ側)
[root@centos ~]# vi /etc/my.cnf
server-id = 2 ←追加(マスターと被らない数字)
log-bin = mysql-bin ←追加
[root@centos ~]# systemctl restart mariadb [root@centos ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.28-log MySQL Community Server (GPL) by Remi Copyright (c) 2000, 2012, 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> CHANGE MASTER TO MASTER_HOST='server-manual.com', MASTER_USER='repl', MASTER_PASSWORD='replpassword', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107; ←MASTER_LOG_FILEとMASTER_LOG_POSはマスターの「SHOW MASTER STATUS」で確認した値に合わせる mysql> START SLAVE; mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: server-manual.com Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 107 Relay_Log_File: mysqld-relay-bin.000003 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 556 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
ヒント
mysql> CHANGE MASTER TO MASTER_HOST='master_host', MASTER_USER='slave_user', MASTER_PASSWORD='slave_password', MASTER_LOG_FILE='ファイル名', MASTER_LOG_POS=ポジション名;
動作確認(マスター)
[root@centos ~]# mysql -u root -p
mysql> SHOW DATABASES;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

mysql> CREATE DATABASE test_db;
mysql> SHOW DATABASES;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test_db            | ←マスターにtest_dbが作成された
+--------------------+
3 rows in set (0.00 sec)
動作確認(スレーブ)
[root@centos ~]# mysql -u root -p
mysql> SHOW DATABASES;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test_db            | ←スレーブにtest_dbが作成されている
+--------------------+
3 rows in set (0.00 sec)
エラー対処法
mysql> STOP SLAVE;
mysql> SHOW SLAVE STATUS \G ←スレーブの停止を確認
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; ←エラーをスキップ
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS \G ←スレーブの再開を確認
監視スクリプト
[root@centos ~]# vi /etc/cron.hourly/mysql-replication
#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

MYSQLPASS="mysql_root_pass"
MAIL="hoge@example.com"

eval  "`mysql -u root -p${MYSQLPASS} -e 'show slave status \G' | sed -ne 's/: \(.*\)/="\1"/p' `"
if [ "$?${Slave_IO_Running}${Slave_SQL_Running}" != "0YesYes" ]; then
mysql -u root -p"${MYSQLPASS}" -e 'show slave status \G' | mail ${MAIL} -s "REPLICATION DOWN "`hostname`
exit 1
fi
[root@centos ~]# chmod +x /etc/cron.hourly/mysql-replication
Home PageTop