通過MySQL參數配置使用主主前提: 1、表的主鍵自增。 ################################################################# #m1-m2配置文件更改:再上篇M-S同步基礎上增加: ########################### ...
通過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________
#_______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________
/data/3306/mysql stop
/data/3306/mysql start
/data/3307/mysql stop
/data/3307/mysql start
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)
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
[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,表示正常
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>
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)