測試主主複製,兩個主機互爲主,爲了解決記錄衝突,能夠將主鍵設成起始設置爲不一樣(如1,3,5,另外一個2,4,6),增加設置爲相同爲2,這樣主鍵不衝突就能夠實現複製,但仍是沒法實現建表建庫相同的衝突。mysql
1. 配置主服務器 2. 配置第二個主服務器 3. 測試
[root@master1 ~]#yum install mariadb-server [root@master1 ~]#mkdir /data/{mysql,logs} [root@master1 ~]#chown mysql:mysql /data/{mysql,logs}
[root@master1 ~]#vi /etc/my.cnf [mysqld] server-id=17 datadir=/data/mysql log-bin=/data/logs/bin auto_increment_offset=1 auto_increment_increment=2 [root@master1 ~]#systemctl start mariadb
[root@master1 ~]#mysql < hellodb_innodb.sql MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.205.%' identified by 'centos';
[root@master1 ~]#mysqldump -A --single-transaction --master-data=1 > /data/all.sql [root@master1 ~]#scp /data/all.sql 192.168.205.27:/data/
[root@master2 ~]#vi /etc/my.cnf [mysqld] server-id=27 datadir=/data/mysql log-bin=/data/logs/bin auto_increment_offset=2 auto_increment_increment=2 [root@master2 ~]#systemctl start mariadb
[root@master2 ~]#vi /data/all.sql CHANGE MASTER TO MASTER_HOST='192.168.205.17', MASTER_USER='repluser', MASTER_PASSWORD='centos', MASTER_PORT=3306, [root@master2 ~]#mysql < /data/all.sql
MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.205.17 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000003 Read_Master_Log_Pos: 8988 Relay_Log_File: mariadb-relay-bin.000003 Relay_Log_Pos: 523 Relay_Master_Log_File: bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes
MariaDB [(none)]> show master logs; +------------+-----------+ | Log_name | File_size | +------------+-----------+ | bin.000001 | 30833 | | bin.000002 | 1069459 | | bin.000003 | 522771 | +------------+-----------+ 3 rows in set (0.00 sec)
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.205.27', MASTER_USER='repluser', MASTER_PASSWORD='centos', MASTER_PORT=3306, MASTER_LOG_FILE='bin.000003', MASTER_LOG_POS=522771; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.205.27 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000003 Read_Master_Log_Pos: 523513 Relay_Log_File: mariadb-relay-bin.000003 Relay_Log_Pos: 523 Relay_Master_Log_File: bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes
MariaDB [(none)]> user hellodb; MariaDB [(none)]> create table test (id int auto_increment primary key, name char(10)); MariaDB [(none)]> desc test; MariaDB [(none)]> insert test (name) values ("leo")
MariaDB [hellodb]> insert test (name) values('zhao'),('song'); MariaDB [hellodb]> select * from test; +----+------+ | id | name | +----+------+ | 1 | leo | | 2 | leo | | 3 | zhao | | 4 | zhao | | 5 | song | | 6 | song | +----+------+ 6 rows in set (0.00 sec)
MariaDB [hellodb]> create bable test2(id int); MariaDB [hellodb]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.205.27 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000003 Read_Master_Log_Pos: 523513 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 1168 Relay_Master_Log_File: bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: No Last_Errno: 1050 Last_Error: Error 'Table 'test2' already exists' on query. Default database: 'hellodb'. Query: 'create table test2(id int)'
MariaDB [hellodb]> set global sql_slave_skip_counter=1; MariaDB [hellodb]> stop slave; MariaDB [hellodb]> start slave; MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.205.27 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000003 Read_Master_Log_Pos: 523513 Relay_Log_File: mariadb-relay-bin.000003 Relay_Log_Pos: 523 Relay_Master_Log_File: bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes