MySQL主从复制原理
主服务器数据库的每次操作都会记录在其二进制文件mysql-bin.xxx(该文件可以在mysql目录下的data目录中看到)中,从服务器启动一个I/O线程使用专用账号登录到主服务器中读取该二进制文件,并将文件内容写入到自己本地的中继日志relay-log文件中,然后从服务器在启动一个SQL线程并根据中继日志中的内容执行SQL语句写入到同步的数据库中.
原理图如下:
前期准备:
关闭防火墙和selinux:
iptables -F #关闭防火墙
setenforce 0 #关闭selinux
getenforce #查看selinux状态
vim /etc/selinux/config #在配置文件中修改selinux
[root@localhost html]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled #改成disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
:wq
下载源码包:
在两台服务器上都下地好源码包,将源码包下载到/usr/local/src路径中
mysql源码包地址:
http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
开发环境与编译工具:
在两台服务器上搭建好编译环境
yum -y groupinstall Development Tools
yum -y install make gcc-c++ cmake bison-devel ncurses-devel libaio libaio-devel perl-Data-Dumper net-tools
1.安装mysql并配置:
要做主从复制首先要安装好mysql,两台服务器安装是一样,步骤如下:
查询并卸载默认mysql:
rpm -qa | grep mysql #如果查询到mysql,就是用rpm -e命令来卸载了原来的mysql
rm -rf /etc/my.cnf #删除原来mysql的配置文件
下载并解压:
cd /usr/local/src #进入下载目录
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
tar -zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz #在下载目录中解压文件
进入下载目录并复制文件:
我们将mysql统一安装在/usr/localmysql目录中,方便管理.
mkdir /usr/local/mysql #创建安装目录,方便以后管理
cp -R ./mysql-5.6.35-linux-glibc2.5-x86_64/* /usr/local/mysql/ #复制解压文件的目录中
进入安装目录并安装:
cd /usr/local/mysql/
mkdir /data #创建数据目录
useradd -s /sbin/nologin mysql #创建mysql用户
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql #初始化脚本
复制配置文件并配置:
cp support-files/my-default.cnf /etc/my.cnf #配置文件
cp support-files/mysql.server /etc/init.d/mysql.server #启动脚本
在vim /etc/my.cnf配置如下:
[root@localhost mysql]# vim /etc/my.cnf
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
datadir =/data/mysql #添加这个数据存放路径
# port = .....
# server_id = .....
# socket = .....
socket = /tmp/mysql.sock #还有这个,其他暂时不用动
...省略...
:wq
启动mysql并初始设置:
/etc/init.d/mysql.server start #启动mysql
/usr/local/mysql/bin/mysql_secure_installation #初始设置,包含root密码设置等等
检查服务是否启动:
[root@localhost src]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 995/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1350/master
tcp6 0 0 :::22 :::* LISTEN 995/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1350/master
tcp6 0 0 :::3306 :::* LISTEN 15505/mysqld
到这里两台mysql服务器已经搭建完成了,下面开始做主从复制配置.
2.mysql 主从复制:
主节点IP: 192.168.1.107
从节点IP: 192.168.1.110
主节点配置:
编译/etc/my.cnf文件的[mysqld]下加入如下配置
[root@localhost src]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
server_id = 1 #主数据库端ID号
log-bin = mysql-bin #开启二进制日志
binlog-do-db = db #需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可
auto_increment_offset = 1 #这个参数一般用在主主同步中,用来错开自增值, 防止键值冲突
auto_increment_increment = 1 #这个参数一般用在主主同步中,用来错开自增值, 防止键值冲突
log_bin_trust_function_creators = 1 #将函数复制到slave
...省略...
:wq
重启mysql并配置用户密码:
#重启mysql
[root@localhost mysql]# /etc/init.d/mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
#登录mysql
[root@localhost mysql]# /usr/local/mysql/bin/mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
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.
#配置从服务器登录的用户密码,权限等
mysql> grant replication slave,replication client on *.* to 'slave'@'192.168.1.110' identified by 'slpass';
Query OK, 0 rows affected (0.00 sec)
#刷新权限
mysql> FLUSH PRIVILEGES;
查看主节点状态:
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 430
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
从节点配置:
编译/etc/my.cnf文件的[mysqld]下加入如下配置
[root@localhost src]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
server_id = 2 #从节点的ID号,不能和主节点的一样
relay_log = relay-log #开启中继日志
read_only = ON #只读开启,实现读写分离
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
datadir = /data/mysql
...省略...
:wq
"/etc/my.cnf" 36L, 1224C
重启从节点mysql并配置:
#重启mysql
[root@localhost mysql]# /etc/init.d/mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
#登录从节点mysql
[root@localhost mysql]# /usr/local/mysql/bin/mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
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.
#设置复制的主节点数据库,pos位置
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.107',MASTER_USER='slave',MASTER_PASSWORD='slpass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=0;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
查看从节点状态:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.107
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003 #复制的二进制文件
Read_Master_Log_Pos: 4 #从哪个pos位置开始复制
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No #复制功能还没有启动
Slave_SQL_Running: 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: 4
Relay_Log_Space: 120
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: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 23562d2f-6c4d-11e8-9fbd-000c29f223ba
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 180610 00:35:58
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.01 sec)
ERROR:
No query specified
开启主从复制并查看状态:
#开启复制功能
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.1.107
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 430 #已经复制到430的pos了
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 593
Relay_Master_Log_File: mysql-bin.000003
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: 430
Relay_Log_Space: 760
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: 1
Master_UUID: 23562d2f-6c4d-11e8-9fbd-000c29f223ba
Master_Info_File: /data/mysql/master.info
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
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:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
看到 Slave_IO_Running: Yes 和Slave_SQL_Running: Yes 都是yes了,说明主从复制已经生效了,环境搭建完成.