事例基于Linux系统
1. 准备两个数据库
主库:192.168.239.234
从库:192.168.239.235
两个数据库的版本最好一致
2. 主库Master配置
- 首先在Mysql的安装目录下面找到 my.cnf 配置文件
[root@localhost ~]# ls /usr/local/mysql/
bin data include man mysql-test scripts sql-bench
COPYING docs lib my.cnf README share support-files
- 然后修改my.cnf文件,在 [mysqld] 下添加配置
[root@localhost ~]# vi /usr/local/mysql/my.cnf
[mysqld]
//添加如下代码
log-bin=mysql-bin //开启二进制日志
server-id=1 //设置server-id
之后保存退出
- 重启Mysql,创建用于同步的账号
//重启Mysql
[root@localhost ~]# service mysqld restart
//打开Mysql会话
[root@localhost ~]# mysql -u root -p
//输入密码之后进入会话
//创建用户并授权:用户:synchro 密码:123456
mysql> CREATE USER 'synchro'@'192.168.239.235' IDENTIFIED BY '123456'; //创建用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'synchro'@'192.168.239.235'; //分配权限
mysql> flush privileges; //刷新权限
- 查看Master状态,记录二进制文件名(mysql-bin.000006)和位置(120):
mysql> SHOW MASTER STATUS; //查看状态
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3. 从库slave配置
- 同样先修改 my.cnf 文件
[root@localhost ~]# vi /usr/local/mysql/my.cnf
[mysqld]
//添加如下代码
server-id=2 //设置server-id,必须唯一
- 重启Mysql服务,打开Mysql会话,执行同步SQL语句(需要主服务器主机名,登陆凭据,二进制文件的名称和位置):
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.239.234',
-> MASTER_USER='synchro',
-> MASTER_PASSWORD='123456',
-> MASTER_LOG_FILE='mysql-bin.000006',
-> MASTER_LOG_POS=120; //注意最后的数字不带单引号
- 启动slave同步进程:
mysql> start slave;
- 查看slave状态:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.234
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000009
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000006
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: 120
Relay_Log_Space: 623
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
Master_UUID: 7d55ead0-f134-11e7-bd0c-000c294d0dd7
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.02 sec)
ERROR:
No query specified
当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了, 然后就可以通过插入数据等操作来测试同步是否可用。还可以关闭slave(mysql> stop slave;),然后再修改master,看slave是否也相应修改(停止slave后,master的修改不会同步到slave),就可以完成主从复制功能的验证了。
master开启二进制日志后默认记录所有库所有表的操作,可以通过配置来指定只记录指定的数据库甚至指定的表的操作,具体在mysql配置文件的 [mysqld] 可添加修改如下选项:
// 不同步哪些数据库
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
//只同步哪些数据库,除此之外,其他不同步
binlog-do-db = game