4.MySQL 主主(m-m) 同步生产库标准同步操作实施流程
通过MySQL参数配置使用主主前提:
1、表的主键自增。
#################################################################
#m1-m2配置文件更改:再上篇M-S同步基础上增加:
#################################################################
m1(192.168.1.31 3306):主库配置文件中添加如下行
vim /data/3306/my.cnf 增加如下两行参数:
#_______m-m m1 start________
auto_increment_increment =2
auto_increment_offset =1
log-slave-updates
log-bin
= /data/3306/mysql-binexpire_logs_days =7
#_______m-m m1 end________
m2(192.168.1.31 3307):主库配置文件中添加如下行
#_______m-m m2 start________
auto_increment_increment =2
auto_increment_offset =2
log-slave-updates
log-bin = /data/3307/mysql-bin
expire_logs_days =7
#_______m-m m2 end________
参数说明:
解决主建自增长变量冲突:
Master1:
auto_increment_increment =2 #自增ID的间隔,如1 3 5间隔为2.
auto_increment_offset =1 #ID的初始位置
(将形成1,3,5,7,...序列)
Master2:
auto_increment_increment =2 #自增ID的间隔,如2 4 6间隔为2.
auto_increment_offset =2 #ID的初始位置
(将形成2,4,6,8,...序列)
2、配置完以后重启数据库
/data/3306/mysql stop
/data/3306/mysql start
/data/3307/mysql stop
/data/3307/mysql start
3、接着登录数据库中验证,都 是否开启
mysql> show variables like "log_%";
+---------------------------------+---------------------------------+
|Variable_name|Value|
+---------------------------------+---------------------------------+
| log_bin | ON |
| log_bin_trust_function_creators | OFF |
| log_error |/data/3306/mysql_oldboy3306.err |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | ON |
| log_slow_queries | OFF |
| log_warnings |1|
+---------------------------------+---------------------------------+
8 rows in set (0.00 sec)
mysql> show variables like "log_%";
+---------------------------------+---------------------------------+
|Variable_name|Value|
+---------------------------------+---------------------------------+
| log_bin | ON |
| log_bin_trust_function_creators | OFF |
| log_error |/data/3307/mysql_oldboy3307.err |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | ON |
| log_slow_queries | OFF |
| log_warnings |1|
+---------------------------------+---------------------------------+
8 rows in set (0.00 sec)
mysql> show variables like "auto_%";
+--------------------------+-------+
|Variable_name|Value|
+--------------------------+-------+
| auto_increment_increment |2|
| auto_increment_offset |1|
| autocommit | ON |
| automatic_sp_privileges | ON |
+--------------------------+-------+
4 rows in set (0.00 sec)
mysql> show variables like "auto_%";
+--------------------------+-------+
|Variable_name|Value|
+--------------------------+-------+
| auto_increment_increment |2|
| auto_increment_offset |2|
| autocommit | ON |
| automatic_sp_privileges | ON |
+--------------------------+-------+
4 rows in set (0.00 sec)
4、把备份的MySQL数据导入从库(Slave ):
首先打包数据
mysqldump -uroot -p123456 -S /data/3307/mysql.sock -A --events -B -x --master-data=1|gzip >/opt/3307_$(date +%F).sql.gz
[root@mysql opt]# gzip -d 3307_2016-04-09.sql.gz
[root@mysql opt]# mysql -uroot -p123456 -S /data/3306/mysql.sock <3307_2016-04-09.sql
接着CHANGE MASTER TO
[root@mysql opt]# mysql -uroot -p123456 -S /data/3306/mysql.sock<<EOF
> stop slave;
> CHANGE MASTER TO
> MASTER_HOST='192.168.1.31',
> MASTER_PORT=3307,
> MASTER_USER='rep',
> MASTER_PASSWORD='123456';
> EOF
5、检查状态是否同步
[root@mysql opt]# mysql -uroot -p123456 -S /data/3306/mysql.sock -e "start slave;show slave status \G"
***************************1. row ***************************
Slave_IO_State:Waitingfor master to send event
Master_Host:192.168.1.31
Master_User: rep
Master_Port:3307
Connect_Retry:60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos:585
Relay_Log_File: relay-bin.000004
Relay_Log_Pos:253
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
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:585
Relay_Log_Space:403
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 ##这个选项为0,表示正常
6、测试数据库是否 主主复制
1、首先登录3307数据库
[root@mysql scripts]# mysql -uroot -p123456 -S /data/3307/mysql.sock
2、创建zhurui1 库
mysql> create database zhurui1;
Query OK,1 row affected (0.02 sec)
3、登录3306数据库
[root@mysql opt]# mysql -uroot -p123456 -S /data/3306/mysql.sock
4、检查zhurui1 库是否复制过来
mysql> show databases;
+--------------------+
|Database|
+--------------------+
| information_schema |
| beautifulgirl |
| lian |
| mysql |
| performance_schema |
| zhu |
| zhurui |
| zhurui1 |
+--------------------+
8 rows in set (0.00 sec)
mysql>
7、在zhurui 库中创建表,测试是否主主复制
mysql> use zhurui
Database changed
mysql> CREATE TABLE `t1`(
->`id` bigint(12) NOT NULL auto_increment,
->`name` varchar(12) NOT NULL,
-> PRIMARY KEY (`id`)
->);
Query OK,0 rows affected (0.04 sec)
mysql> desc t1;##使用desc可以查看表结构
+-------+-------------+------+-----+---------+----------------+
|Field|Type|Null|Key|Default|Extra|
+-------+-------------+------+-----+---------+----------------+
| id | bigint(12)| NO | PRI | NULL | auto_increment |
| name | varchar(12)| NO || NULL ||
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.34 sec)
表中插入数据:
mysql> insert into t1(name) values("woduibuqini");##插入数据的sql语句
Query OK,1 row affected (0.01 sec)
mysql> select *from t1;##查看t1库
+----+--------------+
| id | name |
+----+--------------+
|1| zengqinglian |
|3| woaini |
|5| woduibuqini |
+----+--------------+
3 rows in set (0.00 sec)
########## 今天的苦逼是为了不这样一直苦逼下去!##########