Mysql: master-slave synchronous replication

A: scenarios

In the corporate Web site, when only one back-end database Mysql, will have the following questions:
<1>: a single point of failure, mysql database once down, the service is not available
<2>: Unable to handle a large number of concurrent data requests
<3> : Once the data loss would be a big accident
Here Insert Picture Description
in order to prevent the occurrence of such incidents, and have a more safe and effective way to transform ---- set master-slave synchronization.

From the main role:

<1>: increase MySQL database server, backup of the data, master and slave. Ensure that primary and MySQL database server data is the same, the primary server goes down, the backup server to continue to work, the data is guaranteed.
<2>: to synchronize data from replicated by the main embodiment, to further enhance the load capability database concurrency by separate read and write

Here Insert Picture Description

Two: MySQL master-slave principle backup

providing master binlog binary log
slave from the master binlog pick by I / O thread and copied to the slave relay log
slave binlog read from the relay log by the slave SQL thread, and then parsed into the slave

Note: The master and slave database version must be the same. Main library or database version must be higher than that from the library, or cause more problems occur, consistent with the best version.

Mysql master-slave replication deployment environment:

Master: master 192.168.17.141
from the server: slave1 192.168.17.135
from the server: slave2 192.168.17.128

III: master-slave synchronization step

1: the master server mysql-master configurations:

<1>: master primary server goes down Firewalld

[root@localhost ~]# hostnamectl set-hostname master   主机更名为master
[root@localhost ~]# su
[root@master ~]# systemctl stop firewalld
[root@master ~]# setenforce 0

<2>: establish time synchronization environment, configure the NTP time synchronization server installation, and start the service.
Installation ntp service using yum
modify ntp.conf, set the time synchronization master source

[root@master ~]# yum install ntp -y
[root@master ~]# vim /etc/ntp.conf
/server 查找 插入:
server 127.127.17.0     主服务器是时钟源
fudge 127.127.17.0 stratum 8   设置时间层级为8
[root@master ~]# systemctl start ntpd

<3> MySQL manual translation (herein mysql5.7 compiled version)

[root@localhost LNMP-C7]# tar zxvf mysql-boost-5.7.20.tar.gz -C /opt    //解压mysql到/opt
[root@localhost LNMP-C7]# cd /opt/mysql-5.7.20/
[root@localhost mysql-5.7.20]# yum install ncurses ncurses-devel bison cmake expat-devel -y   //下载mysql的环境包
[root@localhost mysql-5.7.20]# useradd -s /sbin/nologin mysql     //创建mysql的程序用户

cmake配置nginx的编译选项
[root@localhost mysql-5.7.20]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost -DWITH_SYSTEMD=1
[root@localhost mysql-5.7.20]# make && make install    //编译和安装
[root@localhost mysql-5.7.20]# vim /etc/my.cnf         //更改配置文件
把之前全删掉,重新写入:

