MySQL主從複製環境可以說是一切高可用的基礎。它的原理也比較簡單,下面我們先來了解下主從複製的原理:
#雖然圖上一共有7步,可以簡化一下幫助記憶和理解:
Master上進行改、寫入操作;
MySQL把修改資料寫進binlog;
ip | #主從 | |||
---|---|---|---|---|
阿里雲 | 192.168.1.100 | 3306 | MySQL5.7.14 | Master |
3306
[mysqld] log-bin=mysql-bin server-id=1003306
/etc/init.d/mysql restart
mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slave; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.200';
#防止DDL、写操作 mysql>FLUSH TABLES WITH READ LOCK; shell>mysqldump -uroot -p --single-transaction --master-data=2 -A>back.sql
root@localhost [mysql]>show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000045 | 939 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
mysql> UNLOCK TABLES;
rsync back.sql root@192.168.1.200:/root
[mysqld] log-bin=mysql-bin server-id=2003306
shell>mysqldump -uroot -p --databases t1 <back.sql><em>指向Master</em><em>這裡的</em>MASTER_LOG_FILE<em>和</em>MASTER_LOG_POS<em>是剛才</em>show master status的值,當然也可以使用<h3>more</h3>來查看應該指定的位置。 <p></p> <pre class="brush:php;toolbar:false">CHANGE MASTER TO MASTER_HOST='192.168.1.200',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000045',MASTER_LOG_POS=939;
MASTER_LOG_POS
是剛才###show master status###的值,當然也可以使用###more###查看應該指定的位置。 ###shell>more back.sql -- MySQL dump 10.13 Distrib 5.7.14, for linux-glibc2.5 (x86_64) -- -- Host: localhost Database: -- ------------------------------------------------------ -- Server version 5.7.14-log /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Position to start replication or point-in-time recovery from -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000045', MASTER_LOG_POS=939;
mysql>start slave; mysql>show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.200 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000045 Read_Master_Log_Pos: 939 Relay_Log_File: relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000045 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: 939 Relay_Log_Space: 154 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: 23306 Master_UUID: 9a13d860-b55b-11e6-bf33-00163e054164 Master_Info_File: /data/mysql/mysql3306/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: 3feb36dc-ef7e-11e6-a535-52540043f116:1-337886 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
以上是教你如何在阿里雲與騰訊雲輕鬆搭建傳統主從複製環境教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!