mysql 读写分离

数据库主从加读写分离实操手册

1.实战案例——部署主从数据库

1.1 案例目标

(1)了解数据库服务的安装与配置。

(2)了解主从数据库集群的配置架构。

(3)了解主从数据库的使用。

1.2 案例分析

1.规划节点

数据库主从案例的节点规划,见表1-1-1。

表1-1-1 节点规划

IP

主机名

节点

172.30.11.12

mysql1

主数据库节点

172.30.11.13

mysql2

从数据库节点

2.基础准备

使用OpenStack平台创建两台云主机进行实验,云主机镜像使用提供的CentOS_7.5_x86_64_XD.qcow2镜像,flavor使用1核/2G内存/20G硬盘,自行配置网络并使用远程连接工具连接云主机。节点规划表中的IP地址为作者的IP地址,在进行实操案例的时候,按照自己的环境规划网络与IP地址。

1.3 案例实施

1.基础环境安装

(1)修改主机名

使用远程连接工具CRT连接到172.30.11.12、172.30.11.13这两台虚拟机,并对这两台虚拟机进行修改主机名的操作,172.30.11.12主机名修改为mysql1;172.30.11.13主机名修改为mysql2。命令如下:

mysql1节点:

[root@localhost ~]# hostnamectl set-hostname mysql1

[root@localhost ~]# logout

[root@mysql1 ~]# hostnamectl

   Static hostname: mysql1

         Icon name: computer-vm

           Chassis: vm

        Machine ID: 622ba110a69e24eda2dca57e4d306baa

           Boot ID: 859720a14f8f4b5e836f5a0fae7097e0

    Virtualization: kvm

  Operating System: CentOS Linux 7 (Core)

       CPE OS Name: cpe:/o:centos:centos:7

            Kernel: Linux 3.10.0-862.2.3.el7.x86_64

      Architecture: x86-64

mysql2节点:

[root@localhost ~]# hostnamectl set-hostname mysql2

[root@localhost ~]# logout

[root@mysql2 ~]# hostnamectl

   Static hostname: mysql2

         Icon name: computer-vm

           Chassis: vm

        Machine ID: 622ba110a69e24eda2dca57e4d306baa

           Boot ID: 5e41c48c85704016ad0bd940500cc255

    Virtualization: kvm

  Operating System: CentOS Linux 7 (Core)

       CPE OS Name: cpe:/o:centos:centos:7

            Kernel: Linux 3.10.0-862.2.3.el7.x86_64

      Architecture: x86-64

(2)关闭防火墙及SELinux服务

两个节点关闭防火墙firewalld及SELinux服务,命令如下:

# setenforce 0

# systemctl stop firewalld

(3)配置hosts文件

两个节点配置/etc/hosts文件,修改为如下:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.30.11.12  mysql1

172.30.11.13  mysql2

(4)配置YUM源

两个节点均使用提供的mariad-repo.tar.gz的压缩包,解压并放在/opt目录下,进入/etc/yum.repos.d目录下,将原来的repo文件移除,新建local.repo文件并编辑内容,具体操作命令如下:

# tar -zxvf mariadb-repo.tar.gz -C /opt/

# cd /etc/yum.repos.d/

# mv * /media/

# vi local.repo

# cat local.repo

[mariadb]

name=mariadb

baseurl=file:///opt/mariadb-repo

gpgcheck=0

enabled=1

查看配置的YUM源是否可用,命令如下:

# yum repolist

Loaded plugins: fastestmirror

Loading mirror speeds from cached hostfile

repo id                   repo name                        status

mariadb                  mariadb                          19

repolist: 19

查看到repolist数量,即YUM源配置成功。

(5)安装数据库服务并启动

配置完毕后,两个节点安装数据库服务,命令如下:

# yum install -y mariadb mariadb-server

两个节点启动数据库服务并设置开机自启,命令如下:

# systemctl start mariadb

# systemctl enable mariadb

Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

 

2.初始化数据库并配置主从服务

(1)初始化数据库

两个节点初始化数据库,配置数据库root密码为000000,命令如下:

# mysql_secure_installation

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

 

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):                   #默认按回车

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!

(2)配置mysql1主节点

修改mysql1节点的数据库配置文件,在配置文件/etc/my.cnf.d/server.cnf中的[mysqld]增添如下内容。

[root@mysql1 ~]# cat /etc/my.cnf.d/server.cnf

... ...

[mysqld]

log_bin = mysql-bin                       #记录操作日志

binlog_ignore_db = mysql                  #不同步mysql系统数据库

server_id = 12                            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.30.11.12,server_id就写12

... ...

重启数据库服务,并进入数据库,命令如下:

[root@mysql1 ~]# systemctl restart mariadb

[root@mysql1 ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 9

Server version: 10.3.23-MariaDB-log 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)]>

在mysql1节点,授权在任何客户端机器上可以以root用户登录到数据库,然后在主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主节点数据库的权限。命令如下:

MariaDB [(none)]> grant all privileges  on *.* to root@'%' identified by "000000";

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '000000';

