MySQL5.6主從複製(讀寫分離)教程(一)

第1步:修改主數據庫的配置文件/etc/my.cnf 

[mysqld]


server-id=1

log-bin=mysqlmaster-bin.log


#當每進行1次事務提交之後,MySQL將進行一次fsync之類的磁盤同步指令來將binlog_cache中的數據強制寫入磁盤

sync_binlog=1


#緩存innodb表的索引,數據,插入數據時的緩衝, 操作系統內存的70%-80%最佳

innodb_buffer_pool_size=2G

#在每個事務提交時,日誌緩衝被寫到日誌文件,對日誌文件做到磁盤操作的刷新

innodb_flush_log_at_trx_commit=1


sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


#lower_case_table_names=1

log_bin_trust_function_creators=1


第2步:修改從數據庫配置文件/etc/my.cnf

[mysqld]

server-id=2

log-bin=mysqlslave-bin.log

sync_binlog=1


innodb_buffer_pool_size=2G

innodb_flush_log_at_trx_commit=1


sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


#lower_case_table_names=1

log_bin_trust_function_creators=1


第3步:在主數據庫和從數據庫服務器上分別執行以下命令重新啓動主數據庫和從數據庫

[root@master ~]# service mysql restart

[root@slave ~]# service mysql restart


第4步:在主數據庫上創建用於主從複製的賬戶

[root@master ~]# mysql -uroot -p

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.101' IDENTIFIED BY '111111';

Query OK, 0 rows affected (0.00 sec)


注意:以上命令中的IP地址,是從數據庫服務器的IP地址。


第5步:主數據庫鎖表(禁止再插入數據以獲取主數據庫的的二進制日誌座標)


mysql> flush tables with read lock;


Query OK, 0 rows affected (0.00 sec)


第6步:查看主數據庫的狀態(並記錄下File字段和Position字段的值,在配置從服務器時有用到)


mysql> show master status;


+------------------------+----------+--------------+------------------+-------------------+


| File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |


+------------------------+----------+--------------+------------------+-------------------+


| mysqlmaster-bin.000004 |      327 |              |                  |                  |


+------------------------+----------+--------------+------------------+-------------------+


1 row in set (0.00 sec)


第7步:創建主數據庫的快照文件


[root@master ~]# cd /usr/bin/


# ./mysqldump -uroot -p -h127.0.0.1 -P3306 --all-databases --triggers --routines --events >>/mnt/windows/all.sql


上面命令中的紅色部分,是一個共享目錄,這個目錄可以同時被主數據庫服務器和從數據庫服務器訪問到。


如果沒有這樣的共享目錄,可以將all.sql放在其它任何目錄下,然後使用scp命令複製到遠程從數據庫服務器的某個目錄中


這條命令的執行時間根據數據量的不同,會有所不同,如果主數據庫的數據量很大,可能需要很長時間,那麼在這種情況下,就最好在晚上沒有業務的時候進行這個操作,否則第七步中的鎖表操作會對業務系統造成很大的影響


第8步:解鎖主數據庫的鎖表操作


[root@master ~]# mysql -uroot -p    (本命令在主數據庫服務器上執行)


mysql> unlock tables;


Query OK, 0 rows affected (0.00 sec)


第9步:在從數據庫服務器上導入快照文件到從數據庫中


[root@slave ~]# mysql -uroot -p -h127.0.0.1 -P3306 < /mnt/all.sql


第10步:在從數據庫服務器上設置主數據庫服務器向從數據庫服務器同步


[root@slave ~]# mysql -uroot -p


mysql> change master to master_host = '192.168.1.100',master_user='repl',master_password='111111',master_log_file='mysqlmaster-bin.000004',master_log_pos=327;


注意:紅色部分的值,是在第6步中查出來的,這裏不能弄錯了


第11步:啓動從數據庫複製線程


mysql> start slave;


Query OK, 0 rows affected (0.01 sec)


第12步:查詢從數據庫的複製線程狀態


mysql> show slave status \G

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.10.22

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysqlmaster-bin.000001

          Read_Master_Log_Pos: 920

               Relay_Log_File: lingyou2-relay-bin.000002

                Relay_Log_Pos: 887

        Relay_Master_Log_File: mysqlmaster-bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

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

              Relay_Log_Space: 1063

              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: b3825c9e-929c-11e5-898f-44a8422778a9

             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

如果Slave_IO_Running和Slave_SQL_Running兩項都爲yes,就表示主從複製配置成功了.


下面可以開始測試配置是否成功了,首先在主數據庫的test數據庫中新建一張表,然後插入幾條數據,然後到從數據庫看看是否同步過來了。


注意:當從數據庫有大量的查詢時,可以暫時將從數據庫的複製線程關閉掉,等查詢量降下來了,再打開,這樣也不會丟失數據。


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