From the introduction of the main
Mysql master-slave called Replication, AB copy. Simply, it is A and B shots from two machines, write data on the A, B will be followed by another one write data, real-time data synchronization
mysql master-slave based binlog, need to be open binlog master from the master
About three main process steps from
Lord binlog in changes to operational records
Binlog events from the Lord (sql statement) synchronize the machine and recorded in relaylog
The execution order from inside sql statement relaylog
Master-slave role
- Real-time disaster recovery for failover
- Separate read and write, provide consulting services
- Backup, to avoid affecting business
From the main form
* 一主一从
* 主主复制
* 一主多从---扩展系统读取的性能,因为读是在从库读取的
* 多主一从---5.7版本开始支持
* 联级复制
Copy the master-slave principle
Master copy from step
- The main library writes all log records in the binlog, and generates log dump thread, will pass from the library binlog log I / O threads
-
Library generated from two threads, one I / O thread, another thread is SQL
- I / O request binlog thread to the log of the primary database, and the log file is written binlog relay log (relay log) in
- SQL thread reads the content of the relay loy, and parsed into specific operation to achieve consistent master-slave operation, the final data achieve the same purpose
From the master copy configuration steps:
- Data from the database to ensure consistency with the master database of
- Create an account synchronization licensed to use the database from the primary database
- With the main database (to modify the configuration file)
- Configuration database (modify configuration files) from
Needs
to build a MYSQL two servers, one as the primary server, from the server as a primary server writes a read operation from the server
Environment Description
Database Roles | IP | Applications and Systems | Whether the data |
---|---|---|---|
Master database | 192.168.55.130 | centos7 mysql-5.7 | Have |
From the database | 192.168.55.129 | centos7 mysql-5.7 | no |
On both servers are installed by mysql
环境准备
关闭防火墙以SELINUX
[root@yanyinglai ~]# systemctl stop firewalld
[root@yanyinglai ~]# systemctl disable firewalld
[root@yanyinglai ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@yanyinglai ~]# setenforce 0
安装mysql
安装依赖包
[root@yanyinglai ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel 创建用户和组 [root@yanyinglai ~]# groupadd -r -g 306 mysql [root@yanyinglai ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql 下载二进制格式的mysql软件包 [root@yanyinglai ~]# cd /usr/src/ [root@yanyinglai src]#wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz 解压软件至/usr/local/ [root@yanyinglai src]# ls debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz [root@yanyinglai src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@yanyinglai src]# ls /usr/local/ bin etc games include lib lib64 libexec mysql-5.7.22-linux-glibc2.12-x86_64 sbin share src [root@yanyinglai src]# cd /usr/local/ [root@yanyinglai local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql "mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/" [root@yanyinglai local]# ll 总用量 0 drwxr-xr-x. 2 root root 6 11月 5 2016 bin drwxr-xr-x. 2 root root 6 11月 5 2016 etc drwxr-xr-x. 2 root root 6 11月 5 2016 games drwxr-xr-x. 2 root root 6 11月 5 2016 include drwxr-xr-x. 2 root root 6 11月 5 2016 lib drwxr-xr-x. 2 root root 6 11月 5 2016 lib64 drwxr-xr-x. 2 root root 6 11月 5 2016 libexec lrwxrwxrwx. 1 root root 36 9月 7 22:20 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/ drwxr-xr-x. 9 root root 129 9月 7 22:19 mysql-5.7.22-linux-glibc2.12-x86_64 drwxr-xr-x. 2 root root 6 11月 5 2016 sbin drwxr-xr-x. 5 root root 49 9月 3 23:02 share drwxr-xr-x. 2 root root 6 11月 5 2016 src 修改目录/usr/locaal/mysql的属主属组 [root@yanyinglai local]# chown -R mysql.mysql /usr/local/mysql [root@yanyinglai local]# ll /usr/local/mysql -d lrwxrwxrwx. 1 mysql mysql 36 9月 7 22:20 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/ 添加环境变量 [root@yanyinglai local]# ls /usr/local/mysql bin COPYING docs include lib man README share support-files [root@yanyinglai local]# cd [root@yanyinglai ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@yanyinglai ~]# . /etc/profile.d/mysql.sh [root@yanyinglai ~]# echo $PATH /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 建立数据存放目录 [root@yanyinglai ~]# cd /usr/local/mysql [root@yanyinglai mysql]# mkdir /opt/data [root@yanyinglai mysql]# chown -R mysql.mysql /opt/data/ [root@yanyinglai mysql]# ll /opt/ 总用量 0 drwxr-xr-x. 2 mysql mysql 6 9月 7 22:25 data 初始化数据库 [root@yanyinglai mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/ //这个命令的最后会生成一个临时密码,此处密码是1EbNA-k*BtKo 配置mysql [root@yanyinglai ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql "/usr/local/include/mysql" -> "/usr/local/mysql/include/" [root@yanyinglai ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf [root@yanyinglai ~]# ldconfig -v 生成配置文件 [root@yanyinglai ~]# cat > /etc/my.cnf <<EOF > [mysqld] > basedir = /usr/local/mysql > datadir = /opt/data > socket = /tmp/mysql.sock > port = 3306 > pid-file = /opt/data/mysql.pid > user = mysql > skip-name-resolve > EOF 配置服务启动脚本 [root@yanyinglai ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@yanyinglai ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld [root@yanyinglai ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld 启动mysql [root@yanyinglai ~]# service mysqld start Starting MySQL.Logging to '/opt/data/yanyinglai.err'. .. SUCCESS! [root@yanyinglai ~]# ps -ef|grep mysql root 4897 1 0 22:38 pts/2 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid mysql 5075 4897 6 22:38 pts/2 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=yanyinglai.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306 root 5109 4668 0 22:38 pts/2 00:00:00 grep --color=auto mysql [root@yanyinglai ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 80 :::3306 :::* 修改密码 使用临时密码修改 [root@yanyinglai ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.22 Copyright (c) 2000, 2018, 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> set password = password('123456'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> quit Bye mysql主从配置 确保从数据库与主数据库的数据一样先在主数据库创建所需要同步的库和表 [root@yanyinglai ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. Al Oracle is a registered trademark of Oracle Corporation and affiliates. Other names may be trademarks of their respect owners. Type 'help;' or '\h' for help. Type '\c' to clear the curr mysql> create database yan; Query OK, 1 row affected (0.00 sec) mysql> create database lisi; Query OK, 1 row affected (0.00 sec) mysql> create database wangwu; Query OK, 1 row affected (0.00 sec) mysql> use yan; Database changed mysql> create table tom (id int not null,name varchar(100)not null ,age tinyint); Query OK, 0 rows affected (11.83 sec) mysql> insert tom (id,name,age) values(1,'zhangshan',20),(2,'wangwu',7),(3,'lisi',