标签:root add SHA256 性能 form 位置 主服务器 中继 org
MySQL主从复制虽好,能完美解决数据库单点问题吗?一、单个数据库服务器的缺点
二、如何解决单点问题
三、MySQL主从复制架构
1、主库将变更写入到主库的binlog中
2、从库的IO线程在指定位置读取主库binlog内容存储到本地的中继日志(Relay Log)中
可以使用mysqlbinlog来读取relay log中的内容。
3、从库的SQL线程读取Relay Log日志中的内容,并在从库中重放
SQL线程所执行的事件,我们可以通过配置选项来决定是否要写入到从服务器的二进制日志中。
目前MySQL支持两种复制类型:
虽然主从复制增加了一个数据库副本,但从数据库和主数据库的数据最终会是一致的。之所以说是最终一致,因为MySQL复制是异步的,正常情况下主从复制数据之间会有一个微小的延迟。
通过这个数据库副本看似解决了数据库单点问题,但并不完美:因为这种架构下,如果主服务器宕机,需要手动切换从服务器,业务中断不能忍受,不能满足应用高可用的要求。
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
1.2 主从形式
主从复制步骤:
主从复制配置步骤:
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:Red Hat Enterprise Linux 8.0 (Ootpa)
分别在主从两台机上二进制安装mysql-5.7版本,此处略过安装步骤
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//查看数据库
主库的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wlw |
| www |
+--------------------+
6 rows in set (0.00 sec)
从库的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
//全备主库
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
//备份主库并将备份文件传送到从库
[root@zhu ~]# mysqldump -uroot -pw --all-databases > /opt/all-202005071900.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@zhu ~]# ls /opt/
1 all-202005071900.sql data
[root@zhu ~]# scp /opt/all-202005071900.sql root@192.168.66.128:/opt
The authenticity of host ‘192.168.66.128 (192.168.66.128)‘ can‘t be established.
ECDSA key fingerprint is SHA256:lpKqeNs7QFkySWsYsJ1IMnidcmZotljVvyB/y1YuWW4.
ECDSA key fingerprint is MD5:ad:d8:3d:e7:7e:aa:fd:07:79:3c:7f:ca:82:0c:42:2f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.66.128‘ (ECDSA) to the list of known hosts.
root@192.168.66.128‘s password:
all-202005071900.sql 100% 774KB 47.5MB/s 00:00
//在从库上恢复主库的备份并查看,与主库保持一致
[root@cong ~]# ls /opt/
all-202005071900.sql data
[root@cong ~]# mysql < /opt/all-202005071900.sql
[root@cong ~]# mysql -e ‘show databases;‘
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wlw |
| www |
+--------------------+
//在主库里面创建一个同步账号授权给从数据库
mysql> create user ‘wlw‘@‘192.168.66.128‘ identified by ‘w‘ ;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to ‘wlw‘@‘192.168.66.128‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
//配置主库
[root@zhu ~]# vim /etc/my.cnf
[mysql]
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 //数据库服务器唯一的标识符,主库的值一定要大于从库
relay-log=mysql-relay-bin
symbolic-links=0
log-error=/var/log/mysqld.log
//重启mysql服务
[root@zhu ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
Failed to restart mysqld.service: Unit not found.
[root@zhu ~]# 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@zhu ~]# systemctl stop firewalld
[root@zhu ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@zhu ~]# vim /etc/selinux/config
[root@zhu ~]# setenforce 0
//配置从库
[root@cong ~]# vim /etc/my.cnf
[mysql]
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=2 //数据库服务器唯一的标识符,主库的值一定要大于从库
relay-log=mysql-relay-bin
symbolic-links=0
log-error=/var/log/mysqld.log
//重启mysql服务
[root@cong ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@cong ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
[root@cong ~]# systemctl stop firewalld
[root@cong ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@cong ~]# vim /etc/selinux/config
[root@cong ~]# setenforce 0
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.66.128‘,
-> MASTER_USER=‘wlw‘,
-> MASTER_PASSWORD=‘w‘,
-> MASTER_LOG_FILE=‘mysql-bin.000001‘,
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.33 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
//查看服务器状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.66128
Master_User: wlw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-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:
//验证
在主库表中创建内容
mysql> use wlw;
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 w;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | q | 20 |
| 2 | w | 23 |
| 3 | e | 25 |
| 4 | r | 28 |
+----+------+------+
4 rows in set (0.00 sec)
mysql> insert into w values (5,‘sean‘,20),(6,‘tom‘,23),(7,‘jerry‘,30);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from w;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | q | 20 |
| 2 | w | 23 |
| 3 | e | 25 |
| 4 | r | 28 |
| 5 | sean | 20 |
| 6 | tom | 23 |
| 7 | jerry | 30 |
+----+-------+------+
7 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
在从库查看是否同步
mysql> select * from w;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | q | 20 |
| 2 | w | 23 |
| 3 | e | 25 |
| 4 | r | 28 |
| 5 | sean | 20 |
| 6 | tom | 23 |
| 7 | jerry | 30 |
+----+-------+------+
7 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
标签:root add SHA256 性能 form 位置 主服务器 中继 org
原文地址:https://blog.51cto.com/14736606/2493318