11mysql主从
1.主从简介
1.1主从作用
1.2主从形式
2.主从复制原理
3.主从复制配置
MySQL主从配置操作代码:
先查看主从库中有哪些数据库
主库:
[root@localhost ~]# mysql -uroot -pwhb123! -e ‘show databases‘
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
从库:
[root@cong ~]# mysql -uroot -pwhb123! -e ‘show databases‘
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@localhost ~]# mysql -uroot -pwhb123!
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 6
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.00 sec)
[root@localhost ~]# mysqldump -uroot -pwhb123! --all-databases > /opt/all-202005071955.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
//把数据库备份到从库/opt/all-数字最好是当时的日期时间.sql
[root@localhost ~]# ls /opt
all-202005071955.sql data
[root@localhost ~]# scp /opt/all-202005071955.sql root@192.168.86.142:/opt/
The authenticity of host ‘192.168.86.142 (192.168.86.142)‘ can‘t be established.
ECDSA key fingerprint is SHA256:Kndb+VIyoDEqXuzuGh2ZptSYxcbHbHjg4+KSZqKaRLE.
ECDSA key fingerprint is MD5:4b:04:cd:36:f8:e8:85:d9:f2:ce:6c:de:c4:5f:41:61.
Are you sure you want to continue connecting (yes/no)? y
Please type ‘yes‘ or ‘no‘: yes
Warning: Permanently added ‘192.168.86.142‘ (ECDSA) to the list of known hosts.
root@192.168.86.142‘s password:
all-202005071955.sql 100% 773KB 12.8MB/s 00:00
//在从库上恢复主库的备份并查看,与主库保持一致
[root@cong ~]# ls /opt
all-202005071955.sql data
[root@cong ~]# mysql -uroot -pwhb123! < /opt/all-202005071955.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@cong ~]# mysql -uroot -pwhb123! -e ‘show databases;‘
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//在主库里面创建一个同步账号授权给从数据库
mysql> create user ‘whb‘@‘192.168.86.142‘ identified by ‘whb123!‘ ;
Query OK, 0 rows affected (0.05 sec)
mysql> grant replication slave on *.* to ‘whb‘@‘192.168.86.142‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
//配置主库:
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
log-bin=mysql-bin //启用binlog日志
server-id=1 //数据库服务器唯一标识符,主库的值一定要大于从库
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file = /var/run.mysqld/mysqld.pid
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# ss -anlt
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@cong ~]# vim /etc/my.cnf
[mysqld]
datadir = /var/lib/mysql
socket = /var/lib/mysql.sock
server-id=2 //设置从库的唯一标识符,从库的值必须小于主库
relay-log=mysql-relay-bin //启动中继日志
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@cong ~]# systemctl restart mysqld
[root@cong ~]# 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 :::*