主从数据库和读写分离的数据库集群
Posted 人间忽晚,山河以秋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了主从数据库和读写分离的数据库集群相关的知识,希望对你有一定的参考价值。
一、主从的理解
主从复制的工作机制
整体上来说,复制有3个步骤:
① Master将改变记录到二进制日志(binary log)中,这些记录叫做二进制日志事件(binary log events);
② Slave将master的binary log events拷贝到它的中继日志(relay log);
③ Slave重做中继日志中的事件,将改变反映它自己的数据。
主从复制的框架
该过程的第一部分就是Master记录二进制日志。在每个事务更新数据完成之前,Master在二进制日志记录这些改变。mysql将事务串行地写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。下一步就是Slave将master的binary log拷贝到它自己的中继日志。首先,Slave开始一个工作线程——I/O线程。I/O线程在Master上打开一个普通的连接,然后开始binlog dump线程。Binlog dump 线程从Master的二进制日志中读取事件,如果已经同步Master,它会睡眠并等待Master产生新的事件。I/O线程将这些事件写入中继日志。SQL slave thread(SQL从线程)处理该过程的最后一步。SQL线程从中继日志读取事件,并重放其中的事件而更新Slave的数据,使其与Master中的数据一致。只要该线程与I/O线程保持一致,由于中继日志通常会位于OS的缓存中,所以中继日志的开销很小。此外,在Master中也有一个工作线程:和其它MySQL的连接一样,Slave在Master中打开一个连接,也会使得Master开始一个线程。复制过程有一个很重要的限制——复制在Slave上是串行化的,也就是说Master上的并行更新操作不能在Slave上并行操作。
所以我们配置好主从后要关注的是I/O线程和SQL slave thread(SQL从线程)
二、读写分离的理解
三、实验需要的基础准备:
1、规划节点
IP | 主机名 | 节点 |
---|---|---|
10.30.59.216 | mycat | mycat中间件服务节点 |
10.30.59.219 | db1 | Mariadb数据库集群主节点 |
10.30.59.248 | db2 | Mariadb数据库集群从节点 |
2、基础准备
使用CentOS 7.2系统,flavor使用2vCPU/4G内存/50G硬盘,创建3台虚拟机进行实验。
其中2台虚拟机db1和db2部署MariaDB数据库服务,搭建主从数据库集群;一台作为主节点,负责写入数据库信息;另一台作为从节点,负责读取数据库信息。
使用一台虚拟机部署Mycat数据库中间件服务,将用户提交的读写操作识别分发给相应的数据库节点。这样将用户的访问操作、数据库的读与写操作分给3台主机,只有数据库集群的主节点接收增、删、改SQL语句,从节点接收查询语句,分担了主节点的查询压力。
Yum源使用提供的gpmall-repo文件夹作为本地源,Mycat组件使用提供的Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz压缩包安装。
四、案例事实
1、基础环境配置
(1)修改主机名
对应节点,修改主机名
Mycat节点修改主机名命令:
[root@localhost ~]# hostnamectl set-hostname mycat
[root@localhost ~]# bash
[root@mycat ~]#
db1节点修改主机名命令:
[root@localhost ~]# hostnamectl set-hostname db1
[root@localhost ~]# bash
[root@db1 ~]#
db2节点修改主机名命令:
[root@localhost ~]# hostnamectl set-hostname db2
[root@localhost ~]# bash
[root@db2 ~]#
(2)编辑hosts文件
3台集群虚拟机的/etc/hosts文件配置:
[root@mycat yum.repos.d]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.30.59.216 mycat
10.30.59.219 db1
10.30.59.248 db2
(3)所有节点关闭防火墙和iptables规则
[root@mycat opt]# systemctl stop firewalld
[root@mycat opt]# 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@mycat opt]# setenforce 0
[root@mycat opt]## iptables -F
[root@mycat opt]# iptables -X
[root@mycat opt]# iptables -Z
[root@mycat opt]# iptables-save
(4)配置Yum源
数据库集群需要安装MariaDB数据库服务,需要给集群虚拟机配置Yum安装源文件,使用gpmall-repo文件上传至mycat节点的虚拟机的/opt/下,然后设置本地Yum源
首先,把三个节点的/etc/yum.repo.d目录下的文件移动到/media下
# mv /etc/yum.repos.d/C* /media/
然后把gpmall-repo.tar.gz移到/opt/下,在解压,unzip命令如果没有可以下载(yum install unzip -y)
[root@mycat ~]# ll
total 311364
-rw-------. 1 root root 1260 Mar 29 23:35 anaconda-ks.cfg
-rw-r--r--. 1 root root 318829093 Jun 18 10:57 gpmall-repo.tar.gz
[root@mycat ~]# mv gpmall-repo.tar.gz /opt
[root@mycat ~]# cd /opt
[root@mycat opt]# ls
gpmall-repo.tar.gz
[root@mycat opt]# unzip gpmall-repo.tar.gz
在编写Yum安装源文件配置
[root@mycat opt]# vim /etc/yum.repos.d/local.repo
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
[cdrom]
name=cdrom
baseurl=file:///opt/cdrom
gpgcheck=0
enabled=1
其他节点为了方便,可以使用ftp来使用mycat节点的yum源,所以mycat节点需要安装配置ftp服务,并且配置文件加入anon_root=/opt/
[root@mycat opt]# yum install vsftpd -y
[root@mycat opt]# vi /etc/vsftpd/vsftpd.conf
# Example config file /etc/vsftpd/vsftpd.conf
anon_root=/opt/
然后开启ftp并设置开机自启
[root@mycat opt]# systemctl start vsftpd
[root@mycat opt]# systemctl enable vsftpd
db1和db2节点配置yum源
编写yum源文件
db1和db2都执行
[root@db2 ~]# vi /etc/yum.repos.d/local.repo
[cdrom]
name=cdrom
baseurl=ftp://10.30.59.216/cdrom
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=ftp://10.30.59.216/gpmall-repo
gpgcheck=0
enabled=1
在重新加载一下
[root@db2 ~]# yum clean all
已加载插件:fastestmirror
正在清理软件源: cdrom mariadb
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
[root@db2 ~]# yum repolist
已加载插件:fastestmirror
Determining fastest mirrors
cdrom | 3.6 kB 00:00:00
mariadb | 2.9 kB 00:00:00
(1/3): mariadb/primary_db | 144 kB 00:00:00
(2/3): cdrom/group_gz | 166 kB 00:00:00
(3/3): cdrom/primary_db | 3.1 MB 00:00:00
源标识 源名称 状态
cdrom cdrom 3,971
mariadb mariadb 165
repolist: 4,136
[root@db2 ~]#
(5)安装JDK环境
部署Mycat中间件服务需要先部署JDK 1.7或以上版本的JDK软件环境,这里部署JDK 1.8版本。
Mycat节点安装Java环境:
[root@mycat opt]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@mycat opt]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
2、部署MariadbDB主从数据库集群服务
(1)安装MariaDB服务
通过YUM命令在db1和db2虚拟机节点上安装Mariadb服务
yum remove mariadb mariadb-server -y
然后两节点在开启MariaDB服务,并设置开机自启
[root@db1 ~]# systemctl start mariadb
[root@db1 ~]# systemctl enable mariadb
(2)初始化Mariadb数据库
在db1和db2虚拟机节点上初始化MariaDB数据库,并设置MariaDB数据库root访问用户的密码为000000
[root@db1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): #这里输入root的密码
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] y #这里输入root的密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y #选择y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n #选择n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y #选择y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y #选择y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
(3)配置数据库集群
编辑db1和db2的数据库配置文件my.cnf,在配置文件my.cnf中添加以下内容
[root@db1 ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步MySQL系统数据库
server_id = 219 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如10.30.59.219,server_id就写219
然后重启服务
[root@db1 ~]# systemctl restart mariadb
(4)开放主节点的数据库权限
在主节点db1虚拟机上使用mysql命令登录MariaDB数据库,授权在任何客户端机器上可以以root用户登录到数据库,
然后再创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限。
在db1上执行
[root@db1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '000000';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '000000';
Query OK, 0 rows affected (0.00 sec)
(5)配置从节点db2同步主节点db1
在从节点db2虚拟机上使用mysql命令登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名db1,master_user为在步骤(4)中创建的用户user,命令如下:
[root@db2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 4
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, 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='db1',master_user='user',master_password='000000';
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
MariaDB [(none)]> slave stop;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='000000';
Query OK, 0 rows affected (0.01 sec)
我的之前出了一点小问题,只要关了slave就可以,只要在配置完主从数据库之间的连接信息之后,开启从节点服务,使用show slave status\\G; 命令并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show status slave\\G
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'slave' at line 1
MariaDB [(none)]> show slave status\\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: db1
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 526
Relay_Log_File: mariadb-relay-bin.000005
Relay_Log_Pos: 810
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: 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: 526
Relay_Log_Space: 2350
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: 216
1 row in set (0.00 sec)
如果不成功,建议卸了Mariadb服务,重新弄一边
6)验证主从数据库的同步功能
先在主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据。创建完成后,查看表company数据,如下所示
db1执行
MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use test
Database changed
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.00 sec)
MariaDB [test]> select * from company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.00 sec)
db2查看验证一下
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.02 sec)
MariaDB [(none)]> select * from test.company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.00 sec)
MariaDB [(none)]>
可以看到,在db1创建的在db2上会有。
3、部署Mycat读写分离中间件服务
(1)安装Mycat服务
将Mycat服务的二进制软件包Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz上传到Mycat虚拟机的/root目录下,并将软件包解压到/use/local目录中。赋予解压后的Mycat目录权限。
[root@mycat ~]# ls
anaconda-ks.cfg
Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-2016102820
[root@mycat ~]# chown -R 777 /usr/local/mycat
在/etc/profile系统变量文件中添加Mycat服务的系统变量,并生效变量
[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/>>/etc/profile
[root@mycat ~]# source /etc/profile
(2)编辑Mycat的逻辑库配置文件
配置Mycat服务读写分离的schema.xml配置文件在/usr/local/mycat/conf/目录下,可以在文件中定义一个逻辑库,使用户可以通过Mycat服务管理该逻辑库对应的MariaDB数据库。在这里定义一个逻辑库schema,name为USERDB;该逻辑库USERDB对应数据库database为test(在部署主从数据库时已安装);设置数据库写入节点为主节点db1;设置数据库读取节点为从节点db2。(可以直接删除原来schema.xml的内容,替换为如下。)
注意:IP需要修改成实际的IP地址。
[root@mycat ~]# vi /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="USERDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></schema>
<dataNode name="dn1" dataHost="localhost1" database="test" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql" dbDriver="native" writeType="0" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="10.30.59.219:3306" user="root" password="000000">
<readHost host="hostS1" url="10.30.59.248:3306" user="root" password="000000"/>
</writeHost>
</dataHost>
</mycat:schema>
~
~
~
~
"/usr/local/mycat/conf/schema.xml" 12L, 675C written
(3)修改配置文件权限
修改schema.xml的用户权限,命令如下:
[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml
(4)编辑mycat的访问用户
修改/usr/local/mycat/conf/目录下的server.xml文件,修改root用户的访问密码与数据库,密码设置为123456,访问Mycat的逻辑库为USERDB,命令如下:
[root@mycat ~]# cat /usr/local/mycat/conf/server.xml
在配置文件的最后部分,
<user name="root">
<property name="password">123456</property>
<property name="schemas">USERDB</property>
然后删除如下几行:
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
像这个样子
(5)启动Mycat服务
通过命令启动Mycat数据库中间件服务,启动后使用netstat -ntpl命令查看虚拟机端口开放情况,如果有开放8066和9066端口,则表示Mycat服务开启成功。端口查询。
[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
Starting Mycat-server...
Mycat-server is already running.
[root@mycat ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 6114/java
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1080/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1386/master
tcp6 0 0 :::1984 :::* LISTEN 6114/java
tcp6 0 0 :::8066 :::* LISTEN 6114/java
tcp6 0 0 :::9066 :::* LISTEN 6114/java
tcp6 0 0 :::33168 :::* LISTEN 6114/java
tcp6 0 0 :::46130 :::* LISTEN 6114/java
tcp6 0 0 :::21 :::* LISTEN 2378/vsftpd
tcp6 0 0 :::22 :::* LISTEN 1080/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1386/master
4、验证数据库集群服务读写分离功能
(1)用Mycat服务查询数据库信息
先在Mycat虚拟机上使用Yum安装mariadb-client服务
[root@mycat ~]# yum install -y MariaDB-client
在Mycat虚拟机上使用mysql命令查看Mycat服务的逻辑库USERDB,因为Mycat的逻辑库USERDB对应数据库test(在部署主从数据库时已安装),所以可以查看库中已经创建的表company。
[root@mycat ~]# mysql -h127.0.0.1 -P8066 -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| USERDB |
+----------+
1 row in set (0.004 sec)
MySQL [(none)]> use USERDB
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 [USERDB]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| company |
+----------------+
1 row in set (0.003 sec)
MySQL [USERDB]> select * from company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.048 sec)
(2)用Mycat服务添加表数据
在Mycat虚拟机上使用mysql命令对表company添加一条数据(2,“basketball”,“usa”),添加完毕后查看表信息。
MySQL [USERDB]> insert into company values(2,"bastetball","usa");
Query OK, 1 row affected (0.014 sec)
MySQL [USERDB]> select * from company;
+----+------------+------+
| id | name | addr |
+----+------------+------+
| 1 | facebook | usa |
| 2 | bastetball | usa |
+----+------------+------+
2 rows in set (0.002 sec)
(3)验证Mycat服务对数据库读写操作分离
在Mycat虚拟机节点使用mysql命令,通过9066端口查询对数据库读写操作的分离信息。可以看到所有的写入操作WRITE_LOAD数都在db1主数据库节点上,所有的读取操作READ_LOAD数都在db2主数据库节点上。由此可见,数据库读写操作已经分离到db1和db2节点上了。
[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p000000 -e 'show @@datasource;'
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| dn1 | hostM1 | mysql | 10.30.59.219 | 3306 | W | 0 | 10 | 1000 | 3848 | 0 | 1 |
| dn1 | hostS1 | mysql | 10.30.59.248 | 3306 | R | 0 | 9 | 1000 | 3853 | 8 | 0 |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
Mycat读写分离数据库完成
以上是关于主从数据库和读写分离的数据库集群的主要内容,如果未能解决你的问题,请参考以下文章