linux運維、架構之路-MySQL主從復制
阿新 • • 發佈:2017-10-24
date sed mage lin 密碼 sla 環境 從數據 nlog
一、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-2411: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、安裝部署
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;#解鎖表
linux運維、架構之路-MySQL主從復制