mysql 主从复制配置
主服务器
修改主库配置my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1 (取值1-2的32次方减1,主从关系内要保证唯一)
确保skip-networking配置关闭
默认安装完成就是这样,修改之后要重启mysql服务
创建复制用户并赋予REPLICATION SLAVE权限
>create user repl@'%' identified by '123456';
>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
>flush privileges;
查看主库master状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 107 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
File:log文件名
Position:文件中的位置
记录着两个值,从库配置需要这两个参数。这两个值标识从库从哪个位置开始从主库进行更新。
slave从服务器
修改从库配置my.cnf
log-bin=mysql-bin
server-id=2 (取值1-2的32次方减1,主从关系内要保证唯一)
设置slave复制参数(CHANGE MASTER TO)
mysql>CHANGE MASTER TO
MASTER_HOST='x.x.x.x',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=107;
MASTER_HOST 主库地址
MASTER_USER 主从同步使用用户名
MASTER_PASSWORD 用户密码
MASTER_LOG_FILE 主库log文件名
MASTER_LOG_POS log文件开始同步位置
这四个参数可从上面的步骤中获取
启动slave同步
mysql>start slave;
查看状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: x.x.x.x
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 107
Relay_Log_File: xxx-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000004
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: 416
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)
配置完成,可在主库中做操作看在从库中是否同步。
********
Slave_IO_Running和Slave_SQL_Running都为Yes时表示正常,若有值为No,则从库同步失败。
若有错误Last_Error或Last_SQL_Error一般会有说明信息,可根据信息进行适当的修改。
重新对从库进行CHANGE MASTER TO操作,重启slave
mysql>stop slave;(先关闭slave再启动)
mysql>start slave;
关于log-bin
查看日志文件
mysql> show binary logs
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 27708 |
| mysql-bin.000002 | 1112220 |
| mysql-bin.000003 | 312587 |
| mysql-bin.000004 | 1604 |
+------------------+-----------+
查看日志记录内容
SHOW BINLOG EVENTS
[IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
用于在二进制日志中显示事件。如果您不指定'log_name',则显示第一个二进制日志。
from开始位置,LIMIT子句和SELECT语句具有相同的语法。
mysql>show binlog events in 'mysql-bin.000004' from 107 limit 0,1\G;
*************************** 1. row ***************************
Log_name: mysql-bin.000004
Pos: 107
Event_type: Query
Server_id: 1
End_log_pos: 275
Info: use `db_edu`; CREATE TABLE `test` (
`id` INT NULL
)
COMMENT='232'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
1 rows in set (0.01 sec)
参考
http://dev.mysql.com/doc/refman/5.7/en/replication-howto.html