mariadb读写分离
环境
| 节点 | IP | 主机名 |
| node1 | 172.25.253.11 | mycat |
| node2 | 172.25.253.4 | db1 |
| node3 | 172.25.253.5 | db2 |
# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.25.253.11 mycat 172.25.253.4 db1 172.25.253.5 db2
# rm -rf /etc/yum.repos.d/* # vim /etc/yum.repos.d/http.repo [centos] name=centos baseurl=http://172.25.253.3/centos/ gpgcheck=0 enabled=1 [gpmall] name=gpmall baseurl=http://172.25.253.3/colony/gpmall-repo/ gpgcheck=0 enabled=1
配置主从数据库
- db1和db2 安装mariadb服务
[root@db1 ~]# yum install -y mariadb mariadb-server [root@db2 ~]# yum install -y mariadb mariadb-server [root@db1 ~]# systemctl start mariadb [root@db2 ~]# systemctl start mariadb
- 开启mariadb的初始化
[root@db1 ~]# mysqladmin -uroot password 000000 [root@db2 ~]# mysqladmin -uroot password 000000
-
修改配置文件(db1为主,db2为从)
[root@db1 ~]# vim /etc/my.cnf [mysqld] log_bin=mysql-bin binlog_ignore_db=mysql server_id=4 [root@db1 ~]# systemctl restart mariadb
[root@db2 ~]# vim /etc/my.cnf [mysqld] log_bin=mysql-bin binlog_ignore_db=mysql server_id=5 [root@db1 ~]# systemctl restart mariadb
-
配置db1数据库权限
- 授权任何客户端都可以用root登录到数据库
- 创建一个user用户让从节点db2连接,并赋予从节点同步主节点权限
[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 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)]> grant all privileges on *.* to root@'%' identified by '000000'; Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by'000000'; Query OK, 0 rows affected (0.000 sec)
-
配置db2数据库权限
- 从节点连接主节点的连接信息,master_host为主节点的db1,master_user为数据库授权创建的用户user
[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-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='db1',master_user='user',master_password='000000'; Query OK, 0 rows affected (0.007 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 event Master_Host: db1 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 897 Relay_Log_File: db2-relay-bin.000003 Relay_Log_Pos: 1196 Relay_Master_Log_File: mysql-bin.000002 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: 897 Relay_Log_Space: 2763 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: 25 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: 14 Slave_Non_Transactional_Groups: 0 Slave_Transactional_Groups: 2 1 row in set (0.000 sec)
验证主从数据库的同步功能
[root@db1 ~]# mysql -uroot -p000000 MariaDB [(none)]> create database mytest; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> use mytest Database changed MariaDB [mytest]> create table company(id int not null primary key,name varchar(50),addr varchar(255)); Query OK, 0 rows affected (0.009 sec) MariaDB [mytest]> insert into company values(1,"facebook","usa"); Query OK, 1 row affected (0.004 sec) [root@db2 ~]# mysql -uroot -p000000 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mytest | | performance_schema | | test | +--------------------+ 5 rows in set (0.000 sec)
部署Mycat读写分离
-
mycat安装服务
[root@mycat ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
-
添加系统变量
[root@mycat ~]# ls Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz [root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/ [root@mycat ~]# chown -R 777 /usr/local/mycat/ [root@mycat ~]# echo "export MYCAT_HOME=/usr/local/mycat" >> /etc/profile [root@mycat ~]# source /etc/profile
- 修改Mycat逻辑库配置文件
[root@mycat ~]# cd /usr/local/mycat/ [root@mycat mycat]# vim 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="mytest" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchT ype="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="172.25.253.4:3306" user="root" password="000000"> <!-- can have multi read hosts --> <readHost host="hostS2" url="172.25.253.5:3306" user="root" password="000000" /> </writeHost> </dataHost> </mycat:schema>
[root@mycat mycat]# chown root:root conf/schema.xml
- 配置mycat的访问用户
[root@mycat mycat]# vim conf/server.xml
(跑到末尾) 删除: <user name="user"> <property name="password">user</property> <property name="schemas">TESTDB</property> <property name="readOnly">true</property> </user> 修改: <property name="password">000000</property> <property name="schemas">USERDB</property>
- 启动服务
[root@mycat mycat]# /bin/bash /usr/local/mycat/bin/mycat start Starting Mycat-server... [root@mycat mycat]# netstat -ntpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::8066 :::* LISTEN 11390/java tcp6 0 0 :::9066 :::* LISTEN 11390/java
验证数据库集群服务读写分离功能
-
-
在 Mycat 虚拟机上使用 mysql 命令查看 Mycat 服务的逻辑库 USERDB
-
[root@mycat ~]# yum install -y MariaDB-client [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.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_mytest | +------------------+ | company | +------------------+ 1 row in set (0.003 sec) MySQL [USERDB]> select * from company; +----+----------+------+ | id | name | addr | +----+----------+------+ | 1 | facebook | usa | +----+----------+------+ 1 row in set (0.029 sec)
[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 | 172.25.253.4 | 3306 | W | 0 | 10 | 1000 | 33 | 0 | 0 | | dn1 | hostS2 | mysql | 172.25.253.5 | 3306 | R | 0 | 4 | 1000 | 29 | 3 | 0 | +----------+--------+-------+---------------+------+------+--------+------+------+---------+-----------+------------+

浙公网安备 33010602011771号