MySQL單機多實例-主主複製

MySQL主從複製簡介

    MySQL的主從複製方案與scp/rsync等文件級別同步是類似的,都是數據的傳輸。只不過MySQL無需藉助第三方工具,而是其自帶的複製功能,MySQL的主從複製並不是數據庫磁盤上的文件直接拷貝複製,而是通過邏輯的Binlog日誌複製到同步的數據本地然後讀取裏面SQL語句應用到數據庫的過程。


實戰操作

   雙實例端口

[root@db02 3306]# netstat -luntp|grep 330
tcp        0     0 0.0.0.0:3306         0.0.0.0:*               LISTEN      64040/mysqld       
tcp        0     0 0.0.0.0:3307         0.0.0.0:*               LISTEN      55735/mysqld

    MySQL版本號

mysql> select version(); 
+-----------+
| version() |
+-----------+
| 5.5.32    |
+-----------+
1 row in set (0.00 sec)


   1.修改雙主my.cnf配置文件

        3306 

[root@db02 3306]# grep -B 6 "stop" my.cnf  
#______m-m m1 start_________
auto_increment_increment = 2    
auto_increment_offset =   1     
log-slave-updates               
log-bin  = /data/3306/mysql-bin 
expire_logs_days = 7           
#______m-m m1 stop_________

        3307

[root@db02 3306]# grep -B 6 "stop" ../3307/my.cnf 
#______m-m m1 start_________
auto_increment_increment = 2
auto_increment_offset =  2     
log-slave-updates
log-bin  = /data/3307/mysql-bin
expire_logs_days = 7
#______m-m m1 stop_________

     2.重啓服務

[root@db02 3306]# ./mysql stop                    
Stoping MySQL...
[root@db02 3306]# ./mysql start
Starting MySQL...
[root@db02 3306]# ../3307/mysql stop
Stoping MySQL...
[root@db02 3306]# ../3307/mysql start
Starting MySQL...
[root@db02 3306]# netstat -luntp|grep 330         
tcp        0     0 0.0.0.0:3306            0.0.0.0:*                LISTEN      3691/mysqld         
tcp        0     0 0.0.0.0:3307            0.0.0.0:*                LISTEN      4464/mysqld

    3.進入3306數據庫創建rep用戶進行授權

mysql> grant replication slave on *.* to rep@'172.16.1.%' identified by 'oldboy123';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for rep@'172.16.1.%'\G                                           
*************************** 1. row ***************************
Grants for [email protected].%: GRANT REPLICATION SLAVE ON *.* TO 'rep'@'172.16.1.%' IDENTIFIED BY PASSWORD '*FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515'
1 row in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

    4.備份3306數據庫,並導入3307數據量。

[root@db02 3306]# mysqldump -uroot -poldboy123 -S /data/3306/mysql.sock -A -B -x --events --master-data=2|gzip >/server/backup/rep_3306_$(date +%F).sql.gz
[root@db02 3306]# ls -l /server/backup/rep_3306_$(date +%F).sql.gz
-rw-r--r-- 1 root root 144934 12月 25 22:57 /server/backup/rep_3306_2015-12-25.sql.gz
[root@db02 3306]# gzip -d /server/backup/rep_3306_2015-12-25.sql.gz
[root@db02 3306]# ll /server/backup/rep_3306_2015-12-25.sql
-rw-r--r-- 1 root root 531949 12月 25 22:57 /server/backup/rep_3306_2015-12-25.sql
[root@db02 3306]# mysql -uroot -poldboy123 -S /data/3307/mysql.sock </server/backup/rep_3306_2015-12-25.sql

    在備份是一定要--master-data=2,不然在導入3307時會報錯,每次備份完成後檢查。

    4.進入3307數據庫進行與3306複製的修改

[root@db02 3306]# mysql -uroot -poldboy123 -S /data/3307/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
........
mysql> CHANGE MASTER TO  
    -> MASTER_HOST='172.16.1.52',
    -> MASTER_PORT=3306,
    -> MASTER_USER='rep', 
    -> MASTER_PASSWORD='oldboy123';
Query OK, 0 rows affected (0.03 sec)

mysql> start slave;                
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.52
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
........

    5.進行測試

        3306創建

