linux运维架构之路-MySQL主从复制
Posted Demon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux运维架构之路-MySQL主从复制相关的知识,希望对你有一定的参考价值。
一、MySQL主从复制原理图
MySQL主从复制原理:实现主从复制原理是三个线程完成的,主的I/O线程,备的I/O线程与SQL线程
1、首先主库db01需要开启binlog、授权一个replication client账号,锁表进行一次数据全备
2、从库db02、db03导入主库全备数据从而实现主从数据一致性,从库执行change master to将主机、端口、用户名、密码、binlog文件名、binlog位置点存入master.info中
3、从库start slave,首先由从库的I/O线程携带master.info的信息去询问主库,验证rep账号,根据binlog文件和位置点,将新增的binlog文件发回去,同时携带着发送到的文件和位置点,从库收到存到master.info中,收到的binlog日志存放在reply-bin文件中,由从库的SQL执行,先解析binlog文件再先入数据库,执行到的信息存入repay.info中。从库会一直询问主库是否有新的数据,主库有新的数据会返回给从库。
二、MySQL主从复制部署
1、部署环境
[root@db01 tools]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@db01 tools]# uname -r 2.6.32-696.el6.x86_64 [root@db01 tools]# ll /application/ total 4 lrwxrwxrwx 1 root root 26 2017-10-24 11:33 mysql -> /application/mysql-5.6.34/ drwxr-xr-x 13 mysql mysql 4096 2017-10-24 11:33 mysql-5.6.34
2、服务器规划
服务器名称 |
外网IP |
内网IP |
主机名 |
MySQL数据库db01 |
172.19.5.51/24 |
172.16.1.51/24 |
db01 |
MySQL数据库db02 |
172.19.5.52/24 |
172.16.1.52/24 |
db02 |
MySQL数据库db03 |
172.19.5.53/24 |
172.16.1.53/24 |
db03 |
3、安装MySQL
useradd -s /sbin/nologin -M mysql cd /server/tools tar xf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz mkdir -p /application/ mv /server/tools/mysql-5.6.34-*-x86_64 /application/mysql-5.6.34 ln -s /application/mysql-5.6.34/ /application/mysql chown -R mysql.mysql /application/mysql/ /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql \\cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld sed -i \'s#/usr/local/mysql#/application/mysql#g\' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld \\cp /application/mysql/support-files/my-default.cnf /etc/my.cnf /etc/init.d/mysqld start lsof -i:3306 echo \'export PATH=/application/mysql/bin:$PATH\' >>/etc/profile source /etc/profile chkconfig --add mysqld chkconfig mysqld on /application/mysql/bin/mysqladmin -u root password \'123456\'
4、修改db01、db02、db03数据库配置文件/etc/my.conf
[root@db01 ~]# egrep "log_bin|server_id" /etc/my.cnf
log_bin = mysql-bin #主库开启log_bin
server_id = 1 #server_id一定不能相同
[root@db02 ~]# grep server_id /etc/my.cnf
server_id = 2
[root@db03 ~]# grep server_id /etc/my.cnf
server_id = 3
5、主库db01操作
mysql -uroot -p123456#登录数据库 grant replication slave on *.* to \'rep\'@\'172.16.1.%\' identified by \'123456\';#授权从库用户 flush table with read lock;#锁表 show master status; #查看主库binlog和位置点
+------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 326 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysqldump -uroot -p123456 -A -B --master-data=2|gzip >/tmp/mysql_$(date +%F).sql.gz #全备主库数据 [root@db01 ~]# scp -rp /tmp/mysql_2017-10-24.sql.gz 172.16.1.52:/tmp/ #分发主库全备数据到从库 [root@db01 ~]# scp -rp /tmp/mysql_2017-10-24.sql.gz 172.16.1.53:/tmp/ mysql> unlock table;#解锁表
6、从库db02、db03操作一样
[root@db02 ~]# gzip -d /tmp/mysql_2017-10-24.sql.gz [root@db02 ~]# mysql -uroot -p123456</tmp/mysql_2017-10-24.sql#从库导入主库全备数据,保证主从数据一致性 Warning: Using a password on the command line interface can be insecure. mysql -uroot -p\'123456\' << EOF CHANGE MASTER TO MASTER_HOST=\'172.16.1.51\', #主库IP地址 MASTER_PORT=3306, MASTER_USER=\'rep\', #授权用户 MASTER_PASSWORD=\'123456\', MASTER_LOG_FILE=\'mysql-bin.000001\',#binlog日志 MASTER_LOG_POS=326; #位置点 EOF mysql -uroot -p123456 start slave;#开启主从同步功能 mysql> show slave status\\G#查看主从同步状态,下图看到Slave_IO和Slave_SQL为两个Yes刚证明已经同步 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.1.51 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 326 Relay_Log_File: db02-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
以上是关于linux运维架构之路-MySQL主从复制的主要内容,如果未能解决你的问题,请参考以下文章