文章目錄
- MySQL完成主從復制教程
- 準備:
- 原理:
- 步驟:
- 推薦文章
MySQL完成主從復制教程
主從復制(也稱 AB 復制)就是將一個服務器(主服務器)的數據復制到一個或多個MySQL數據庫服務器(從服務器)中。
可以根據配置,指定復制哪個庫哪個表數據。
可以想到復制是通過異步的。
準備:
2臺版本一致的服務器數據庫,一臺作為主庫、一臺作為從庫
(我的兩個數據庫版本:5.7)
一臺服務器搭建2個數據庫實例教程:http://t.csdnimg.cn/aaErM
原理:
主庫一旦變更數據,就會寫入二進制日志文件(Binary log),從庫IO線程( I/O thread)連接到主庫,讀取二進制日志文件內容并寫入自己的中繼日志文件(Realy log),然后從庫 SQL thread 定時檢查中繼日志 (Realy log),發現有更新的內容就自己的庫執行一遍。
從服務器都會copy主服務器二進制日志的全部內容到副本,然后從服務器設備負責決定應該執行副本中的哪些語句。
步驟:
-
主庫配置my.cnf文件指定唯一server-id
[mysqld] ## 同一局域網內注意要唯一 server-id=100 ## 開啟二進制日志功能,可以隨便取(關鍵) log-bin=mysql-bin ## 復制過濾:不需要備份的數據庫,不輸出(mysql庫一般不同步) binlog-ignore-db=mysql ## 為每個session 分配的內存,在事務過程中用來存儲二進制日志的緩存 binlog_cache_size=1M ## 主從復制的格式(mixed,statement,row,默認格式是statement) binlog_format=mixed
-
從庫配置my.cnf文件指定唯一server-id
[mysqld] ## 設置server_id,注意要唯一 server-id=102 ## 開啟二進制日志功能,以備Slave作為其它Slave的Master時使用 log-bin=mysql-slave-bin ## relay_log配置中繼日志 relay_log=edu-mysql-relay-bin ##復制過濾:不需要備份的數據庫,不輸出(mysql庫一般不同步) binlog-ignore-db=mysql ## 如果需要同步函數或者存儲過程 log_bin_trust_function_creators=true ## 為每個session 分配的內存,在事務過程中用來存儲二進制日志的緩存 binlog_cache_size=1M ## 主從復制的格式(mixed,statement,row,默認格式是statement) binlog_format=mixed ## 跳過主從復制中遇到的所有錯誤或指定類型的錯誤,避免slave端復制中斷。 ## 如:1062錯誤是指一些主鍵重復,1032錯誤是因為主從數據庫數據不一致 slave_skip_errors=1062
-
重啟3306、3307服務,使配置生效
# 查看mysql 3306、3307進程 root@songdanminserver:[/usr/local/mysql/3306/run]ps -ef | grep mysqld root 8568 25795 0 13:40 pts/0 00:00:00 grep --color=auto mysqld mysql 30201 1 0 Nov10 ? 00:07:13 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/3306/my.cnf --user=mysql mysql 30473 1 0 Nov10 ? 00:07:46 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/3307/my.cnf --user=mysql # 停止3306、3307進程服務 root@songdanminserver:[/usr/local/mysql/3306/run]kill 30201 root@songdanminserver:[/usr/local/mysql/3306/run]kill 30473 # 查看mysql進程 root@songdanminserver:[/usr/local/mysql/3306/run]ps -ef | grep mysqld root 8614 25795 0 13:41 pts/0 00:00:00 grep --color=auto mysqld # 查看端口占用情況是否沒有3306、3307 root@songdanminserver:[/usr/local/mysql/3306/run]netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp6 0 0 :::111 :::* LISTEN tcp6 0 0 :::8080 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN tcp6 0 0 127.0.0.1:8005 :::* LISTEN # 重新啟動3306、3307 root@songdanminserver:[/usr/local/mysql/3306]nohup /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/3306/my.cnf --user=mysql & [1] 9413 root@songdanminserver:[/usr/local/mysql/3306]nohup: ignoring input and appending output to ‘nohup.out’ ^C root@songdanminserver:[/usr/local/mysql/3307]nohup /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/3307/my.cnf --user=mysql & [2] 9457 root@songdanminserver:[/usr/local/mysql/3307]nohup: ignoring input and appending output to ‘nohup.out’ ^C root@songdanminserver:[/usr/local/mysql/3306]# 查看mysql 3306、3307進程 root@songdanminserver:[/usr/local/mysql/3307]ps -ef | grep mysqld mysql 9413 25795 0 13:52 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/3306/my.cnf --user=mysql mysql 9559 25795 1 13:54 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/3307/my.cnf --user=mysql root 9608 25795 0 13:54 pts/0 00:00:00 grep --color=auto mysqld
-
登錄主庫,授予從庫連接主庫,并復制主庫數據的權限,刷新權限生效
# 登錄主庫3306 root@songdanminserver:[/usr/local/mysql/3306]mysql -uroot -h127.0.0.1 -p -P3306 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.36-log MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.# 授予從庫3307的root用戶在指定從庫ip上從主庫復制所有庫、所有表數據的權限 mysql> grant replication slave, replication client on *.* to 'root'@'你的從庫服務器ip' identified by '你的從庫root用戶密碼'; Query OK, 0 rows affected, 1 warning (0.00 sec)# 刷新權限 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)# 查看MySQL現在有哪些用戶及對應的IP權限,可以看到你剛授權的從庫user,host mysql> select user,host from mysql.user; +---------------+--------------+ | user | host | +---------------+--------------+ | root | % | | root | 121.41.59.91 | | mysql.session | localhost | | mysql.sys | localhost | +---------------+--------------+ 4 rows in set (0.00 sec)# 查看主庫3306的binlog文件名和位置 mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000003 | 612 | | mysql | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
-
登錄從庫,連接主庫,指定主庫ip、主庫為從庫連接所創建的用戶、密碼、從庫從主庫哪個二進制文件的哪里開始讀取數據等
# 登錄從庫3307 root@songdanminserver:[/usr/local/mysql/3306]mysql -uroot -h127.0.0.1 -p -P3307 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.36-log MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.# 配置連接主庫3306 # change master to master_host='主庫服務器ip', master_user='root(上一步主庫授權從庫能進行復制的用戶)', master_password='123456(上一步主庫授權從庫能進行復制的密碼)', master_port=3306(主庫端口), master_log_file='mysql-bin.000002(上一步查看到的主庫日志文件名稱)',master_log_pos=2079(上一步查看到的主庫日志文件位置); mysql> change master to master_host='121.41.53.91', master_user='root', master_password='123456', master_port=3306, master_log_file='mysql-bin.000003',master_log_pos=612; Query OK, 0 rows affected, 1 warning (0.02 sec)
-
從庫啟動主從復制,查看是否連接主庫成功
# 啟動從庫復制主庫 mysql> start slave; Query OK, 0 rows affected (0.00 sec)# 查看主從復制狀態,查看Slave_IO_Running、Slave_SQL_Running 是否為yes mysql> show slave status\G; *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 121.41.53.91 # 綁定的主庫ipMaster_User: root # 主庫用戶Master_Port: 3306 # 主庫端口Connect_Retry: 60 Master_Log_File: mysql-bin.000003 # 主庫同步日志,從這讀取主庫數據Read_Master_Log_Pos: 612Relay_Log_File: edu-mysql-relay-bin.000002 # 從庫中繼日志Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: Yes # 這里是yes 說明復制成功Slave_SQL_Running: Yes # 這里是yes 說明復制成功Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 612Relay_Log_Space: 531Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 100Master_UUID: cfb53930-7fa2-11ee-8ea9-00163e2859d2Master_Info_File: /usr/local/mysql/3307/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)# 停止從庫復制主庫 mysql> stop slave; Query OK, 0 rows affected (0.01 sec)
-
測試主從復制是否成功,在主庫建表,插入數據,查看從庫是否也有
在主庫執行操作:
- 新增庫
- 新增表
- 新增數據
- 修改數據
- 刪除數據
查看從庫3307是否發生變化
# 進入主庫3306 root@songdanminserver:[/usr/local/mysql/3306]mysql -uroot -h127.0.0.1 -p -P3306 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 5.7.36-log MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. # 新建數據庫 my_test mysql> create database my_test; Query OK, 1 row affected (0.00 sec)mysql> use my_test; Database changed mysql> CREATE TABLE test (-> id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,-> name VARCHAR(30) NOT NULL,-> email VARCHAR(50) NOT NULL,-> reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP-> ); Query OK, 0 rows affected (0.01 sec) # 插入初識數據 mysql> INSERT INTO test (name, email)-> VALUES ('John Doe', 'john@example.com'); Query OK, 1 row affected (0.00 sec)# 查看從庫變化
?
注意:如果出現從庫復制失敗沒有效果時候,從庫重新綁定主庫(執行6)
造成這類問題的原因一般是在主從復制的時候,基于創建表,然后又去刪除和操作了數據表或者表。
推薦文章
【史上最細教程】一臺服務器上搭建2個MySQL實例