mysql> create database abc;
Query OK, 1 row affected (0.00 sec)

         3307查看

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| mysql              |
| oldboy             |
| oldboy_gbk         |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.00 sec)

    6.備份3307數據庫並導入3306數據庫。

[root@db02 ~]# mysqldump -uroot -poldboy123 -S /data/3306/mysql.sock -A -B -x --events --master-data=2|gzip >/server/backup/rep_3307_$(date +%F).sql.gz 
[root@db02 ~]# ls -l /server/backup/rep_3307_$(date +%F).sql.gz 
-rw-r--r-- 1 root root 144734 12月 25 23:31 /server/backup/rep_3307_2015-12-25.sql.gz
[root@db02 ~]# gzip -d /server/backup/rep_3307_2015-12-25.sql.gz
[root@db02 ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock  </server/backup/rep_3307_2015-12-25.sql

mysql> grant replication slave on *.*  to rep@'172.16.1.%';
Query OK, 0 rows affected (0.00 sec)

    7.進入3306數據庫進行與3307複製的修改

[root@db02 ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock   
.......
mysql> CHANGE MASTER TO  
    -> MASTER_HOST='172.16.1.52',
    -> MASTER_PORT=3307,
    -> MASTER_USER='rep', 
    -> MASTER_PASSWORD='oldboy123';
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.52
                  Master_User: rep
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1064183
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 1061699
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

    8.進行兩端自增長測試

        3306

mysql> CREATE TABLE `zizeng` (
    ->   `REL_ID` bigint(12) NOT NULL auto_increment COMMENT '流水id',
    ->   `ARTICLE_TITLE` varchar(255) NOT NULL COMMENT '文章標題',
    ->   PRIMARY KEY  (`REL_ID`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into zizeng(ARTICLE_TITLE) values('odlboy');
Query OK, 1 row affected (0.00 sec)
mysql> insert into zizeng(ARTICLE_TITLE) values('oldgirl');
Query OK, 1 row affected (0.00 sec)
mysql> select * from zizeng;
+--------+---------------+
| REL_ID | ARTICLE_TITLE |
+--------+---------------+
|      1 | odlboy        |
|      3 | oldgirl       |
+--------+---------------+
2 rows in set (0.00 sec)

        3307

mysql> select * from zizeng;
+--------+---------------+
| REL_ID | ARTICLE_TITLE |
+--------+---------------+
|      1 | odlboy        |
|      3 | oldgirl       |
+--------+---------------+
2 rows in set (0.00 sec)
mysql> insert into zizeng(ARTICLE_TITLE) values('abc');   
Query OK, 1 row affected (0.00 sec)
mysql> insert into zizeng(ARTICLE_TITLE) values('abcd');
Query OK, 1 row affected (0.00 sec)
mysql> select * from zizeng;                            
+--------+---------------+
| REL_ID | ARTICLE_TITLE |
+--------+---------------+
|      1 | odlboy        |
|      3 | oldgirl       |
|      4 | abc           |
|      6 | abcd          |
+--------+---------------+
4 rows in set (0.00 sec)


常見問題

    1、授權問題

    mysql5.5授權的時候:

GRANT REPLICATION CLIENT ON *.* TO 'rep'@'10.0.0.%' IDENTIFIED BY PASSWORD '123456'

    在主從複製的時候狀態會一直顯示connecting,所以在授權時請選擇:

GRANT REPLICATION SLAVE ON *.* TO 'rep'@'10.0.0.%' IDENTIFIED BY PASSWORD '123456'

    

    2、change master配置

    change master 裏面的東西如果設置錯了,修改master info不會生效的,請執行 reset slave all;進行清除所有slave數據。

    

    3、change master空格

    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'

    這個是配置

    CHANGE MASTER TO   
    MASTER_HOST='172.16.1.52',  
    MASTER_PORT=3306, MASTER_USER='rep',  
    MASTER_PASSWORD='oldboy123',  
    MASTER_LOG_FILE=' mysql-bin.000010', 
    MASTER_LOG_POS=194;

    其中MASTER_LOG_FILE=' mysql-bin.000010',文件位置多了空格。切記不可加空格!!!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章