Linux, 应用部署, 数据库

Mariadb主从复制与读写分离配置

一、主从复制的实现

安装mysql数据库一主二从共三台

主数据库的配置:

1、在/etc/my.cnf文件中添加两行
      server-id=01
      log-bin=mysql-index

2、建立同步账号并授权
  GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';

3、重新数据库
    systemctl restart mariadb

4、查看主数据库的状态
[root@localhost ]# mysql -uroot -p
MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mysql-index.000001 |      455 |              |                  |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
从数据库的配置:

1、在/etc/my.cnf文件中添加一行
     server-id=02
    ----从库2修改server-id=03,其它配置与从库1的设置一样。
2、重新数据库
    systemctl restart mariadb

3、登录进入mysql命令行,先停止slave

     stop slave;
    
4、修改配置

CHANGE MASTER TO
  MASTER_HOST='192.168.168.146',
  MASTER_USER='slave',
  MASTER_PASSWORD='123456',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mysql-index.000001',
  MASTER_LOG_POS=455,
  MASTER_CONNECT_RETRY=10;

5、重启slave进程
   start slave;

6、查看从数据库运行状态
 Slave_IO_Running: Yes            Slave_SQL_Running: Yes  即说明运行正常。

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.168.146
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-index.000001
          Read_Master_Log_Pos: 699
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 985
        Relay_Master_Log_File: mysql-index.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 

二、读写分离的实现

使用mycat代理方式实现读写分离,mycat的安装需要jdk环境。mycat的主要配置文件server.xml,schema.xml两个,具体配置如下

server.xml的配置主要是关于前端连接mycat的用户名密码、ACL等信息的修改,根据需要修改即可。

schema.xml文件的主要内容为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="TESTDB" checkSQLschema="false"  sqlMaxLimit="100" dataNode="dn1">
    </schema>
    <dataNode name="dn1" dataHost="localhost1" database="student" />
    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"
                      writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
            <heartbeat>select user()</heartbeat>
            <writeHost host="hostM1" url="192.168.168.143:3306" user="aaa"
                               password="123456">
                    <readHost host="hostS1" url="192.168.168.142:3306" user="aaa" password="123456" />
            </writeHost>
    </dataHost>
</mycat:schema>

启动mycat服务,并查看mycat的运行状态

1、先使用调试模式运行检查是否有错,最后一行显示successfully表示运行正确。

[root@localhost ~]# /usr/local/mycat/bin/mycat console
Running Mycat-server...
wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
jvm 1    | OpenJDK 64-Bit Server VM warning: Option AggressiveOpts was deprecated in version 11.0 and will likely be removed in a future release.
jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
jvm 1    | 
jvm 1    | MyCAT Server startup successfully. see logs in logs/mycat.log

2、停止调试模式,启动mycat

control+c

[root@localhost ~]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@localhost ~]# 

3、查看运行情况

RS_CODE = 1 表示连接数据库池正常。

[root@localhost ~]# mysql -h127.0.0.1 -uroot -p -P9066

三、注意事项

1、mycat的配置文件有错的情况下,提示并不是很清晰,需要仔细排查。

Leave a Reply