MySQL主從讀寫分離專題

主機A:192.168.1.101
從機B:192.168.1.102mysql

一、先登陸主機 A
mysql>GRANT REPLICATION SLAVE ON *.* TO slave_user@192.168.1.101 IDENTIFIED BY 'admin888';
賦予從機權限,有多臺叢機,就執行屢次.
二、 打開主機A的my.cnf,輸入
server-id               = 1    #主機標示,整數
log_bin                 = /var/log/mysql/mysql-bin.log   #確保此文件可寫
read-only              =0  #主機,讀寫均可以
binlog-do-db         =test   #須要備份數據,多個寫多行
binlog-ignore-db    =mysql #不須要備份的數據庫,多個寫多行
三、打開從機B的my.cnf,輸入
server-id               = 2
log_bin                 = /var/log/mysql/mysql-bin.log
(master-host     =192.168.1.101(注意:MySQL5.1之後master-*形式廢棄了,採用change master to)
master-user     =backup
master-pass     =123456
master-port     =3306)sql

mysql> change master to master_host='192.168.1.101',master_user='master_name',ma
ster_password='admin888',master_port=3306,master_log_file='mysql-bin.000001',mas
ter_log_pos=107;

(master_log_file和master_log_pos)的值你能夠在服務器上運行 show master status; 來獲得。
master-connect-retry=60 #若是從服務器發現主服務器斷掉,從新鏈接的時間差(秒)
replicate-do-db =test #只複製某個庫
replicate-ignore-db=mysql #不復制某個庫
四、同步數據庫
不用太費事,只把主從庫都啓動便可自動同步,若是不嫌麻煩的話能夠把主庫的內容導出成SQL,而後在從庫中運行一遍
五、先重啓主機A的mysql,再重啓從機B的mysql
六、驗證
在主機A中,mysql>show master status\G;
在從機B中,mysql>show slave status\G;
能看到大體這些內容
File: mysql-bin.000001
Position: 1374
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
能夠在主機A中,作一些INSERT, UPDATE, DELETE 操做,看看主機B中,是否已經被修改。數據庫

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.1.100
                  Master_User: master_name
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: Y9EMERNXJ2AZ4L2-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: slave_user
          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: 107
              Relay_Log_Space: 107
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1130
                Last_IO_Error: error connecting to master 'master_name@192.168.1
.100:3306' - retry-time: 60  retries: 86400
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
1 row in set (0.00 sec)

以上問題是:connecting... 待解決。服務器

相關文章
相關標籤/搜索