配置mysql主從復制


1. 先准備兩台服務器,可以一台本機,一台使用虛擬機

本機IP:192.168.132.1 主服務器
虛擬機IP:192.168.111.128 從服務器
注意,主從服務器上的mysql版本要一致,不然可能會有不兼容問題。

2. 在主機創建一個賬號,用於從機復制主機數據。

// *.* 所有數據庫的所有表
// repl 賬號
// 123456 密碼
//@'192.168.111.128' 該賬號只允許這個ip使用
mysql > grant replication slave on *.* to 'repl'@'192.168.111.128' identified by '123456';

// 使創建的賬號生效
mysql > flush privileges;

3. 編輯主機my.cnf文件

vi /etc/my.cnf

// my.cnf 文件 新增配置。
[mysqld]
server-id=1 #設置server-id,值無所無,只要唯一就行。
log-bin=mysql-bin # 啟用二進制日志
binlog-do-db=myblog # 需要備份的數據庫,可以寫多條
log-slave-updates=1 # 這個參數一定要加上,否則不會更新記錄到二進制文件里。
slave-skip-errors=1 #跳過錯誤,繼續執行賦值操作(可選

4. 查看主機狀態

mysql > show master status;
Empty set (0.00 sec)
mysql > show binary logs;
ERROR 1381 (HY000): You are not using binary logging

5. 重啟mysql數據庫

service mysqld start

6. 設置讀鎖,這里是為了下下面備份數據的時候,沒有遺漏的數據。備份完以后就可以解鎖了。

mysql > flush tables with read lock;

