简介:
主从复制的工作机制:
① Master将改变记录到二进制日志(binary log)中,这些记录叫做二进制日志事件(binary log events);
② Slave将master的binary log events拷贝到它的中继日志(relay log);
③ Slave重做中继日志中的事件,将改变反映它自己的数据。
读写分离概念:
基本原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。
读写分离的作用:
可以解决数据库写入时影响查询效率的问题
构建读写分离的数据库
ip | 主机名 / 节点 |
---|---|
10.30.59.193 | mycat / Mycat中间件服务节点 |
10.30.59.194 | db1 / MariaDB数据库集群主节点 |
10.30.59.195 | db2 / MariaDB数据库集群从节点 |
一、基础环境配置
(一)、修改主机名
hostnamectl set-hostname mycat
hostnamectl set-hostname db1
hostnamectl set-hostname db2
(二)、 所有节点编辑hosts节点
cat >>/etc/hosts << EOF
10.30.59.193 mycat
10.30.59.194 db1
10.30.59.195 db2
EOF
(三)、所有节点关闭防火墙
#关闭selinx
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config #关闭firewalld
systemctl stop firewalld && systemctl disable firewalld#关闭iptables
iptables -F
iptables -X
iptables -Z
iptables-save
(四)、所有节点配置Yum源
将gpmall-repo文件上传至虚拟机的/opt目录下
#移走yum源
mv /etc/yum.repos.d/* /media/#建立镜像挂载点并挂载镜像
mkdir /opt/centos
mount /dev/sr0 /opt/centos/ #配置yum源
cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
(五)、mycat节点安装JDK环境
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel[root@mycat ~]# 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)
二、部署MariaDB主从数据库集群服务
(一)、db1节点和db2节点安装mariadb服务
yum install -y mariadb mariadb-server
(二)、db1节点和db2节点启动mariadb服务
systemctl start mariadb
systemctl enable mariadb
(三)、db1节点和db2节点初始化MariaDB数据库
[root@db1 ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS 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): #回车
OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.Set root password? [Y/n] y
New password: #输入数据库root密码000000
Re-enter new password: #重复输入密码000000
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... 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... 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- 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... 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!
(四)、配置数据库集群主节点
在配置文件/etc/my.cnf
中增添下面的内容:
[mysqld]
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步MySQL系统数据库
server_id = 194 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
注意:编译完成配置文件,重启mariadb服务
[root@db1 ~]# systemctl restart mariadb
(五)、配置主节点的数据库权限
[root@db1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.18-MariaDB-log MariaDB ServerCopyright (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)]> grant all privileges on *.* to root@'%' identified by '000000'; #授权在任何客户端机器上可以以root用户登录到数据库
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> grant replication slave on *.* to user@'db2' identified by '000000'; #创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限
Query OK, 0 rows affected (0.001 sec)
(六)、配置从节点的数据库权限
[root@db2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.18-MariaDB MariaDB ServerCopyright (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='db1',master_user='user',master_password='000000'; #配置从节点连接主节点的连接信息
Query OK, 0 rows affected (0.052 sec)MariaDB [(none)]> start slave; #开启从节点服务
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> show slave status\G #查看从节点服务状态
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: db1Master_User: userMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 699Relay_Log_File: db2-relay-bin.000002Relay_Log_Pos: 998Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 699Relay_Log_Space: 1305Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 194Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: NoGtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservativeSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itSlave_DDL_Groups: 2
Slave_Non_Transactional_Groups: 0Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
(七)、验证主从数据库的同步功能
`主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据`MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.001 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.014 sec)MariaDB [test]> insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.003 sec)MariaDB [test]> select * from company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.000 sec)`在从节点db2查询test数据库与表company`
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.002 sec)MariaDB [(none)]> select * from test.company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.001 sec)可以查询到信息,说明主从数据库集群正常运行。
三、部署Mycat读写分离中间件服务
(一)、安装mycat服务
软件包Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz上传到mycat的/节点root目录下
[root@mycat ~]# tar -zxf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local
[root@mycat ~]# chmod -R 777 /usr/local/mycat/`在/etc/profile系统变量文件中添加Mycat服务的系统变量,并生效变量`
[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile
(二)、编辑Mycat的逻辑库配置文件
[root@mycat ~]# cat >/usr/local/mycat/conf/schema.xml << EOF
> <?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> #定义一个逻辑库schema,name为USERDB
> <dataNode name="dn1" dataHost="localhost1" database="test" /> #该逻辑库USERDB对应数据库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.194:3306" user="root" password="000000"> #设置数据库写入节点为主节点db1
> <readHost host="hostS1" url="10.30.59.195:3306" user="root" password="000000" /> #设置数据库读取节点为从节点db2
> </writeHost>
> </dataHost>
> </mycat:schema>
> EOF
sqlMaxLimit:配置默认查询数量。
database:为真实数据库名。
balance="0":不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
balance="1":全部的readHost与stand by writeHost参与select语句的负载均衡,简单来说,当双主双从模式(M1->S1,M2->S2,并且M1与M2互为主备),正常情况下,M2、S1、S2都参与select语句的负载均衡。
balance="2":所有读操作都随机的在writeHost、readhost上分发。
balance="3":所有读请求随机地分发到wiriterHost对应的readhost执行,writerHost不负担读压力,注意balance=3只在1.4及其以后版本有,1.3版本没有。
writeType="0":所有写操作发送到配置的第一个writeHost,第一个挂了需要切换到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件dnindex.properties中。
writeType="1":所有写操作都随机的发送到配置的writeHost。
(三)、修改Mycat的逻辑库配置文件权限
[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml
(四)、编辑mycat的访问用户
修改/usr/local/mycat/conf/server.xml
配置文件
`修改root用户的访问密码与数据库,密码设置为123456,访问Mycat的逻辑库为USERDB`····································
····························<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>
(五)启动Mycat服务
[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
使用netstat -ntpl命令查看虚拟机端口开放情况,如果有开放8066和9066端口,则表示Mycat服务开启成功。
四、验证数据库集群服务读写分离功能
(一)、在mycat节点上安装MariaDB-client服务。
yum install -y MariaDB-client
(二)、在Mycat节点使用mysql命令查看Mycat服务的逻辑库USERDB
[root@mycat ~]# mysql -h127.0.0.1 -P8066 -uroot -p123456
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.002 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 -ADatabase changed
MySQL [USERDB]> show tables; # 查看所有表
+----------------+
| Tables_in_test |
+----------------+
| company |
+----------------+
1 row in set (0.002 sec)MySQL [USERDB]> select * from company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.055 sec)
(三)、用Mycat服务添加表数据
MySQL [USERDB]> insert into company values(2,"bastetball","usa");
Query OK, 1 row affected (3.541 sec)MySQL [USERDB]> select * from company;
+----+------------+------+
| id | name | addr |
+----+------------+------+
| 1 | facebook | usa |
| 2 | bastetball | usa |
+----+------------+------+
2 rows in set (0.002 sec)
(四)、验证Mycat服务对数据库读写操作分离
在mycat节点使用mysql命令,通过9066端口查询对数据库读写操作的分离信息。
[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show @@datasource;'