linux中mysql主從同步

主服務器:10.236.51.151

從服務器:10.236.51.152

 

 

安裝mysql

yum -y install mysql-devel mysql-server 

運行安全設置嚮導

mysql_secure_installation

 

主服務器

cp /usr/share/mysql/my-large.cnf /etc/my.cnf    (my-large是一個註釋了很多配置的文件)

修改master端的/etc/my.cnf文件

server_id = 1 (1表示master,2表示slave)
binlog-do-db = test(test表示要同步的數據庫),如果有多個數據庫,每個數據庫一行
binlog-ignore-db= mysql    設置不需要同步的數據庫,每個數據庫一行 (一般這條可以不寫)(若該2條都不寫,就代表同步全部)
log-bin = mysql-bin

 

進入mysql(不要忘記;)

創建一個數據庫test
create database test;
創建同步用戶

在主服務器上爲從服務器建立一個連接帳戶,該帳戶必須授予REPLICAITON SLAVE權限。
mysql> grant replication slave on *.* to 'replication'@'10.236.51.152' identified by 'password';

mysql> flush privileges;
重啓mysql

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show master status\G

*************************** 1. row ***************************

            File: mysql-bin.000002(這個在slave的配置中要用到)

        Position: 329(這個在slave的配置中要用到)

    Binlog_Do_DB: test

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

 

mysql> unlock tables;

注:這裏鎖表的目的是爲了生產環境中不讓進新的數據,好讓從服務器定位同步位置。初次同步完成後,記得解鎖。

 

從服務器

cp /usr/share/mysql/my-large.cnf /etc/my.cnf

修改master端的/etc/my.cnf文件

[mysqld]  

server-id = 2

log-bin = mysql-bin

replicate-do-db = test

replicate-ignore-db = mysql,information_schema

重啓mysql

change master語句指定同步位置

 

mysql> change master to master_host='10.236.51.151', master_user='replication', master_password='password', master_log_file='mysql-bin.000002', master_log_pos=0;

ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first

mysql> stop slave

Query OK, 0 rows affected (0.00 sec)

 

mysql> change master to master_host='10.236.51.151', master_user='replication', master_password='password', master_log_file='mysql-bin.000002', master_log_pos=329;

Query OK, 0 rows affected (0.01 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: 10.236.51.151

                  Master_User: replication

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 329

               Relay_Log_File: mysqld-relay-bin.000002

                Relay_Log_Pos: 474

        Relay_Master_Log_File: mysql-bin.000002

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: test

          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: 329

              Relay_Log_Space: 630

              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: 


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