Centos环境
198.168.1.20
198.168.1.21
master操作:
修改mysql配置文件(my.cnf)
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=xxx //要同步的库
binlog-ignore-db=mysql //不同步的
sync_binlog = 1 //类似实时存入binlog,=0则会先写进缓存
binlog_checksum = none //此命令可以同步不同版本的mysql数据库,mysql5.6.5以后的版本中binlog_checksum默认值是crc32,
binlog_format = mixed
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
锁定数据库flush tables with read lock;
//此命令为只读锁,unlock tables命令解除锁定
导出要同步的库
mysqldump -uroot 库名 -p > /root/mobi.sql
rsync传输sql库rsync -r "ssh -p22" -avpgolr /root/mobi.sql 192.168.1.21:/root
授权用户(测试注意防火墙的关闭)
grant replication slave,replication client on *.* to root@'192.168.1.21' identified by '123456';
flush privileges;
查看授权:show grants for root@'192.168.1.21';
查看主服务器状态:
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000024 | 592 | | |
+------------------+----------+--------------+------------------+
从服务器操作
修改配置文件my.cnf
server-id=2
log-bin=mysql-bin
replicate-do-db=mobi
replicate-ignore-db=mysql
slave-skip-errors = all
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql
如果有库则先dump下备份,避免操作失误
mysqldump -uroot 要备份的库 -p > /root/要备份的库.sql.bak
drop 要备份的库;
创建空库然后导入
mysql> create database 要备份的库;
mysql> use mobi;
mysql> source /root/要备份的库.sql
额外内容(同步表内容):
当只针对某些库的某张表进行同步时,如下,只同步huanqiu库的haha表和huanpc库的heihei表:
replicate-do-db = 库名
replicate-wild-do-table = 库名.表名 //当只同步几个或少数表时,可以这样设置。注意这要跟上面的库指定配合使用;
replicate-do-db = 库名
replicate-wild-do-table = 库名.表名 //如果同步的库的表比较多时,就不能这样一一指定了,就把这个选项配置去掉,直接根据指定的库进行同步。
=================================
配置主从指令:
mysql> stop slave;(同步前先关闭)
mysql> change master to master_host='192.168.1.20',master_user='root',master_password='123456',master_log_file='mysql-bin.000024',master_log_pos=592;
mysql> start slave;
从库先查看slave状态
show slave status\G;
看是不是两个yes 如果不是就有问题
测试阶段(主库)
mysql> unlock tables;
//解锁mysql> create table zhang (id,int)
从库查看
mysql> desc zws;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| student | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
主主同步
=============================================================================
=============================================================================
master中修改
修改配置文件 my.cnf
server-id = 1
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 1
slave-skip-errors = all
授权用户:
mysql> grant replication slave,replication client on *.* to root@'192.168.1.21' identified by "123456";
mysql> flush privileges;
mysql> FLUSH TABLES WITH READ LOCK;
//注意该参数设置后,如果自己同步对方数据,同步前一定要记得先解锁!
mysql> show master status;
//查看主信息
slave库配置
修改配置文件my.cnf
[mysqld]
server-id=2
log-bin=mysql-bin
replicate-do-db=mobi
replicate-ignore-db=mysql
slave-skip-errors = all
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 2
slave-skip-errors = all
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql
授权用户
mysql> grant replication slave ,replication client on *.* to root@'192.168.1.20' identified by "123456";
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000020 | 602 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
主库解锁
mysql> unlock tables; //先解锁,将对方数据同步到自己的数据库中
mysql> stop slave ;
mysql> change master to master_host='192.168.1.20',master_user='root',master_password='123456',master_log_file='master-bin.000027',master_log_pos=150;
mysql> start slave;
mysql> show slave status \G;
此处实现了slave--->master的功能
====================================================================
master---->slave功能
mysql> unlock tables;
mysql> stop slave ;
mysql> change master to master_host='192.168.1.21',master_user='root',master_password='123456',master_log_file='master-bin.000023',master_log_pos=150;
mysql> start slave;
mysql> show slave status \G;
此刻完成
====================================================================
问题排错:
出现
Slave_IO_Running: No
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
解决方案:
1)从库上执行
stop slave;
2)主库进行刷新日志
flush logs;
3)查看日志
mysql> show master status\G;
4)重新
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000114',MASTER_LOG_POS=120;
5)重启
start slave;