Mysql 5.7主主複製配置

MySQL5.7主主複製配置

主機1IP:192.168.1.2
主機2IP:192.168.1.4

1、首先安裝MySQL 5.7
一、卸載兩臺主機系統中已經有的mysql相關軟件包
rpm -qa | grep -i mysql
yum remove mysql(相關軟件包名稱) -y

二、分別在兩臺主機系統中執行如下命令
在http://dev.mysql.com/downloads/repo/yum/站點下載mysql57-community-release-el6-8.noarch
rpm -ivh mysql57-community-release-el6-8.noarch.rpm
yum -y install mysql.x86_64 mysql-server.x86_64 mysql-devel.x86_64

三、分別在兩臺主機上執行如下語句,初始化

mkdir /home/mysql
mysqld --initialize-insecure --user=mysql --datadir=/home/mysql

備註:
    若是你用 --initialize 初始化data目錄,請用以下命令登陸服務
    mysql -u root -p
    執行上面命令,會提示輸入密碼,輸入隨機生成的密碼便可。
    若是你不知道這個隨機密碼,請查看error log文件查找這個隨機密碼。
       
    若是用 --initialize-insecure 初始化data目錄,請用root用登陸,並不須要輸入密碼就能夠登陸,以下命令:
    mysql -u root --skip-password
    
Mysql5.7忘記root密碼及mysql5.7修改root密碼的方法的相關資料
a、關閉正在運行的 MySQL :service mysql stop
b、運行:mysqld_safe --skip-grant-tables &
c、爲了安全能夠這樣禁止遠程鏈接:mysqld_safe --skip-grant-tables --skip-networking &    
d、使用mysql鏈接server:mysql -p
e、更改密碼:
mysql> update mysql.user set authentication_string=password('wclwcw') where user='root' and Host = 'localhost';     

特別提醒注意的一點是,新版的mysql數據庫下的user表中已經沒有Password字段了
而是將加密後的用戶密碼存儲於authentication_string字段

四、分別登陸修改主機一、主機2的mysql密碼
輸入mysql回車,進入mysql環境
set password = password ('wclwcw');
flush privileges;

五、在主機1(192.168.1.2)的/etc/my.cnf中配置如下內容
[mysqld]
datadir=/home/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id = 1
auto-increment-increment = 2
auto-increment-offset = 1

explicit_defaults_for_timestamp = 1
character_set_server=utf8
interactive_timeout = 57600
log-bin = mysql-bin

expire-logs-days = 100
replicate-do-db  = wang                 #須要同步的數據庫
binlog-ignore-db  = mysql
binlog-ignore-db  = information_schema

slave-skip-errors=all
log-slave-updates
symbolic-links=0
skip-name-resolve

六、在主機1(192.168.1.4)的/etc/my.cnf中配置如下內容
[mysqld]
datadir=/home/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id = 2
auto-increment-increment = 2
auto-increment-offset = 2

character_set_server=utf8
interactive_timeout = 57600
log-bin = mysql-bin
expire-logs-days = 100

replicate-do-db = wang                   #須要同步的數據庫
binlog-ignore-db = mysql
binlog-ignore-db = information_schema

slave-skip-errors=all
log-slave-updates
symbolic-links=0
skip-name-resolve

備註:主機1和主機2都只有server-id不一樣和 auto-increment- offset不一樣
auto-increment-offset是用來設定數據庫中自動增加的起點的,回爲這兩能
服務器都設定了一次自動增加值2,因此它們的起點必須得不一樣,這樣才能避
免兩臺服務器數據同步時出現主鍵衝突replicate-do-db 指定同步的數據庫,
咱們只在兩臺服務器間同步wang數據庫
另:auto-increment-increment的值應設爲整個結構中服務器的總數,本案例用到兩臺服務器,因此值設爲2mysql


七、分別重啓主機1,主機2的mysql服務,使配置生效
service mysqld restart