Query OK, 0 rows affected (0.00 sec)

(3)配置mysql2从节点

修改mysql2节点的数据库配置文件,在配置文件/etc/my.cnf.d/server.cnf中的[mysqld]增添如下内容。

[root@mysql2 ~]# cat /etc/my.cnf.d/server.cnf

... ...

[mysqld]

log_bin = mysql-bin                       #记录操作日志

binlog_ignore_db = mysql                  #不同步mysql系统数据库

server_id = 13                            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.30.11.13,server_id就写13

... ...

修改完配置文件后,重启数据库服务,并在从节点mysql2上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名mysql1,master_user为上一步中创建的用户user,命令如下:

[root@mysql2 ~]# systemctl restart mariadb

[root@mysql2 ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 9

Server version: 10.3.23-MariaDB-log 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='mysql1',master_user='user',master_password='000000';

Query OK, 0 rows affected (0.01 sec)

配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。命令如下:

MariaDB [(none)]> start slave;

MariaDB [(none)]> show slave status\G

*************************** 1. row ***************************

                Slave_IO_State: Waiting for master to send event

                   Master_Host: mysql1

                   Master_User: user

                   Master_Port: 3306

                 Connect_Retry: 60

               Master_Log_File: mysql-bin.000001

           Read_Master_Log_Pos: 705

                Relay_Log_File: mysql2-relay-bin.000002

                 Relay_Log_Pos: 1004

         Relay_Master_Log_File: mysql-bin.000001

              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: 705

               Relay_Log_Space: 1314

               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: 12

                Master_SSL_Crl:

            Master_SSL_Crlpath:

                    Using_Gtid: No

                   Gtid_IO_Pos:

       Replicate_Do_Domain_Ids:

   Replicate_Ignore_Domain_Ids:

                 Parallel_Mode: conservative

                     SQL_Delay: 0

           SQL_Remaining_Delay: NULL

       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

              Slave_DDL_Groups: 2

Slave_Non_Transactional_Groups: 0

    Slave_Transactional_Groups: 0

1 row in set (0.000 sec)

可以看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,配置数据库主从集群成功。

3.验证数据库主从服务

(1)主节点创建数据库

先在主节点mysql1中创建库test,并在库test中创建表company,插入表数据,创建完成后,查看表company数据,命令如下:

[root@mysql1 ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 11

Server version: 10.3.23-MariaDB-log 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)]> 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,"alibaba","china");

Query OK, 1 row affected (0.01 sec)

 

MariaDB [test]> select * from company;

+----+---------+-------+

| id | name    | addr  |

+----+---------+-------+

|  1 | alibaba | china |

+----+---------+-------+

1 row in set (0.00 sec)

(2)从节点验证复制功能

登录mysql2节点的数据库,查看数据库列表。找到test数据库,查询表,并查询内容验证从数据库的复制功能,命令如下:

[root@mysql2 ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 12

Server version: 10.3.23-MariaDB-log 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)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.00 sec)

 

MariaDB [(none)]> use test;

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 [test]> show tables;

+----------------+

| Tables_in_test |

+----------------+

| company        |

+----------------+

1 row in set (0.00 sec)

 

MariaDB [test]> select * from company;

+----+---------+-------+

| id | name    | addr  |

+----+---------+-------+

|  1 | alibaba | china |

+----+---------+-------+

1 row in set (0.00 sec)

可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。

2.实战案例——部署读写分离数据库

2.1 案例目标

(1)了解读写分离数据库的部署方式

(2)了解mycat中间件的安装与使用

(3)了解读写分离数据库的工作方式

2.2 案例分析

1.规划节点

数据库主从案例的节点规划,见表2-1-1。

表2-1-1 节点规划

IP

主机名

节点

172.30.11.12

mysql1

主数据库节点

172.30.11.13

mysql2

从数据库节点

172.30.11.14

mycat

数据库中间件节点

2.基础准备

使用上个案例安装完成的主从数据库作为基础进行实验,使用OpenStack平台再创建一台云主机作为mycat数据库中间件,云主机镜像使用提供的CentOS_7.5_x86_64_XD.qcow2镜像,flavor使用1核/2G内存/20G硬盘,自行配置网络并使用远程连接工具连接云主机。节点规划表中的IP地址为作者的IP地址,在进行实操案例的时候,按照自己的环境规划网络与IP地址。

2.3 案例实施

1.基础环境安装

(1)修改主机名

使用远程连接工具CRT连接到172.30.11.14虚拟机,并修改主机名为mycat,命令如下:

# hostnamectl set-hostname mycat

# logout

[root@mycat ~]# hostnamectl

   Static hostname: mycat

         Icon name: computer-vm

           Chassis: vm

        Machine ID: 622ba110a69e24eda2dca57e4d306baa

           Boot ID: c9e345d94abb4f0684ce75726c39dbcf

    Virtualization: kvm

  Operating System: CentOS Linux 7 (Core)

       CPE OS Name: cpe:/o:centos:centos:7

            Kernel: Linux 3.10.0-862.2.3.el7.x86_64

      Architecture: x86-64

 

