Mysql master-slave server principle and service construction, super detailed can be followed, novices can also learn

principle

Why should there be a master-slave server

In the corporate website, when there is only one back-end MySQL database, the following problems will occur:

Single point of failure, service unavailable
Unable to handle a large number of concurrent data requests
Data loss-catastrophe

Insert picture description here
Modification method:

Increase the MySQL database server to back up data to form a master/backup
Ensure that the primary and secondary MySQL database server data are the same
The main server is down, the backup server continues to work, and the data is guaranteed

MySQL master-slave replication is closely related to read-write separation.
Insert picture description here
Of course, there are more advanced solutions, read-write separation. Today we only talk about master-slave replication

Master-slave knowledge expansion

Types of MySQL master-slave replication:

Statement-based replication (default) The statement executed on the master server, the slave server executes the same statement,
Row-based replication Copy the changed content to the slave server
Mixed types of replication -Once it is found that the statement-based copy cannot be accurately replicated, line-based replication will be used

###The working process of master-slave replication The process of
Insert picture description here
replication:
·Before the completion of each transaction update data, the Master records these changes in the binary log. After writing the binary log, the Master informs the storage engine to submit the transaction.

Slave copies the Binary log of the Master to the relay log. First, the Slave starts a worker thread - l/o thread, which opens a normal connection on the Master, and then starts the Binlog dump process, and the Binlog dump process starts from the master. Read events in the binary log. If the Master has been updated, it will sleep and wait for new events from the Master. The l/o thread writes these logs to the relay log

SQL Slave thread (SQL slave thread) handles the last step of the process. The SQL thread reads events from the relay log and replays the events to update the slave data to make it consistent with the data in the master. As long as the thread is consistent with the 1/0 thread, the relay log is usually located in the OS cache, so the overhead of the relay log is small.

Mysql master-slave service construction

Environment introduction:
20.0.0.21 master server
20.0.0.25 slave server
20.0.0.26 slave server

###master, slave1, slave2####
Turn off the firewall, turn off the core protection

(1) Establish a time synchronization environment

1、在主机 Master 搭建时间同步服务器NTP (20.0.0.21)
[root@localhost ~]# yum-y install ntp
[root@localhost ~]# vi /etc/ntp.conf               ####最后面添加这二行
server 127.127.1.0
fudge 127.127.1.0 stratum 8

[root@localhost ~]# service ntpd restart
[root@localhost ~]# systemctl restart ntpd
[root@localhost ~# systemctl enable ntpd

2、在从服务器上配置NTP同步
登录到从1服务器 20.0.0.25
[root@localhost ~]# yum-y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.21
[root@localhost ~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.21 >>/var/log/ntpdate.log

[root@localhost ~]# systemctl restart crond
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# tail -f /var/log/ntpdate.log             ###动态查看更新日志文件

登录到从2服务器 20.0.0.26
[root@localhost ~]# yum-y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.21
[root@localhost ~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.21 >>/var/log/ntpdate.log
[root@localhost ~]# systemctl restart crond
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# tail -f /var/log/ntpdate.log             ###动态查看更新日志文件

(2) Compile and install MySQL database

     ##三台都是此操作,可同步进行
#使用CRT登录3台主机, 登录以后检查, 登录是否正常, 确认下主机的IP地址是否正确, 各个主机通信是否正常。
登录 20.0.0.21 此终端, 在终端页面最下面, 右击打开 ---send conmands to all sessions
    ###此功能是敲一条命令, 其他终端全部同步执行


1、安装Mysq环境依赖包
[root@localhost ~]#
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake \
gcc \
gcc-c++

2、创建运行用户
[root@localhost ~]# useradd -s /sbin/nologin mysql

3、编译安装
###上传mysql-boost-5.7.20.tar.gz到opt目录下###
[root@localhost ~]cd /opt
[root@localhost opt]# tar xzvf mysql-boost-5.7.20.tar.gz
[root@localhost opt]#cd /opt/mysql-5.7.20/

[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 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1

######编译安装####

make && make install


#####数据库目录进行权限调整###为了让你安装时不报错,踩坑集!!###########

chown -R mysql:mysql /usr/local/mysql/


#####建立调整配置文件########

vi /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

chown mysql:mysql /etc/my.cnf

#######设置环境变量####
###为了让系统能够直接识别 mysql/bin下的命令   和mysql/lib 下的各种库

echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/bin:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile
source /etc/profile

cd /usr/local/mysql/

###########初始化##########
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

###为什么cp usr/而不是cp /usr/ ?因为现在是在这个目录底下
###为什么要cp?为了让systemctl进行管理,systemctl管理/usr/lib/systemd/system/下的程序
cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/

systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld
netstat -anpt | grep 3306

###刚开始没密码是空的直接回车,然后输入密码
mysqladmin -u root -p password 

mysql -u root -p     输入刚刚的密码

(3) Log in to the Master master server to configure 20.0.0.21

 ###在原来server-id = 1的地方修改成11后面新增下面两行

[root@localhost mysq]# vi /etc/my.cnf 
server-id = 11
log_bin = master-bin
log-slave-updates = true

[root@localhost mysq]# systemct restart mysqld                 ###重启数据库

#登录Master数据库给从服务器授权
[root@localhost mysql# mysql -uroot -p

#下面的账号和密码是给从服务器使用的
mysql>grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by 'abc123';
mysąl> flush privileges;

#下面这条命令下的东西尤其重要,FILE和POS都是一会从服务器要使用的
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      599 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+

(4) Log in to salve and configure from the server

[root@localhost mysql]# vi /etc/my.cnf     ##在原来server-id = 1的地方修改成22后面新增
server-id = 22
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

[root@localhost mysq]# systemctl restart mysqld     ##重启数据库

#登录Slave数据库配置同步   ###注意下这边的master_log_file='master-bin.000001', 
master_log _pos=599; 要和Master数据库信息一致, 不一致的话要更改先stop slave;
#然后更改同步信息

[root@localhost mysql]#mysql -uroot -p

##账号和密码是主服务器设置的,同步的日志文件和pos也是刚刚主服务器的
mysql>change master to master_host='20.0.0.21',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=599;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
********************* 1. row*********************************
Slave_lO_State: Waiting for master to send event
Master_Host: 192.168.32.11
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 604
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_lO_Running: Yes      ####开启
Slave SQL Running: Yes     ####开启

##看到上面两条的时候说明成功了,如果出现其他,请检查配置文件
#####登录salve2从服务器配置20.0.0.26 ####配置和上面从1服务器一样

[root@localhost mysq]# vi /etc/my.cnf       ##在原来server-id =1的地方修改成22 后面新增           
server-id = 33
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

[root@localhost mysq]# systemctl restart mysqld           ###重启数据库

#登录Slave 数据库配置同步   ###注意下这边的master_log_file= 'master-bin.000001',                  
master_log_pos= 599; 要和Master 数据库信息一致, 不一致的话要更改先stop slave;
#然后更改同步信息

[root@localhost mysqj]j# mysql -uroot -p

mysql> change master tomaster_host='20.0.0.21',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=599;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G
***************1. row**************************
Slave_lO_State: Waiting for master to send event
Master_Host: 192.168.32.11
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 604
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_lO_Running: Yes      ####开启
Slave SQL Running: Yes     ####开启

(5) Verification

主服务器操作:
mysql> create database qhdx;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| qhdx               |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

###从服务器1查看 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| qhdx               |
| qq                 |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

###从服务器2查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| qhdx               |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Read and write the next article

Guess you like

Origin blog.csdn.net/weixin_48190891/article/details/108590434