准备

准备三台新虚拟机
修改主机名(关闭防火墙)

  1. hostnamectl set-hostname mycat
  2. hostnamectl set-hostname db1
  3. hostnamectl set-hostname db2

编辑host文件

3台集群虚拟机的/etc/hosts文件配置部分

  1. vim /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.100.40 mycat
  5. 192.168.100.41 db1
  6. 192.168.100.42 db2

配置Yum安装源

(如果下载不了先使用外部yum源)

  1. mv /etc/yum.repos.d/* /media/
  2. vim /etc/yum.repos.d/local.repo
  3. [mariadb]
  4. name=mariadb
  5. baseurl=file:///opt/gpmall-repo
  6. gpgcheck=0
  7. enabled=1
  8. (这里我只是修改了mycat的yum源)

安装JDK环境

  1. yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
  2. 查看版本
  3. java -version

部署MariaDB主从数据库集群服务

安装MariaDB服务

通过YUM命令在db1和db2虚拟机节点上安装MariaDB服务

  1. yum install -y mariadb mariadb-server
  2. systemctl start mariadb
  3. systemctl enable mariadb

初始化MariaDB数据库

在db1和db2虚拟机节点上初始化MariaDB数据库,并设置MariaDB数据库root访问用户的密码为123456。

  1. mysql_secure_installation
  2. usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
  3. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  4. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  5. In order to log into MariaDB to secure it, we'll need the current
  6. password for the root user. If you've just installed MariaDB, and
  7. you haven't set the root password yet, the password will be blank,
  8. so you should just press enter here.
  9. Enter current password for root (enter for none): #默认按回车
  10. OK, successfully used password, moving on...
  11. Setting the root password ensures that nobody can log into the MariaDB
  12. root user without the proper authorisation.
  13. Set root password? [Y/n] y
  14. New password: #输入数据库root密码123456
  15. Re-enter new password: #重复输入密码123456
  16. Password updated successfully!
  17. Reloading privilege tables..
  18. ... Success!
  19. By default, a MariaDB installation has an anonymous user, allowing anyone
  20. to log into MariaDB without having to have a user account created for
  21. them. This is intended only for testing, and to make the installation
  22. go a bit smoother. You should remove them before moving into a
  23. production environment.
  24. Remove anonymous users? [Y/n] y
  25. ... Success!
  26. Normally, root should only be allowed to connect from 'localhost'. This
  27. ensures that someone cannot guess at the root password from the network.
  28. Disallow root login remotely? [Y/n] n
  29. ... skipping.
  30. By default, MariaDB comes with a database named 'test' that anyone can
  31. access. This is also intended only for testing, and should be removed
  32. before moving into a production environment.
  33. Remove test database and access to it? [Y/n] y
  34. - Dropping test database...
  35. ... Success!
  36. - Removing privileges on test database...
  37. ... Success!
  38. Reloading the privilege tables will ensure that all changes made so far
  39. will take effect immediately.
  40. Reload privilege tables now? [Y/n] y
  41. ... Success!
  42. Cleaning up...
  43. All done! If you've completed all of the above steps, your MariaDB
  44. installation should now be secure.
  45. Thanks for using MariaDB!

配置数据库集群主节点

  1. vim /etc/my.cnf
  2. [mysqld]
  3. log_bin = mysql-bin #记录操作日志
  4. binlog_ignore_db = mysql #不同步MySQL系统数据库
  5. server_id = 18 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.18,server_id就写18

编辑完成配置文件my.cnf后,重启MariaDB服务。

  1. systemctl restart mariadb

开放主节点的数据库权限

  1. mysql -uroot -p123456
  2. 在主节点db1虚拟机上使用mysql命令登录MariaDB数据库,授权在任何客户端机器上可以以root用户登录到数据库。
  3. grant all privileges on *.* to root@'%' identified by "123456";
  4. 在主节点db1数据库上创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限,
  5. grant replication slave on *.* to 'user'@'db2' identified by '123456';

配置从节点db2同步主节点db1

在从节点db2虚拟机上使用mysql命令登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名db1,master_user为在步骤(4)中创建的用户user,

  1. mysql -uroot -p123456
  2. change master to master_host='db1',master_user='user',master_password='123456';
  3. start slave;
  4. 查询
  5. show slave status\G;

验证主从数据库的同步功能

先在主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据。创建完成后,查看表company数据,

  1. mysql -uroot -p123456
  2. create database test;
  3. use test
  4. create table company(id int not null primary key,name varchar(50),addr varchar(255));
  5. insert into company values(1,"facebook","usa");
  6. select * from company;

这时从节点db2的数据库就会同步主节点数据库创建的test库,可以在从节点查询test数据库与表company,如果可以查询到信息,就能验证主从数据库集群功能在正常运行。

  1. mysql -uroot -p123456
  2. show databases;
  3. select * from test.company;

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

安装mycat

安装mycat并赋予解压后mycat目录权限

  1. tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
  2. chown -R 777 /usr/local/mycat/

在/etc/profile系统变量文件中添加Mycat服务的系统变量,并生效变量

  1. echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
  2. source /etc/profile

编辑mycat的逻辑库配置文件

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

vim /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.16.51.18:3306" user="root" password="123456">
        <readHost host="hostS1" url="172.16.51.30:3306" user="root" password="123456" />
    </writeHost>
</dataHost>
</mycat:schema>

代码说明:
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。

修改配置文件权限

修改schema.xml的用户权限

chown root:root /usr/local/mycat/conf/schema.xml

编辑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>

启动mycat服务

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

/bin/bash /usr/local/mycat/bin/mycat start

验证数据库集群服务读写分离功能

用Mycat服务查询数据库信息

先在Mycat虚拟机上使用Yum安装mariadb-client服务

yum install -y MariaDB-client

在Mycat虚拟机上使用mysql命令查看Mycat服务的逻辑库USERDB,因为Mycat的逻辑库USERDB对应数据库test(在部署主从数据库时已安装),所以可以查看库中已经创建的表company。

mysql -h127.0.0.1 -P8066 -uroot -p123456
show databases;
use USERDB
show tables;
 select * from company;

验证mycat服务对数据库读写操作分离

mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show  @@datasource;'

image.png