Test mysql master-slave configuration: a main achieve a separation from the reader

First, the master-slave introduce
Mysql master-slave called Replication, AB copy. Simply, it is A and B shots after two machines from the write data on the A, B will be followed by another one write data, real-time data synchronization.

Second, the role of master from
a real-time disaster recovery, failover for
2, separate read and write, provide access to services
3, backup, to avoid affecting the business

Third, on both servers are installed by mysql

1、环境准备
关闭防火墙以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

2, mounting mysql
install dependencies
[root @ yanyinglai ~] # yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

3, create users and groups
[the root yanyinglai @ ~] # 306 the groupadd -g -R & lt MySQL
[yanyinglai the root @ ~] # the useradd -M -s / sbin / nologin -g -u 306 306 MySQL

4, mysql package download binary format
[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

5、解压软件至/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

The total amount of 0
drwxr-XR-X. 2. 6. 11 the root the root bin 2016 dated. 5
drwxr-XR-X. 2. 6. 11 the root the root dated 2016 etc. 5
drwxr-XR-X. 2. 6. 11 the root the root dated 2016 games. 5
drwxr- xr-x. 2 root root 6. 11 dated. 5 2016 the include
drwxr-xr-x. 2 root root 6. 11 dated. 5 2016 lib
drwxr-xr-x. 2 root root 6. 11 dated. 5 2016 the lib64
drwxr-XR-X. 2 root root 6. 11 dated 2016 libexec. 5
. lrwxrwxrwx the root 36. 1 the root MySQL 22:20 on September 7 -> MySQL-5.7.22-Linux-glibc2.12-the x86_64 /
drwxr XR-X-129. 9 September 7, the root the root. MySQL 5.7.22-Linux-22:19-glibc2.12 the x86_64-
drwxr-XR-X. 2. 6. 11 the root the root sbin dated 2016. 5
drwxr-XR-X.. 5 the root the root. 9 dated 49 23:02 Share. 3
drwxr- xr-x. 2 root root 6. 11 dated 5 2016 src

6, modify the directory / usr / locaal / mysql the owner is a group
[the root @ yanyinglai local] # chown -R & lt mysql.mysql / usr / local / MySQL
[@ yanyinglai the root local] # LL / usr / local / MySQL -d
lrwxrwxrwx 1 mysql mysql 36. 9 dated 7 22:20 / usr / local / mysql -.> mysql-5.7.22-linux-glibc2.12-x86_64 /

7、添加环境变量
[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

8, the establishment of a data storage directory
[yanyinglai the root @ ~] # CD / usr / local / MySQL
[@ yanyinglai the root MySQL] # mkdir / opt / Data
[@ yanyinglai the root MySQL] # chown -R & lt mysql.mysql / opt / Data /
[root @ yanyinglai mysql] # ll / opt /
total amount of 0
drwxr-XR-X. 2. 6. 9 MySQL MySQL dated 7 22:25 data

9, initialize the database
[root @ yanyinglai MySQL] # / usr / local / MySQL / bin / MySQL mysqld --initialize --datadir --user = = / opt / the Data /
// this command will eventually generate a temporary password, here password is 1EbNA-k * BtKo

10、配置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

11, generating a configuration file
[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

12, configure the service startup script
[yanyinglai the root @ ~] -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld CP #
[@ yanyinglai the root ~] -ri # Sed 'S ^ # (the basedir =). # \. 1 / usr / local / MySQL # G '/etc/init.d/mysqld
[yanyinglai the root @ ~] -ri Sed #' # ^ S (= DATADIR).
# \. 1 / opt / data # g '/etc/init.d/mysqld

13、启动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 :::

14, change the password
using the temporary password change
[root @ yanyinglai ~] # MySQL-uroot--p
the Enter password:
.. Is available for purchase at The MySQL Monitor Commands to End with; or \ G
Your MySQL Connection the above mentioned 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

Three, mysql master-slave configuration

1, to ensure that the same data from the database with the primary database.
(1) create a database in the main library and the need to synchronize the table
[root @ yanyinglai ~] # MySQL-uroot--p
the Enter password:
.. Is available for purchase at The MySQL Monitor Commands to End with; or \ G
Your MySQL Connection the above mentioned 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.
A, creating a database Yan
MySQL> Create Database Yan;
Query the OK,. 1 Row affected (0.00 sec)
B, create a database Lisi
MySQL> Create database Lisi;
Query the OK,. 1 Row affected (0.00 sec)
C, create a database wangwu
MySQL> Create database wangwu;
Query the OK,. 1 Row affected (0.00 sec)
D, useyan database
MySQL> use Yan;
database changed
F, creating a table tom
MySQL> Create table tom (ID int Not null, name VARCHAR (100) Not null, Age tinyint);
Query the OK, 0 rows affected (11.83 sec)
G, interpolated values tom table
mysql> insert tom (id, name, Age) values (. 1, 'zhangshan', 20 is), (2, 'wangwu',. 7), (. 3, 'Lisi', 23 is);
Query the OK,. 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0
h、展示tom表
mysql> select * from tom;
+----+--------------+------+
| id | name | age |
+----+--------------+------+
| 1 | zhangshan | 20 |
| 2 | wangwu | 7 |
| 3 | lisi | 23 |
+----+---------------+-----+
3 rows in set (0.00 sec)

(2) primary database backup
requires the backup master when a terminal to open another database, a read lock on the database, else avoid inconsistency synchronized writing of data during the backup results in
a, enters MySQL
[yanyinglai the root @ ~] # MySQL - -p -uroot
the Enter password:
is available for purchase at The MySQL Monitor Commands to End with; or \ G..
Your MySQL Connection the above mentioned id IS 5
Server Version: 5.7.22 MySQL Community Community Server (the GPL)

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..
B, close all open tables and use the global read locks all tables for all databases
mysql> flush tables with read lock ;
Query the OK, 0 rows affected (0.76 sec)
// list must lock this terminal to exit (exit lock table failure) after completion of the backup
transmission c, back up to the primary database and the backup file from the library
[root @ yanyinglai ~] -uroot--p123456 the mysqldump --all-# Databases> /opt/all-20180907.sql
the mysqldump:. [Warning] a password ON the Using the Command Line interface CAN BE in the insecure
D, list the files in the directory opt
[root @ yanyinglai ~] # LS / opt /
All 20180907.sql Data-
G, copy / all-20180907.sql file in the directory to 192.168.1.201 opt / opt directory
[root @ yanyinglai ~] # scp / opt / all-20180907. [email protected] SQL: / opt /
ECDSA Key Fingerprint IS SHA256: 7mLj77SFk7sPkhjpMPfdK3nZ98hOuyP4OKzjXeijSJ0.
ECDSA key fingerprint is MD5:a0:1b:eb:7f:f0:b6:7b:73:97:91:4c:f3:b1:89:d8:ea.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.55.129' (ECDSA) to the list of known hosts.
[email protected]'s password:
all-20180907.sql 100% 784KB 783.3KB/s 00:01

h, lifting the main library table lock status, you can exit the interactive interface

mysql> quit
Bye

(3) and viewed in the main library backup and recovery from the library are consistent with the data in the main library

[root@yanyinglai ~]# mysql -uroot -p123456 < /opt/all-20180907.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@yanyinglai ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
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. 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lisi |
| mysql |
| performance_schema |
| sys |
| wangwu |
| yan |
+--------------------+
7 rows in set (0.18 sec)

mysql> use yan;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Changed Database
MySQL> from the SELECT * tom;
+ ---- + ------ + ----------- +
| the above mentioned id | name | Age |
+ ---- + --- + ------ + --------
|. 1 | zhangshan | 20 is |
| 2 | wangwu |. 7 |
|. 3 | Lisi | 23 is |
+ ---- + ------- + ------ + ----
3 rows in the SET (0.06 sec)
(4) create a database in the primary synchronization account authorization to use data from
[root @ yanyinglai ~] # MySQL-uroot--p
the Enter password:
.. is available for purchase at The MySQL Monitor Commands to End with; or \ G
Your MySQL Connection the above mentioned id IS 7
Server Version: 5.7.22 MySQL Community Community Server (the GPL)

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 the Clear the INPUT of Statement at The Current.
A, create a user repl, password
mysql> create user 'repl'@'192.168.1.201' identified by '123456 ';
Query the OK, 0 rows affected (5.50 sec)
B, granting permission to copy from the server 192.168.1.201
MySQL> grant replication Slave ON . to' repl'@'192.168.1.200 ';
Query the OK, 0 rows affected (0.04 sec)
c, the refresh permissions granted
MySQL> the flush privileges;
Query the OK, 0 rows affected (0.09 sec)
(. 5) arranged to edit the primary database configuration file
[yanyinglai the root @ ~] # Vim /etc/my.cnf
[yanyinglai the root @ ~] # /etc/my.cnf CAT
[mysqld]
the basedir = / usr / local / MySQL base path ###
datadir = / opt / data ### data directory
socket = / tmp / mysql.sock ### interface path
port = 3306 ### port
pid-file = /opt/data/mysql.pid ## pid file path to
the User = MySQL
Skip-name-Resolve
// add the following
log-bin = mysql-bin // enable binlog log
server-id = 1 // Main unique identifier of the primary database server to be necessary from the large ratio of
log-error = / opt / data / mysql.log // error log path
(6) to restart the service mysql
[yanyinglai the root @ ~] # mysqld the restart-service
Shutting Down the mySQL .. SUCCESS ...!
Starting MySQL.Logging to '/opt/data/mysql.log'.
............................. ! .. SUCCESS
(. 7) lists all open network connection port
[yanyinglai the root @ ~] # SS -antl
State Recv-Q-Q the Send the Local the Address: the Peer port the Address: port
the LISTEN 0 128 : 22 is : the LISTEN 0 127.0 100 .0.1: 25 : the LISTEN 0 128 ::: ::: 22 is the LISTEN 0 100 ::. 1: 25 :::



3306 0 80 ::: ::: the LISTEN

(. 8) to view the status of the primary database
MySQL> Show Master Status;
+ ------ + ------------------ ---- ----------- + -------------- + ------------------ + + --------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+ ------------------ + ---------- + ------------------ + -------------- + --------------- + ----
| MySQL-bin.000001 | 154 | | | |
+ ------------------ + ---------- + - + ------------ + ------------------ ------------------ - +
. 1 in Row SET (0.00 sec)
(. 9) from a configuration database
editing configuration files
[yanyinglai the root @ ~] CAT # /etc/my.cnf
[mysqld]
the basedir = / usr / local / MySQL
DATADIR = / opt / Data
= /tmp/mysql.sock Socket
Port = 3306
PID = File-/opt/data/mysql.pid
= MySQL User
Skip-name-Resolve
// add the following:
Server-ID = 2 // unique identifier provided from the library to be smaller than the main
= mysql-relay-bin // enable relay relay relay-log log log
error-log = / opt / Data / mysql.log
(10) to restart the service from the mysql
[yanyinglai the root @ ~] # mysqld the restart-service
Shutting Down the mySQL .. SUCCESS!
Starting the mySQL .. SUCCESS!
[the root ~ @ yanyinglai ] -antl SS #
State Recv-Q-Q the Send the Local the Address: the Peer Port the Address: Port
the LISTEN 0 128 : 22 is : the LISTEN 0 100 127.0.0.1:25 : the LISTEN 0 128 ::: ::: 22 is the LISTEN 0 :: 100 ::: 1:25 the LISTEN 0 3306 80 ::: ::: (. 11) disposed on and start the copy from MySQL> Change to master -> MASTER_HOST = '192.168.1.200', -> MASTER_USER = 'the repl',








-> master_password='123456',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.28 sec)
(13)查看从服务器状态
mysql> show slave status\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.55.130
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //此处必须是yes
Slave_SQL_Running: Yes //此处必须是yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 527
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 5abf1791-b2af-11e8-b6ad-000c2980fbb4
Master_Info_File: /opt/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

Fourth, testing and validation insert data tom table yan library of the master server:
(1) insert data tom table yan library
MySQL> use yan;
Reading Table Information for Completion of Table and column names
by You CAN Turn OFF the this Feature to get a quicker startup with -A

Database changed
mysql> select * from tom;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | wangwu | 7 |
| 3 | lisi | 23 |
+----+-----------+------+
3 rows in set (0.09 sec)

mysql> insert tom(id,name,age) value (4,"yyl",18);
Query OK, 1 row affected (0.14 sec)

MySQL> from the SELECT * tom;
+ ---- + ------ + ----------- +
| the above mentioned id | name | Age |
+ ---- + ----- + ------ + ------
|. 1 | zhangshan | 20 is |
| 2 | wangwu |. 7 |
|. 3 | Lisi | 23 is |
|. 4 | YYL | 18 is |
+ ---- + - + ------ + ---------
. 4 in rows SET (0.00 sec)
(2) to see if the data synchronization from the database
MySQL> Yan use;
Reading Table Information for Completion of Table names and column
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tom;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | wangwu | 7 |
| 3 | lisi | 23 |
| 4 | yyl | 18 |
+----+-----------+------+
4 rows in set (0.00 sec)

Guess you like

Origin blog.51cto.com/14413105/2437997