MySQL 主从复制配置

系统约定

主库IP:10.0.0.3,从库IP:10.0.0.4

同步账户:repl

同步数据库:beta

假如主从都无应用的情况配置过程

主库(10.0.0.3)

# vim /etc/my.cnf
[mysqld]

..........

log-bin = mysql-bin
binlog_format = mixed
binlog-ignore-db=mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
binlog-do-db = beta

server-id = 1
mysql> GRANT REPLICATION SLAVE ON *.* to repl@10.0.0.4 identified by '123456';
mysql> flush privileges;

#此账号是主库的账号,host设为从库可访问,最好设为%。

mysql> show master status;

记住File和Position。

从库 (10.0.0.4)

vim /etc/my.cnf
[mysqld]

log-bin=mysql-bin
binlog_format=mixed

server-id = 2

replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = test.%
replicate-wild-ignore-table = information_schema.%
replicate-wild-ignore-table = performance_schema.%
replicate_wild_do_table = beta.%
log-slave-updates
skip-slave-start
read_only
relay-log = /data/mysql/mysql-relay-bin
mysql> stop slave;
mysql> change master to
master_host='10.0.0.3',
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000123',
master_log_pos=123;

启动从服务器复制

mysql> start slave;

查看slave状态

mysql> show slave status\G;

如果看到

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

都为yes,说明配置成功

说明:如果主库不是空数据库,库里已经有数据,就在(show master status;)前执行锁定表(flush tables with read lock;),然后把数据备份出来后,然后再unlock tables;采用mysqldump备份时候加上–master-data参数,如下mysqldump –master-data -u root -p 123admin > 123admin.sql这样就可以保留file和position的信息,在新搭建一个slave的时候,还原完数据库,file和position的信息也随之更新,接着再start slave 就可以很迅速的完成增量同步!