mysql主从库配置读写分离以及备份

1,什么是读写分离?
其实就是将数据库分为了主从库,一个主库用于写数据,多个从库完成读数据的操作,
主从库之间通过某种机制进行数据的同步,是一种常见的数据库架构。
一个组从同步集群,通常被称为是一个“分组”。

2,数据库分组架构解决什么问题?

大多数互联网业务,往往读多写少,这时候,数据库的读会首先称为数据库的瓶颈,
这时,如果我们希望能够线性的提升数据库的读性能,消除读写锁冲突从而提升数据库的写性能,那
么就可以使用“分组架构”(读写分离架构)。
用一句话概括,读写分离是用来解决数据库的读性能瓶颈的。


3,什么是数据库水平切分?
数据库水平切分,也是一种常见的数据库架构,是一种通过算法,将数据库进行分割的架构。
一个水平切分集群中的每个数据库,通常称为一个“分片”。
每一个分片中的数据没有重合,所有分片中的数据并集组成全部数据。

步骤一:搭建MySQL一主一从同步结构

1)配置主服务器192.168.4.51

]# vim /etc/my.cnf
[mysqld]
server_id=51 //指定服务器ID号
log-bin=master51 //启用binlog日志,并指定文件名前缀
...
[root@master10 ~]# systemctl restart mysqld //重启mysqld


2)主服务器授权用户,并查看binlog日志信息

]# mysql -uroot -p123456
mysql> grant all on *.* to 'repluser'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 | 449 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


3)配置从服务器192.168.4.52

]# vim /etc/my.cnf

[mysqld]

server_id=52 //指定服务器ID号,不要与Master的相同

:wq

]# systemctl restart mysqld


4)配置从服务器192.168.4.52,指定主服务器信息,日志文件、偏移位置(参考MASTER上的状态输出)

]# mysql -uroot -p123456
mysql> change master to master_host='192.168.4.51',
-> master_user='repluser',
-> master_password='123456',
-> master_log_file='master51.000001',
-> master_log_pos=449;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave statusG;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000001
Read_Master_Log_Pos: 738
Relay_Log_File: slave20-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000001
Slave_IO_Running: Yes //IO线程YES
Slave_SQL_Running: Yes //SQL线程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: 738
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: 10
Master_UUID: 95ada2c2-bb24-11e8-abdb-525400131c0f
Master_Info_File: /var/lib/mysql/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)


5)测试配置,在主服务器本机创建数据库 aa库

]# mysql –uroot –p123456
mysql> create database aa;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aa |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)


6)从服务器上查看,有aa库

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aa |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

备份方式:
冷备份:拷贝数据库目录,需要先停机再备份,对于在线不间断提供业务的不适用
逻辑备份: mysqldump工具,单线程备份,备份速度较慢;mydumper工具,mysqldump升级版,有限制条件

备份单个库:
备份:mysqldump -uroot -p dbname1> 1.sql
还原:mysql -uroot -p dbname1 < 1.sql

备份多个库:
备份:mysqldump -uroot -p --database db1 db2 > 1.sql
还原: mysql -uroot -p < 1.sql


备份全部库:
备份:mysqldump -uroot -p --all-databases > 1.sql
还原:mysql -uroot -p < 1.sql

备份单个表:
备份:mysqldump dbname1 tb1 > 1.sql
还原: mysql -uroot -p dbname1 tb1 < 1.sql

原文地址:https://www.cnblogs.com/zgqbky/p/11656377.html