八、在主機1(192.168.1.2)上建立數據庫和表並插入數據
mysql -uroot -pwclwcw
create database wang;
use wang;
create table wclwcw(id int,name varchar(100));
insert into wclwcw value (1,'tom')

九、相互受權
在主機1(192.168.1.2)上
GRANT REPLICATION SLAVE ON *.* TO 'wang'@'192.168.1.4' IDENTIFIED BY 'wclwcw'
flush privileges;

在主機2(192.168.1.4)上
GRANT REPLICATION SLAVE ON *.* TO 'wang'@'192.168.1.2' IDENTIFIED BY 'wclwcw'    
flush privileges;

十、互告bin-log信息
a、在主機1(192.168.1.2)上
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000006 |      106 |      | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

b、在主機2(192.168.1.4)上
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000008 |      192 |      | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

c、在主機1(192.168.1.2)上
mysql> change master to master_host='192.168.1.4',master_user='wang',master_password='wclwcw',master_log_file='mysql-bin.000008',master_log_pos=194;

d、在主機2(192.168.1.4)上
mysql> change master to master_host='192.168.1.2',master_user='wang',master_password='wclwcw',master_log_file='mysql-bin.000007',master_log_pos=1082;

十一、在主機一、主機2兩服務器都執行如下命令
mysql> start slave;

十二、查看狀態
主機1上(192.168.1.2)
mysql> show slave status\G
Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.4
                  Master_User: zz
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 778
               Relay_Log_File: template-relay-bin.000002
                Relay_Log_Pos: 780
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: wang
          Replicate_Ignore_DB:
           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: 778
              Relay_Log_Space: 990
              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
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 2
                  Master_UUID: e6a7bb8f-4fe6-11e6-abd0-fa163e5cb863
             Master_Info_File: /home/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:

在主機2(192.168.1.4)上
mysql> show slave status\G;
      Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.2
                  Master_User: zz
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 769
               Relay_Log_File: template-relay-bin.000002
                Relay_Log_Pos: 484
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: wang
          Replicate_Ignore_DB:
           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: 769
              Relay_Log_Space: 694
              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
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 10824007-4fe4-11e6-a4ff-fa163ea94ff8
             Master_Info_File: /home/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
                     
當看到了兩個yes,即:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
說明已經配置成功了,同時查看主機2中是否已經有主機1中的數據庫

其餘
同步數據
用test作的實驗,導出將test.sql文件從1.4服務器拷貝到1.2服務器
備份數據前先鎖表,保證數據一致性
mysql> FLUSH TABLES WITH READ LOCK;
# mysqldump -uroot -p123456 test> /tmp/test.sql;

mysql> UNLOCK TABLES;

scp /tmp/test.sql root@192.168.1.2:/tmp

在搭建mysql master-slave複製環境時,一切都正常,可是在slave上執行:show slave status\G; 時,出現下面的情況:
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes

一直沒法進行正確的複製。查看錯誤日誌文件顯示:Connecting error_code:2003
看來是slave 沒有鏈接上master,在網上搜索一下,網上文章大都只是指出了可能的三種錯誤:
1. 網絡不通
2. 密碼不對
3. pos不對

我測試網絡是能夠ping通的。密碼也對,pos也對。

後來想了好久,忽然想起來了防火牆好像沒有關閉!一檢查果真。
因此第四種錯誤是:
4. 防火牆沒有關閉。

相關命令以下:
關閉命令:  service iptables stop
永久關閉防火牆:chkconfig iptables off

兩個命令同時運行,運行完成後查看防火牆關閉狀態
service iptables status

另外順便也將 selinux關閉掉:
1      vi /etc/selinux/config
2      #SELINUX=enforcing     #註釋掉
3      #SELINUXTYPE=targeted  #註釋掉
4      SELINUX=disabled  #增長
5      :wq  #保存,關閉。
6      shutdown -r now   #重啓系統

查看SELinux的狀態:
getenforce linux

相關文章
相關標籤/搜索