主要介绍了如何部署 MySQL 主从模式,下面是 MySQL 的两台主机:
master: 192.168.10.10
slave : 192.168.10.11
两个实例都是新装 MySQL实例。
1 启动 master 主机上的 mysql 实例,注意下面的相关配置:
[mysqld]
server-id = 1
log-bin = /mysql/data/bin
2 启动 slave 主机上的 mysql 实例,注意下面的相关配置:
server-id = 2
relay-log = /mysql/data/relay-bin
replicate-do-db = db1,db2
replicate-ignore-db = mysql,information_schema
slave-skip-errors = all
skip-slave-start = 1
# log_slave_updates = 1
3 在 master 主机增加主从同步用户,并赋予相关权限:
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replication_user'@'192.168.10.11' IDENTIFIED BY PASSWORD 'helloworld';
mysql> flush privileges;
note: 如果报如下错误:
ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number
只需要按照如下处理即可:
mysql> select password('helloworld');
+-------------------------------------------+
| password('helloworld') |
+-------------------------------------------+
| *D35DB127DB631E6E27C6B75E8D376B04F64FAF83 |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replication_user'@'192.168.10.11' IDENTIFIED BY PASSWORD '*D35DB127DB631E6E27C6B75E8D376B04F64FAF83';
mysql> flush privileges;
4 在 slave 主机上设置 master 信息,并启动同步线程:
-- 1 设置 master-info :
mysql> change master to master_host='192.168.10.10',
master_user='replication_user',
master_password='helloworld',
master_log_file='bin.000001',
master_log_pos=0;
-- 2 查看设置的是否正确:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.10.10
Master_User: replication_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bin.000001
Read_Master_Log_Pos: 4
...
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB: db1,db2
Replicate_Ignore_DB: mysql,information_schema
...
Master_Server_Id: 1
1 row in set (0.00 sec)
-- 3 启动 slave 线程:
mysql> start slave;
-- 4 查看 slave 复制的状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.10
Master_User: replication_user
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
Last_Errno: 0
Last_Error:
...
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Master_Server_Id: 1
1 row in set (0.00 sec)
-- 5 如果看到上面的状态输出,则表示 MySQL 主从模式部署完成
5 其他
-- 1 查看正从 master 复制的 sql :
mysql> show processlist;
-- 2 停止 slave 复制:
mysql> stop mysql;