1 环境描述
2 检查主库相关配置
2.1 确认主库的server-id已经配置
root@localhost 00:38:55 [(none)]> show global variables like 'server_id';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| server_id | 703306 |
+---------------+--------+
1 row in set (0.00 sec)
2.2 确认主库的binlog是否开启
root@localhost 00:50:40 [(none)]> show global variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
若没有开启,那么需要重启数据库的:
(1)在my.cnf参数文件中[mysqld]中添加
log-bin = mysql-bin
(2)重启MySQL数据库。
2.3 检查到从库的网络及端口是否可达
[root@mysql01 ~]# netstat -tnlp|grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1220/mysqld
[root@mysql01 ~]# telnet 192.168.230.71 3306
Trying 192.168.230.71...
Connected to 192.168.230.71.
Escape character is '^]'.
2.4 在主库创建同步账号并授权REPLICATION
root@localhost 01:28:42 [(none)]> CREATE USER 'repl'@'192.168.230.%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.01 sec)
root@localhost 01:28:50 [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'192.168.230.%';
Query OK, 0 rows affected (0.13 sec)
然后在从库中测试是否可以使用repl用户登录主库
[root@mysql02 ~]# mysql -urepl -preplpass -h192.168.230.70 -P3306
2.5 记录master上的当前二进制日志名和偏移量
主要是用于slave中指定开始恢复的位置。
root@localhost 15:31:10 [(none)]> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 107
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
3 全备主库
[root@mysql01 opt]# mysqldump -S /tmp/mysql.sock --all-databases --single-transaction --master-data=1 --flush-logs > masterdb.sql
[root@mysql01 opt]# ls -l masterdb.sql
-rw-r--r-- 1 root 555559 Jul 4 01:21 masterdb.sql
将备份文件scp到从库中:
[root@mysql01 opt]# scp masterdb.sql 192.168.230.71:/opt
4 将主库的备份文件导入到从库中
# mysql -f < masterdb.sql
说明:这里要加上-f,强制导入,如果有错误,也要强制继续执行导入
5 在slave上执行change master设置主服务器复制信息
root@localhost 15:58:24 [(none)]> change master to master_host='192.168.230.70', master_user='repl', master_password='replpass',master_port=3306,master_log_file='mysql-bin.000007',master_log_pos=107;
Query OK, 0 rows affected (0.06 sec)
注意,上面的master_log_file和master_log_pos就是刚刚记录的主服务器日志信息了:
root@localhost 15:31:10 [(none)]> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 107
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
6 在slave上启动复制
root@localhost 16:00:56 [(none)]> start slave;
Query OK, 0 rows affected (0.02 sec)
7 检查主从复制是否正常
root@localhost 16:02:20 [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.230.70
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 107
Relay_Log_File: mysql-relay-bin.000006
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes ## 这两个都是“Yes”,表示复制是OK的
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: 452
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: 703306
1 row in set (0.00 sec)
8 将slave设置为只读
mysql> set global read_only=true;
Query OK, 0 rows affected (0.01 sec)
说明:read-only模式只对不具有super权限和replication权限的用户生效。
也可以在my.cnf中配置:
read_only=1
另外,slave进程默认会随着从库启动而自动启用,如果想关闭,那么可以在my.cnf中配置:
skip-slave-start
2 检查主库相关配置
2.1 确认主库的server-id已经配置
root@localhost 00:38:55 [(none)]> show global variables like 'server_id';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| server_id | 703306 |
+---------------+--------+
1 row in set (0.00 sec)
2.2 确认主库的binlog是否开启
root@localhost 00:50:40 [(none)]> show global variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
若没有开启,那么需要重启数据库的:
(1)在my.cnf参数文件中[mysqld]中添加
log-bin = mysql-bin
(2)重启MySQL数据库。
2.3 检查到从库的网络及端口是否可达
[root@mysql01 ~]# netstat -tnlp|grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1220/mysqld
[root@mysql01 ~]# telnet 192.168.230.71 3306
Trying 192.168.230.71...
Connected to 192.168.230.71.
Escape character is '^]'.
2.4 在主库创建同步账号并授权REPLICATION
root@localhost 01:28:42 [(none)]> CREATE USER 'repl'@'192.168.230.%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.01 sec)
root@localhost 01:28:50 [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'192.168.230.%';
Query OK, 0 rows affected (0.13 sec)
然后在从库中测试是否可以使用repl用户登录主库
[root@mysql02 ~]# mysql -urepl -preplpass -h192.168.230.70 -P3306
2.5 记录master上的当前二进制日志名和偏移量
主要是用于slave中指定开始恢复的位置。
root@localhost 15:31:10 [(none)]> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 107
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
3 全备主库
[root@mysql01 opt]# mysqldump -S /tmp/mysql.sock --all-databases --single-transaction --master-data=1 --flush-logs > masterdb.sql
[root@mysql01 opt]# ls -l masterdb.sql
-rw-r--r-- 1 root 555559 Jul 4 01:21 masterdb.sql
将备份文件scp到从库中:
[root@mysql01 opt]# scp masterdb.sql 192.168.230.71:/opt
4 将主库的备份文件导入到从库中
# mysql -f < masterdb.sql
说明:这里要加上-f,强制导入,如果有错误,也要强制继续执行导入
5 在slave上执行change master设置主服务器复制信息
root@localhost 15:58:24 [(none)]> change master to master_host='192.168.230.70', master_user='repl', master_password='replpass',master_port=3306,master_log_file='mysql-bin.000007',master_log_pos=107;
Query OK, 0 rows affected (0.06 sec)
注意,上面的master_log_file和master_log_pos就是刚刚记录的主服务器日志信息了:
root@localhost 15:31:10 [(none)]> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 107
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
6 在slave上启动复制
root@localhost 16:00:56 [(none)]> start slave;
Query OK, 0 rows affected (0.02 sec)
7 检查主从复制是否正常
root@localhost 16:02:20 [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.230.70
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 107
Relay_Log_File: mysql-relay-bin.000006
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes ## 这两个都是“Yes”,表示复制是OK的
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: 452
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: 703306
1 row in set (0.00 sec)
8 将slave设置为只读
mysql> set global read_only=true;
Query OK, 0 rows affected (0.01 sec)
说明:read-only模式只对不具有super权限和replication权限的用户生效。
也可以在my.cnf中配置:
read_only=1
另外,slave进程默认会随着从库启动而自动启用,如果想关闭,那么可以在my.cnf中配置:
skip-slave-start