[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
   
[root@localhost mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql     //数据库目录进行权限调整,更改属主和属组
[root@localhost mysql-5.7.20]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile      //更改环境变量
[root@localhost mysql-5.7.20]# echo 'export PATH' >> /etc/profile     //设置全局变量
[root@localhost mysql-5.7.20]# source /etc/profile      使/etc/profile的更改生效
[root@localhost mysql-5.7.20]# cd /usr/local/mysql  
[root@localhost mysql]#         //初始化数据库
 bin/mysqld \         
 --initialize-insecure \
 --user=mysql \
 --basedir=/usr/local/mysql \
 --datadir=/usr/local/mysql/data 
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service  /usr/lib/systemd/system 
[root@localhost mysql]# systemctl start mysqld   //开启mysql服务
[root@localhost mysql]# netstat -ntap | grep mysqld   //查看mysql服务是否开启
[root@localhost mysql]# systemctl enable mysqld      //设置开机自启动
[root@localhost mysql]# mysqladmin -uroot -p password   //设置mysql的初始密码
[root@master ~]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@master ~]# echo 'export PATH' >> /etc/profile
[root@master ~]# source /etc/profile

<4>: Master main server configuration, and start the service.
/Etc/my.cnf modify the configuration file, add the server id, configure binary logging options

[root@master ~]# vim /etc/my.cnf
更改插入:
server-id = 11
log-bin=master-bin
log-slave-updates=true
[root@master ~]# systemctl restart mysqld

<5>: Sign MySQL service, all authorized permission to copy the binary logs from the server. And view the binary log position

[root@master ~]# mysql -uroot -p  //登录数据库
mysql> grant replication slave on *.* to 'myslave'@'192.168.17.%' identified by '111';
mysql> show master status;

   master-bin.000001     451

2: mysql-slave1 from the server configuration:

<1>: slave1 server closed Firewalld

[root@slave1 ~]# systemctl stop firewalld
[root@slave1 ~]# setenforce 0

<2>: the time synchronization from the server and open the service
yum installation and use time synchronization ntpdate

[root@slave1 ~]# yum install ntp ntpdate -y
[root@slave1 ~]# systemctl start ntpd
[root@slave1 ~]# /usr/sbin/ntpdate 192.168.17.141

<3> installation manual translation mysql5.7 (master and master mysql database compilation step, as can be seen above, will be omitted here)

<4>: Configure slave1 from the server, and start the service.
/Etc/my.cnf modify the configuration file, add the server id, configure binary logging options

[root@slave1 ~]# vim /etc/my.cnf
插入更改:
server-id = 22
relay-log=relay-log-bin              从服务器同步日志文件记录到本地中继日志 
relay-log-index=slave-relay-bin.index    定义relay-log的位置和名称
[root@slave1 ~]# systemctl restart mysqld   

<5>: log MySQL, configure the master-slave synchronization

[root@slave1 ~]# mysql -uroot -p
mysql> change master to master_host='192.168.17.141',master_user='myslave',master_password='111',master_log_file='master-bin.000001',master_log_pos=451;
mysql> start salve;     启动主从同步
mysql> show slave status\G;   查看slave状态
   Slave_l0_Running: Yes
   Slave_SQL_Running: Yes

Here Insert Picture Description

3: mysql-slave2 from the server configuration:

<1>: slave2 server closed Firewalld

[root@slave2 ~]# systemctl stop firewalld
[root@slave2 ~]# setenforce 0

<2>: the time synchronization from the server and open the service
yum installation and use time synchronization ntpdate

[root@slave2 ~]# yum install ntp ntpdate -y
[root@slave2 ~]# systemctl start ntpd
[root@slave2 ~]# /usr/sbin/ntpdate 192.168.17.141

<3> installation manual translation mysql5.7 (master and master mysql database compilation step, as can be seen above, will be omitted here)

<4>: Configure slave1 from the server
to modify /etc/my.cnf profile, increase server id, configure binary logging options

[root@slave2 ~]# vim /etc/my.cnf
插入更改:
server-id = 23
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[root@slave1 ~]# systemctl restart mysqld

<5>: log MySQL, configure the master-slave synchronization

[root@slave1 ~]# mysql -uroot -p
mysql> change master to master_host='192.168.17.141',master_user='myslave',master_password='111',master_log_file='master-bin.000001',master_log_pos=451;
mysql> start salve;
mysql> show slave status\G;

Here Insert Picture Description

4: the synchronization configuration, master-slave synchronization verification replication effect

主服务器master--创建数据库:
   mysql> create database abc;
   mysql> show databases;
从服务器slave1--查看数据库: mysql> show databases;
从服务器slave2--查看数据库: mysql> show databases;

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Log in MySQL master server, the new database abc
login from the server MySQL, the database view, respectively, showing the same master database, the master-slave replication success.

Published 40 original articles · won praise 6 · views 672

Guess you like

Origin blog.csdn.net/weixin_45691464/article/details/103964962