MySQL主從數據庫配置

使用工具

MySQL數據版本:5.6.36-log、
兩臺雲服務器(Linux系統)html

首先,須要在Linux系統下安裝MySQL,具體步驟能夠參考這裏,而且確保兩臺主機能夠相互訪問,能夠直接ping一下。mysql

配置Master

  • Linux環境下,MySQL的配置文件在/ect/my.cnf,直接打開並編輯該文件:vim /etc/my.cnf
  • [mysqld]下輸入配置
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-do-db=rwtest

這裏的server-id用於標識惟一的數據庫,這裏設置爲2在設從庫須要設置爲其餘值sql

binlog-ignore-db:表示同步的時候ignore的數據庫
binlog-do-db:指定須要同步的數據庫數據庫

  • 重啓MySQLservice mysqld restart
  • 登陸MySQL並對訪問用戶進行受權:Slave機器須要File權限和REPLICATION SLAVE權限,固然也能夠授予所有權限。
grant file on *.* to 'root'@'%' identified by 'your password';

grant replication slave on *.* to 'root'@'%' identified by 'your password';

flush privileges;

or

grant all privileges on *.* to 'root'@'%' identified by 'your password';

flush privileges;
  • 完成後,能夠查看下Master的狀態:show master status
mysql> show master status;
+------------------+----------+--------------+----------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                 | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
| mysql-bin.000001 |     2272 | rwtest       | information_schema,mysql |                   |
+------------------+----------+--------------+----------------------------------+-------------------+
1 row in set (0.03 sec)

這裏的FilePositionSlave中都要用到,Binlog_Do_DB指須要同步的數據庫,Binlog_Ignore_DB指不須要同步的數據庫,就是剛纔配置的值。vim

配置Slave

  • 一樣在[mysqld]下對配置文件進行編寫
log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
replicate-do-db=rwtest
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
  • 配置完畢後一樣重啓一下MySQL,並進入設置其對應的Master
mysql> stop slave;  #關閉Slave
mysql> change master to master_host='Master主機IP',master_user='剛纔受權的用戶',master_password='your password',master_log_file='mysql-bin.000001', master_log_pos=2272;

mysql> start slave;  #開啓Slave

master_log_filemaster_log_pos都是Master的狀態值。必須對應服務器

  • 登陸slave的數據庫,查看slave的狀態:show slave status \G
Slave_IO_State: Waiting for master to send event
                  Master_Host: 你的Master主機IP
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 2272
               Relay_Log_File: izwz9fcvpu481xh55cegx0z-relay-bin.000002
                Relay_Log_Pos: 1339
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: rwtest
          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: 2272
              Relay_Log_Space: 1530
              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: de20814f-2fb8-11e7-8f61-5254002b91a1
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.03 sec)

若是狀態中含有沒有報Error,則表示配置成功了。ide

接下來你經過可視化工具鏈接兩個數據庫,在主庫中進行增刪改操做,從庫中也會有相應的操做,而在從庫中進行此類操做對主庫無效。工具

相關文章
相關標籤/搜索