mysql主从复制 灵活
一主一从
主主复制
一主多从—扩展系统读取的性能,因为读是在从库读取的;
多主一从—5.7开始支持
联级复制—
用途及条件
mysql主从复制用途
实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务
主从部署必要条件:
主库开启binlog日志(设置log-bin参数)
主从server-id不同
从库服务器能连通主库
主从原理
从库生成两个线程,一个I/O线程,一个SQL线程;
i/o线程去请求主库 的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中;
主库会生成一个 log dump 线程,用来给从库 i/o线程传binlog;
SQL 线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,而最终数据一致;
问题及解决方法
mysql主从复制存在的问题:
主库宕机后,数据可能丢失
从库只有一个sql Thread,主库写压力大,复制很可能延时
解决方法:
半同步复制—解决数据丢失的问题
并行复制----解决从库复制延迟的问题
配置
角色 | 版本 | ip |
---|---|---|
master主 | rhel7.0 | 192.168.118.112 |
master备 | rhel7.4 | 192.168.118.222 |
安装(两台服务器的安装是一样的)
#先关闭防火墙和selinux
yum -y install mariadb mariadb-server
#启动
systemctl restart mariadb
#改密码为123
mysqladmin -uroot password 123
#进入数据库
mysql -uroot -p123
主服务器配置
创建用户授权
MariaDB [(none)]> create user 'xsb'@'192.168.118.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to 'xsb'@'192.168.118.%';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
修改配置文件
[root@mysql ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-bin=mysql-bin #启用binlog日志
server-id=1 #数据库服务器唯一标识符,主库的server-id的值必须比从库的小
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
重启服务,查看主库状态
[root@mysql ~]# systemctl restart mariadb
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 245 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
修改从库的配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
relay-log=mysql-relay-bin #启用中继日志relay-log
server-id=2 #设置从库的唯一标识符,从库的值大于主库的值
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
重启从服务器
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#配置并启动主从复制
MariaDB [(none)]> change master to
-> master_host='192.168.118.112',
-> master_user='xsb',
-> master_password='123456',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
#查看服务器状态
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.118.112
Master_User: xsb
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 245
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 529
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: 245
Relay_Log_Space: 823
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
1 row in set (0.00 sec)
在主服务器上创建xsb1数据库,在xsb1里面新建xsb2表,在表中插入内容
MariaDB [(none)]> create database xsb1;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use xsb1;
Database changed
MariaDB [xsb1]> create table xsb2 (id int(11),name varchar(11));
Query OK, 0 rows affected (0.00 sec)
MariaDB [xsb1]> insert into xsb2 values (1,'xsb3'),(2,'xsb4');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
在备服务器上查看
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| xsb1 |
+--------------------+
5 rows in set (0.01 sec)
MariaDB [(none)]> use xsb1;
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
MariaDB [xsb1]> show tables;
+----------------+
| Tables_in_xsb1 |
+----------------+
| xsb2 |
+----------------+
1 row in set (0.00 sec)
MariaDB [xsb1]> desc xsb2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)