标签:replicate 备份 sha start lock select 使用 内容 RoCE
主从介绍主从过程大概有3个步骤
数据的高可用。(主:作为读写数据,从:实时同步,当主宕机时,从 也可以及时提供服务)
数据的负载均衡。(客户从 从 这台机器上读取数据(但是不
从库生成两个线程,一个是I/O线程,另一个是SQL线程
搭建两台MYSQL
服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
数据库角色 | IP | 应用与系统 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.24.18 | centos7 mysql-5.7 | 有 |
从数据库 | 192.168.24.129 | centos7 mysql-5.7 | 无 |
具体配置查看《mysql进阶简单解析》
先在主数据库创建所需要同步的库和表
[root@linfan ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
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> create database doudou;
Query OK, 1 row affected (0.03 sec)
mysql> create database job;
Query OK, 1 row affected (0.00 sec)
mysql> create database mary;
Query OK, 1 row affected (0.00 sec)
mysql> use doudou;
Database changed
mysql> create table tom (id int not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.22 sec)
mysql> insert tom(id,name,age) values(1,"lisi",6),(2,"waangwu",5),(3,"zhaotie",9);
Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from tom;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 1 | lisi | 6 |
| 2 | waangwu | 5 |
| 3 | zhaotie | 9 |
+----+---------+------+
3 rows in set (0.01 sec)
mysql>
备份主库
备份主库时需要另开一个终端,给数据库上读锁,避免在备份期间有其他人在写入导致数据同步的不一致
[root@linfan ~]# 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. 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> flush tables with read lock;
Query OK, 0 rows affected (0.17 sec)
mysql> //此锁表的终端必须在备份完成以后才能退出(退出锁表失效)
备份主库并将备份文件传送到从库
[root@linfan ~]# mysqldump -uroot -plinfan123 --all-databases >/opt/all-20180907.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@linfan ~]# ls /opt/
all-20180907.sql data
[root@linfan ~]# scp /opt/all-20180907.sql root@192.168.24.130:/opt/
The authenticity of host ‘192.168.24.130 (192.168.24.130)‘ can‘t be established.
ECDSA key fingerprint is SHA256:w+sgREnQRuhBiqS0qL9wlAImCSmvSQ6KnNqW6N3znJ0.
ECDSA key fingerprint is MD5:f0:fd:ea:c7:97:83:f0:b0:03:84:d2:a6:0a:23:12:e0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.24.130‘ (ECDSA) to the list of known hosts.
root@192.168.24.130‘s password:
all-20180907.sql 100%
解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
在从库上恢复主库的备份并查看是否与主库的数据保持一致
[root@linfan ~]# mysql -uroot -plinfan123 < /opt/all-20180907.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@linfan ~]# mysql -uroot -plinfan123
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 |
| doudou |
| job |
| mary |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.01 sec)
mysql> use doudou
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from tom;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 1 | lisi | 6 |
| 2 | waangwu | 5 |
| 3 | zhaotie | 9 |
+----+---------+------+
3 rows in set (0.00 sec)
[root@linfan ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
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> create user ‘repl‘@‘192.168.24.130‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.04 sec)
mysql> grant replication slave on *.* to ‘repl‘@‘192.168.24.130‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
编辑配置文件
[root@linfan ~]# vim /etc/my.cnf
[root@linfan ~]# cat /etc/my.cnf
[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
//添加以下内容
log-bin=mysql-bin //启用binlog日志
server-id=1 //主数据库服务器唯一标识符 主的必须必从大
log-error=/opt/data/mysql.log
重启mysql服务
[root@linfan ~]# service mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.Logging to ‘/opt/data/mysql.log‘.
.......... SUCCESS!
[root@linfan ~]# ss -natl
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
查看主库的状态(里面的数据要记住,待会会用到)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
编辑配置文件
[root@linfan ~]# cat /etc/my.cnf
[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
//添加以下内容:
server-id=2 //设置从库的唯一标识符 从的必须比主小
relay-log=mysql-relay-bin //启用中继日志relay log
error-log=/opt/data/mysql.log
重启从库的mysql服务
[root@linfan ~]# service mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.Logging to ‘/opt/data/mysql.log‘.
.......... SUCCESS!
[root@linfan ~]# ss -natl
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
配置并启动主从复制
mysql> change master to
-> master_host=‘192.168.24.128‘,
-> master_user=‘repl‘,
-> master_password=‘123456‘,
-> master_log_file=‘mysql-bin.000001‘,
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看从服务器状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.24.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: linfan-relay-bin.000002
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: 528
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: 3396fe35-b1e1-11e8-81bb-000c292340f6
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)
mysql> use doudou;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from tom;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 1 | lisi | 6 |
| 2 | waangwu | 5 |
| 3 | zhaotie | 9 |
+----+---------+------+
3 rows in set (0.00 sec)
mysql> insert tom(id,name,age) value (4,"mary",18);
Query OK, 1 row affected (0.10 sec)
mysql> select * from tom;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 1 | lisi | 6 |
| 2 | waangwu | 5 |
| 3 | zhaotie | 9 |
| 4 | mary | 18 |
+----+---------+------+
4 rows in set (0.00 sec)
mysql> use doudou;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from tom;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 1 | lisi | 6 |
| 2 | waangwu | 5 |
| 3 | zhaotie | 9 |
| 4 | mary | 18 |
+----+---------+------+
4 rows in set (0.00 sec)
标签:replicate 备份 sha start lock select 使用 内容 RoCE
原文地址:http://blog.51cto.com/13858192/2171613