原理:利用mysql二進位日誌文件,實現數據同步 步驟: 一、配置主庫(Master) 1. 修改主庫my.cnf配置文件,加入如下配置 [root@localhost ~]# nano -w /etc/my.cnf [mysqld] log-bin=mysql-bin server-id=1 2. ...
原理:利用mysql二進位日誌文件,實現數據同步 步驟: 一、配置主庫(Master) 1. 修改主庫my.cnf配置文件,加入如下配置 [root@localhost ~]# nano -w /etc/my.cnf [mysqld] log-bin=mysql-bin server-id=1 2. 重啟mysql,使配置生效 [root@localhost ~]# service mysqld restart 3. 以root登錄mysql,創建主從複製用戶並授權主從複製 [root@localhost ~]# mysql -uroot -p mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'repl_password'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; mysql> FLUSH PRIVILEGES; 4. 查看主庫二進位日誌坐標(記住File和Position列對應的值) mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 747 | | | +------------------+----------+--------------+------------------+
二、配置從庫(Slave) 1. 修改從庫my.cnf配置文件,加入如下配置 [root@localhost ~]# nano -w /etc/my.cnf [mysqld] server-id=2 2. 修改MySQL server UUID(可選,如果是直接克隆虛擬機,則這步必須做) mv /var/lib/mysql/auto.cnf /var/lib/mysql/auto.cnf.bak 3. 重啟mysql,使配置生效並生成新的MySQL server UUID [root@localhost ~]# service mysqld restart 4. 以root登錄mysql,並設置從庫訪問主庫二進位日誌文件 [root@localhost ~]# mysql -uroot -p
mysql> CHANGE MASTER TO -> MASTER_HOST='master_host_name', -> MASTER_USER='repl', -> MASTER_PASSWORD='repl_password', -> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_LOG_POS=747;5. 啟動Slave mysql> start slave; 6. 查看Slave連接狀態(Slave_IO_Running、Slave_SQL_Running都為Yes則表示連接成功) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.168.110 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 747 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes