您的位置:首页 > 数据库 > MySQL

17.1 MySQL主从介绍17.2 准备工作17.3 配置主17.4 配置从17.5 测试主从同步

2018-03-27 11:08 736 查看

17.1 MySQL主从介绍

MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的

• MySQL主从是基于binlog的,主上须开启binlog才能进行主从。bilog,是二进制文件,无法cat

• 主从过程大致有3个步骤:

1)主将更改操作记录到binlog里

2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里;relaylog,中继日志

3)从根据relaylog里面的sql语句按顺序执行

主上有一个log dump线程,用来和从的I/O线程传递binlog

从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地

mysql原理图



使用场景:

一、数据备份,主机器宕机,从机器还能随时对web提供服务

二、数据备份且可以分担主机器被调用数据时的压力,mysql主从,是有方向性的,写数据,必须从主机器开始;如果不依照原理会导致数据紊乱

17.2 准备工作

主从配置 - 主上操作

mysql安装总结

进入src专用的下载目录下,下载mysql二进制免编译包

wget 56版本,x64位

解压

tar zxvf 。。。

移动

mv 。。 /usr/local/mysql

移动时,需注意这个目录是否已经存在,如果存在了,需要删除旧文件

初始化配置
进入所在的mysql目录

[root@aminglinux-02 mysql]# pwd
/usr/local/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

--user=mysql 需提前创建好这个用户

编辑my.cnf文件(7版本的centos系统,就自带安装一个mariadb,所以就会自动生成有一个my.cnf,就省去了拷贝)

vim /etc/my.cnf   //添加以下两行内容

datadir=/data/mysql
socket=/tmp/mysql.sock

拷贝启动脚本

cp support-files/mysql.server /etc/init.d/mysqld

编辑启动脚本

vim /etc/init.d/mysqld //对以下两行进行指定路径
basedir=
datadir=

指定basedir的路径 /usr/local/mysql
指定datadir的路径 /data/mysql

最重要的一步,记得查看/data/mysql 的默认属主、属组,如果不是mysql的,启东时会因为无法写入数据而不能启动

chomd mysql:mysql /data/mysql

然后就可以尝试启动

/etc/init.d/mysql start

主机器

192.168.133.131
[root@aminglinux-02 ~]#

MySQL启动情况

[root@aminglinux-02 ~]# ps aux |grep mysql
root      1399  0.0  0.0 115376  1684 ?        S    13:55   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aminglinux-02.pid
mysql     1738  1.1 24.0 1038600 451880 ?      Sl   13:55   0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql--plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/aminglinux-02.err --pid-file=/data/mysql/aminglinux-02.pid --socket=/tmp/mysql.sock
root      2390  0.0  0.0 112664   976 pts/0    R+   13:57   0:00 grep --color=auto mysql

从机器

192.168.133.130
[root@localhost ~]#

MySQL启动情况

[root@localhost ~]# ps aux |grep mysql
root      1343  0.0  0.0 115380  1732 ?        S    13:55   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/mysqldata --pid-file=/mysqldata/localhost.localdomain.pid
mysql     1864  0.0  6.0 1402948 113516 ?      Sl   13:55   0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/mysqldata/localhost.localdomain.err --pid-file=/mysqldata/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root      3532  0.0  0.0 112664   976 pts/0    S+   15:28   0:00 grep --color=auto mysql

17.3 配置主

修改主机器上的配置文件

server-id=131
log_bin=Master

更改完以后,需要重启mysql

/etc/init.d/mysqld restart

重启之后,查看/data/mysql/ 可以查看到新生成了几个文件

[root@aminglinux-02 ~]# ls -lt /data/mysql
总用量 110676
-rw-r----- 1 mysql mysql 50331648 8月  31 00:40 ib_logfile0
-rw-r----- 1 mysql mysql 12582912 8月  31 00:40 ibdata1
-rw-rw---- 1 mysql mysql    52309 8月  31 00:40 aminglinux-02.err
-rw-rw---- 1 mysql mysql        5 8月  31 00:40 aminglinux-02.pid
-rw-rw---- 1 mysql mysql       16 8月  31 00:40 Master.index
-rw-rw---- 1 mysql mysql      120 8月  31 00:40 Master.000001
drwx------ 2 mysql mysql      324 8月  30 00:12 zrlog
drwx------ 2 mysql mysql     4096 8月  28 14:48 xdm31037750_db
drwx------ 2 mysql mysql       20 8月  28 14:48 xdm31037750
-rw-rw---- 1 mysql mysql       56 8月  24 23:11 auto.cnf
-rw-r----- 1 mysql mysql      518 8月  24 01:36 xtrabackup_info
drwx------ 2 mysql mysql     4096 8月  24 01:36 performance_schema
drwx------ 2 mysql mysql     4096 8月  24 01:36 mysql
drwx------ 2 mysql mysql       20 8月  24 01:36 test
-rw-r----- 1 mysql mysql 50331648 8月  24 01:36 ib_logfile1


.index 索引页,这个文件是必须要有的

.000001 这个是二进制日志文件,会持续生成2、3、4等等(这个文件是实现主从配置的根本,没有这个文件根本没有办法实现)

准备一个数据,做演示用的

mysqldump -uroot -paminglinux zrlog > /tmp/zrlog.sql

创建一个新的库

mysql -uroot -paminglinux -e "create database aming"

恢复之前备份的zrlog.sql的数据到新库

mysql -uroot -paminglinux aming < /tmp/zrlog.sql

现在做的主从,参考的就是aming这个库

查看binlog 文件

ls -lt /data/mysql
总用量 110684
-rw-r----- 1 mysql mysql 50331648 8月  31 00:45 ib_logfile0
-rw-r----- 1 mysql mysql 12582912 8月  31 00:45 ibdata1
-rw-rw---- 1 mysql mysql    10699 8月  31 00:45 Master.000001
drwx------ 2 mysql mysql      324 8月  31 00:45 aming
-rw-rw---- 1 mysql mysql    52309 8月  31 00:40 aminglinux-02.err
-rw-rw---- 1 mysql mysql        5 8月  31 00:40 aminglinux-02.pid
-rw-rw---- 1 mysql mysql       16 8月  31 00:40 Master.index
drwx------ 2 mysql mysql      324 8月  30 00:12 zrlog
drwx------ 2 mysql mysql     4096 8月  28 14:48 xdm31037750_db
drwx------ 2 mysql mysql       20 8月  28 14:48 xdm31037750
-rw-rw---- 1 mysql mysql       56 8月  24 23:11 auto.cnf
-rw-r----- 1 mysql mysql      518 8月  24 01:36 xtrabackup_info
drwx------ 2 mysql mysql     4096 8月  24 01:36 performance_schema
drwx------ 2 mysql mysql     4096 8月  24 01:36 mysql
drwx------ 2 mysql mysql       20 8月  24 01:36 test
-rw-r----- 1 mysql mysql 50331648 8月  24 01:36 ib_logfile1

原来刚创建的时候大小为 120 ,新增一个库,以后库的大小变为 10699;理论上如果binlog文件完整的话,还可以使用这个文件进行恢复数据的

创建用于主从相互同步数据的用户

进入mysql

mysql -uroot -paminglinux

创建mysql用户密码

mysql> grant replication slave on *.* to 'repl'@'192.168.133.130' identified by 'aminglinux';

锁表

mysql>flush tables with read lock;

锁表的目的是不让表继续写,因为一会需要做从机器配置,需要进行一个同步,让两台机器同步,保证两台机器的数据一致,同步才不会出错

查看一下binlog的文件和大小

mysql>show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| Master.000001 |    10911 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

需要记住binlog的filename,可以文件的位置

对当前/data/mysql下的库,进行备份,因为需要在从机器上做一个同步,保证两台机器的数据一致,主从同步才不会出错

[root@aminglinux-02 mysql]# ls
aming              auto.cnf     ib_logfile1    mysql               xdm31037750      zrlog
aminglinux-02.err  ibdata1      Master.000001  performance_schema  xdm31037750_db
aminglinux-02.pid  ib_logfile0  Master.index   test                xtrabackup_info

高亮为库

17.4 配置从

主从配置 - 从上操作

从机器上,因为是按照其他的文档做的。mysql库文件所在路径为 /mysqldata;而且使用的是mariadb数据库

增加mysql变量

使变量永久生效

[root@aminglinux-02 ~]# vim /etc/profile

将变量放在配置文件末行添加即可

使变量直接生效

source /etc/profile  //重新加载

配置my.cnf

查看my.cnf,配置server-id=130(这个地址为本机IP),只有主机器才需要配置 log_bin 从机器不用配置

重启服务
因为之前我要调整重启的配置文件,已经关闭了,现在直接开启就好

[root@localhost ~]# /etc/init.d/mysqld start
Starting mysqld (via systemctl):                           [  确定  ]

数据一致

恢复数据
保证主从机器上的库数据一致

把主机器上备份的数据,拷贝到从机器上,然后做一个数据恢复

[root@localhost ~]# scp 192.168.133.131:/tmp/*.sql /tmp/
root@192.168.133.131's password:
pref.sql                                                                          100%  801     0.8KB/s   00:00
test.sql                                                                          100% 1258     1.2KB/s   00:00
xdm_db.sql                                                                        100%   36KB  35.7KB/s   00:00
xdm.sql                                                                           100% 1265     1.2KB/s   00:00
zrlog.sql                                                                         100%   10KB  10.0KB/s   00:00

为了方便使用mysql服务,将mysql目录加入到环境变量里

export PATH=$PATH:[命令地址]
[root@aminglinux-02 ~]# export PATH=$PATH:/usr/local/mysql/bin/

因为从机器,上没有用过mysql,使用空密码登录即可

[root@localhost ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.2.6-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

讲主机器上的库全部做一个创建,名字必须一样

create database aming;
create database xdm31037750_db;
create database xdm31037750;
create database zrlog;


test库、performance_schema库属于测试库,不用弄

恢复数据

[root@localhost ~]# mysql -uroot aming < /tmp/zrlog.sql
[root@localhost ~]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@localhost ~]# mysql -uroot xdm31037750 < /tmp/xdm.sql
[root@localhost ~]# mysql -uroot xdm31037750_db < /tmp/xdm_db.sql

再次进入mysql

[root@localhost ~]# mysql -uroot

停止slave

MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec

配置主机器相关配置

change master to master_host='192.168.133.131', master_user='repl', master_password='aminglinux', master_log_file='Master.000001', master_log_pos=10911;
指定主机器host                           指定主机器用户              指定主机器密码                           指定binlog文件名                     指定binlog文件大小

MariaDB [(none)]> change master to master_host='192.168.133.131', master_user='repl', master_password='aminglinux',master_log_file='Master.000001', master_log_pos=109
Query OK, 0 rows affected (0.02 sec)

也可以指定主机器的port,因为在生产环境中,也会有人更改mysql的默认端口 “ master_port=3306”

查看slave配置

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.133.131
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: Master.000001
Read_Master_Log_Pos: 10911
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: Master.000001
Slave_IO_Running: No      //这个很重要,其中一个是NO就表示主从已经端口
Slave_SQL_Running: Yes   //这个很重要,其中一个是NO就表示主从已经端口
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: 10911
Relay_Log_Space: 256
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be usedon slave but this does not always make sense; please check the manual before using it).
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 130
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)


因为my.cnf配置的时候没注意Server-id的配置,两个ID一样了导致, Slave_IO_Running: No ,主从服务没有办法启动。

更改了ID以后重新做一个“change master” 相关的配置以后就可以正常同步了,做“change master”之前,记得先“stop slave”

查看主从同步是否正常

•从上执行

mysql -uroot

• show slave stauts\G

• 看是否有

• Slave_IO_Running: Yes
• Slave_SQL_Running: Yes

• 还需关注

• Seconds_Behind_Master: 0  //为主从延迟的时间
• Last_IO_Errno: 0
• Last_IO_Error:
• Last_SQL_Errno: 0
• Last_SQL_Error:

恢复主机器上的表的写操作

unlock tables

到此,主从就算搭建完了

17.5 测试主从同步

几个配置参数

主服务器上

• binlog-do-db=            //仅同步指定的库(多个库,可以用“ , ”逗号分隔)
• binlog-ignore-db=     //忽略指定库

从服务器上

• replicate_do_db=          //仅同步指定的库
• replicate_ignore_db=     //忽略指定库
• replicate_do_table=         //仅同步指定的库
• replicate_ignore_table=    //忽略指定库,例如:有一个临时表,写的数据非常快,数据也大,每天都需要删除这时就可以使用这个,但如果
• replicate_wild_do_table=   //如aming.%, 支持通配符%  指定同步靠谱的匹配  同步表    这两个就够了根本不需要用上面的两条配置
• replicate_wild_ignore_table=   //如aming.%, 支持通配符%  指定同步靠谱的匹配  忽略表

测试

主机器

进入mysql
切换到axdm31037750_db库

mysql> use xdm31037750_db;

查看都有哪些表

mysql> show tables;
+--------------------------+
| Tables_in_xdm31037750_db |
+--------------------------+
| wp_commentmeta           |
| wp_comments              |
| wp_links                 |
| wp_options               |
| wp_postmeta              |
| wp_posts                 |
| wp_term_relationships    |
| wp_term_taxonomy         |
| wp_termmeta              |
| wp_terms                 |
| wp_usermeta              |
| wp_users                 |
+--------------------------+
12 rows in set (0.00 sec)

查看type表下的数据

mysql> select count(*) wp_user;
+---------+
| wp_user |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)


从机器

查看一下是否有同样的数据

MariaDB [(none)]> use xdm31037750_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [xdm31037750_db]> show tables;
+--------------------------+
| Tables_in_xdm31037750_db |
+--------------------------+
| wp_commentmeta           |
| wp_comments              |
| wp_links                 |
| wp_options               |
| wp_postmeta              |
| wp_posts                 |
| wp_term_relationships    |
| wp_term_taxonomy         |
| wp_termmeta              |
| wp_terms                 |
| wp_usermeta              |
| wp_users                 |
+--------------------------+
12 rows in set (0.00 sec)
MariaDB [xdm31037750_db]> select count(*) wp_user;
+---------+
| wp_user |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

在主机器上做一个删除表数据的操作

mysql> truncate table wp_users;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) wp_users;
+----------+
| wp_users |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

从机器上也看不出区别

所以做一个删除库的操作看看是否同步

-主机器上
删除库的操作

mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aming              |
| mysql              |
| performance_schema |
| test               |
| xdm31037750        |
| xdm31037750_db     |
| zrlog              |
+--------------------+
8 rows in set (0.00 sec)
mysql> drop database xdm31037750;
Query OK, 0 rows affected (0.02 sec)
mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aming              |
| mysql              |
| performance_schema |
| test               |
| xdm31037750_db     |
| zrlog              |
+--------------------+
7 rows in set (0.01 sec)


从机器

MariaDB [xdm31037750_db]> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aming              |
| mysql              |
| performance_schema |
| test               |
| xdm31037750        |
| xdm31037750_db     |
| zrlog              |
+--------------------+
8 rows in set (0.00 sec)
MariaDB [xdm31037750_db]> show databases;
+--------------------+
| Database           |
+--------------------+
| aming              |
| information_schema |
| mysql              |
| performance_schema |
| test               |
| xdm31037750_db     |
| zrlog              |
+--------------------+
7 rows in set (0.00 sec)

总结,可以看出两边的数据是一样的。

因为意外在从机器上做了意外的删除,新增,更改操作,就可以可能导致“Slave_SQL_Running: NO”

如果出现了“Slave_SQL_Running: NO ” 需要从新做一个“change master”的操作 比对主机器上的“master_log_pos=1091”,就可以恢复了

操作前需要sotp slave

change master to master_host='192.168.133.131', master_user='repl', master_password='aminglinux',master_log_file='Master.000001', master_log_pos=10911;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: