配置mysql主从复制

时间:2022-09-21 13:16:23

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互为主从,设置方法和上边一样。