mysql 主從搭建

step 1:

假設主服地址爲192.168.1.1,從服爲192.168.1.2mysql

主服啓用binlog,並設置server-idsql

[mysqld]
log-bin=mysql-bin
server-id=1

step 2:

主服建立同步帳戶並設置其權限:shell

CREATE USER 'replication'@'192.168.1.2' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.1.2';

step 3:

從服設置配置:segmentfault

[mysqld]
server-id=2

step 4:

主服查看bin-log信息服務器

show master status;
+------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 111| | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)

從服獲取主服master的position 和 binlog文件,設置masterspa

CHANGE MASTER TO MASTER_HOST='192.168.1.1', MASTER_USER='replication', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=111;

step 5:

啓動從服slave:線程

START SLAVE;

查看slave狀態:日誌

show slave status\G;
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.252.123 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 629 Relay_Log_File: master2-relay-bin.000003 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes ......

檢查主從複製通訊狀態code

Slave_IO_State #從站的當前狀態 
Slave_IO_Running: Yes #讀取主程序二進制日誌的I/O線程是否正在運行 
Slave_SQL_Running: Yes #執行讀取主服務器中二進制日誌事件的SQL線程是否正在運行。與I/O線程同樣 
Seconds_Behind_Master #是否爲0,0就是已經同步了server

 

參考:

http://www.javashuo.com/article/p-mytcqyco-k.html

相關文章
相關標籤/搜索