7. 得到binlog日志文件名和偏移量(此處記住File名稱和Position值,后面slave服務器配置時需要用到

mysql > show master status;
+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+

| mysql-bin.000001 | 713 | | |
+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

8. 備份要同步的數據庫

// 在主服務器執行
mysqldump -uroot -proot test > test.sql

9. 解鎖

mysql > unlock tables;

10. 將剛才從master備份的數據庫導入slave從服務器上。

// 在從服務器執行
mysqldump -uroot -proot test < test.sql;

11. 編輯slave從服務器my.cnf文件。

vi /etc/my.cnf

# 新增
server-id=109 # 值無所謂,唯一就行。

12. 重啟從數據庫

service mysqld restart

13. 對從服務器進行相應的設置,此處要注意logfile的名稱和position的值,其余host,user,password為主服務器的ip,賬號和密碼。

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to
   -> master_host='192.168.132.1',
   -> master_user='repl',
   -> master_password='123456',
   -> master_log_file='mysql-bin.000001',
   -> master_log_pos=713;

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.132.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1079
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-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: 1079
Relay_Log_Space: 407
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:
1 row in set (0.00 sec)

ERROR:
No query specified

這里主要看 :這兩個值必須為Yes,為其他值就代表沒有連接成功,需要重新設置。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

如果Slave_IO_Running:Connection 可能是網絡問題,pos問題,賬號問題。
如果Slave_SQL_Runing:NO 這里LAST_SQL_ERROR會有相應的錯誤信息。

14. 測試

上述配置成功后可以查看master和slave上的線程狀態,在master上,你可以看到slave的I/O線程的鏈接。
在master上輸入

mysql > show processlist\G;
*************************** 1. row ***************************
Id: 4
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 19
User: repl
Host: 192.168.111.128:42337
db: NULL
Command: Binlog Dump
Time: 183
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
2 rows in set (0.00 sec)

ERROR:
No query specified

復制代碼

15. 在主服務器master上新增一條數據,到從服務器slave上查看是否同步,如同步,則代表從服務器正常工作。

16. 故障解決

  1. 當出現slave_sql_runiing:no的時候,代表有sql異常,可以使用忽略異常的方式解決。
mysql > stop slave; // 停止從庫復制行為
mysql > set global sql_slave_skip_counter=1 // 從庫sql指針想下移動一步,這樣就會忽略該步點的錯誤sql

// sql_slave_skip_counter=val, val可以是任意數字,代表忽略更新的sql條數。

// 這種方式會導致主從庫可能會不一致。對於要求主從庫必須一致的情況,就必須要重新導入叢庫數據了。
  1. mysql日志中:Failed to open the relay log ‘./mysqld-relay-bin.000007’
1. 錯誤原因:mysqld-relay-bin.000007文件丟失。
2. 解決方法:重置slave,在mysql中執行下面命令
mysql > reset slave;
3. 上面的命令會清除記錄的master_log_file和master_log_pos參數,所以需要重置這兩個參數,命令如下
masql > change master to
master_log_file='mysql-bin.000005',
master_log_pos=316;
4. 重啟slave
mysql > start slave;
5. 查看slave狀態
msql > show slave status\G;

// 這兩個參數是這樣的就ok了。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
  1. 字符編碼錯誤: Error ‘Character set ‘#28’ is not a compiled character set and is not specified in the ‘/phpstudy/mysql/share/charsets/Index.xml’ file’ on query.
1. #28是mysql的gbk_chinese_ci編碼
2. 原因是主機mysql的character_set_results='gbk',而從機(slave)中沒有gbk字符集。

查看主機(master)設置字符集:
mysql > show variables like '%char%';

+--------------------------+------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------+

| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:\phpStudy2\MySQL\share\charsets\ |
+--------------------------+------------------------------------+


查看從機(slave)設置字符集
mysql > show variables like '%char%';

+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+

| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /phpstudy/mysql/share/charsets/ |
+--------------------------+---------------------------------+


查看主機(master)所有字符集

msql > show character set;
+----------+-----------------------------+---------------------+--------+

| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+

| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
+----------+-----------------------------+---------------------+--------+


查看從機所(slave)有字符集:
mysql > show character set;

+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+

| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
+----------+-----------------------------+---------------------+--------+



從以上不難看出,主機(master)有gbk字符集並且character_set_results='gbk',而從機上沒有gbk字符集,所以報編碼錯誤。

解決方式,設置主機character_set_results='utf8',語句如下:
mysql > set character_set_results='utf8';

補充:character_set_results,character_set_client,character_set_connection 這三個值是無法再my.ini中配置的
或者說,在my.ini中的配置會被客戶端給覆蓋掉。
character_set_results: 客戶端結果集編碼
character_set_client: 客戶端編碼
character_set_connection: 數據庫鏈接編碼
同一個數據庫在不同的客戶端打開這三個編碼可能不一樣:如:
dos下連接:

mysql > show variables like '%char%';
+--------------------------+------------------------------------+

| Variable_name | Value |
+--------------------------+------------------------------------+

| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:\phpStudy2\MySQL\share\charsets\ |
+--------------------------+------------------------------------+


phpmyadmin下連接:
show valiables like '%char%';

Variable_name Value
character_
set_client utf8
character_
set_connection utf8
character_
set_database utf8
character_
set_filesystem binary
character_
set_results utf8
character_
set_server utf8
character_
set_system utf8
character_
sets_dir D:\phpStudy2\MySQL\share\charsets\

在dos下寫入數據,從庫就會因為character_set_results=gbk編碼報錯。
使用set character_set_results='utf8' 修改編碼后,就不會在報錯。

在doc下修改主庫數據,經常會碰到莫名其妙的編碼問題;

17. 擴展

1. mysql 雙主,其實就是兩個mysql互為主從,設置方法和上邊一樣。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



一.Mysql主從復制配置 配置MySQL主從復制 mysql主從復制配置 MySQL 主從復制配置 [mysql] 主從復制配置 MySQL主從復制配置 mysql主從復制配置 MySQL主從復制配置 Mysql主從復制配置 MySQL主從復制配置
 
粤ICP备14056181号  © 2014-2021 ITdaan.com