(2)安装JDK环境

在mycat节点安装Java JDK环境,具体操作步骤如下:

使用提供的mariadb-repo.tar.gz包上传至mycat节点的/opt目录下,解压并配置成本地yum源,命令如下:

[root@mycat opt]# tar -zxvf mariadb-repo.tar.gz

[root@mycat ~]# mv /etc/yum.repos.d/* /media/

[root@mycat ~]# vi /etc/yum.repos.d/local.repo

[root@mycat ~]# cat /etc/yum.repos.d/local.repo

[mariadb]

name=mariadb

baseurl=file:///opt/mariadb-repo

gpgcheck=0

enabled=1

配置完yum源之后,进行安装Java JDK环境,命令如下:

[root@mycat ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel

... ...

安装完之后,可以使用命令查看Java JDK环境,命令如下:

[root@mycat ~]# java -version

openjdk version "1.8.0_262"

OpenJDK Runtime Environment (build 1.8.0_262-b10)

OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)

 

3.部署Mycat读写分离中间件服务

(1)安装Mycat服务

将Mycat服务的二进制软件包Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz上传到Mycat虚拟机的/root目录下,并将软件包解压到/use/local目录中。赋予解压后的Mycat目录权限。

[root@mycat ~]# tar -zxvf 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

(2)编辑Mycat的逻辑库配置文件

配置Mycat服务读写分离的schema.xml配置文件在/usr/local/mycat/conf/目录下,可以在文件中定义一个逻辑库,使用户可以通过Mycat服务管理该逻辑库对应的MariaDB数据库。在这里定义一个逻辑库schema,name为USERDB;该逻辑库USERDB对应数据库database为test(在部署主从数据库时已创建);设置数据库写入节点为主节点mysql1;设置数据库读取节点为从节点mysql2。(可以直接删除原来schema.xml的内容,替换为如下。)

注意:IP需要修改成实际的IP地址。

[root@mycat ~]# cat /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="172.30.11.12:3306" user="root" password="000000">

        <readHost host="hostS1" url="172.30.11.13:3306" user="root" password="000000" />

    </writeHost>

</dataHost>

</mycat:schema>

代码说明:

l sqlMaxLimit:配置默认查询数量。

l database:为真实数据库名。

l balance="0":不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。

l balance="1":全部的readHost与stand by writeHost参与select语句的负载均衡,简单来说,当双主双从模式(M1->S1,M2->S2,并且M1与M2互为主备),正常情况下,M2、S1、S2都参与select语句的负载均衡。

l balance="2":所有读操作都随机的在writeHost、readhost上分发。

l balance="3":所有读请求随机地分发到wiriterHost对应的readhost执行,writerHost不负担读压力,注意balance=3只在1.4及其以后版本有,1.3版本没有。

l writeType="0":所有写操作发送到配置的第一个writeHost,第一个挂了需要切换到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件dnindex.properties中。

l writeType="1":所有写操作都随机的发送到配置的writeHost。

(3)修改配置文件权限

修改schema.xml的用户权限,命令如下:

[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml

(4)编辑mycat的访问用户

修改/usr/local/mycat/conf/目录下的server.xml文件,修改root用户的访问密码与数据库,密码设置为000000,访问Mycat的逻辑库为USERDB,命令如下。

[root@mycat ~]# cat /usr/local/mycat/conf/server.xml

在配置文件的最后部分,

<user name="root">

              <property name="password">000000</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>

保存并退出server.xml配置文件。

(5)启动Mycat服务

通过命令启动Mycat数据库中间件服务,启动后使用netstat -ntpl命令查看虚拟机端口开放情况,如果有开放8066和9066端口,则表示Mycat服务开启成功。端口查询情况如下图所示。

[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start

 

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 2

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.001 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 | alibaba | china |

+----+---------+-------+

1 row in set (0.005 sec)

 

(2)用Mycat服务添加表数据

在Mycat虚拟机上使用mysql命令对表company添加一条数据(2,"basketball","usa"),添加完毕后查看表信息。命令如下。

MySQL [USERDB]> insert into company values(2,"bastetball","usa");

Query OK, 1 row affected (0.050 sec)

 

MySQL [USERDB]> select * from company;

+----+------------+-------+

| id | name       | addr  |

+----+------------+-------+

|  1 | alibaba    | china |

|  2 | bastetball | usa   |

+----+------------+-------+

2 rows in set (0.003 sec)

(3)验证Mycat服务对数据库读写操作分离

在Mycat虚拟机节点使用mysql命令,通过9066端口查询对数据库读写操作的分离信息。可以看到所有的写入操作WRITE_LOAD数都在mysql1主数据库节点上,所有的读取操作READ_LOAD数都在mysql2主数据库节点上。由此可见,数据库读写操作已经分离到mysql1和mysql2节点上了。命令如下。

[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p000000 -e 'show  @@datasource;'

查询结果如下图所示。

 

至此,Mycat读写分离数据库案例完成。

 

posted @ 2021-12-11 04:46  王路飞飞飞  阅读(142)  评论(0)    收藏  举报