主從複製及MariaDB讀寫分離

主從複製
安裝MariaDB
配置主從複製
[root@mariadb01 ~]# systemctl stop firewalld
[root@mariadb01 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multiuser.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbusorg.fedoraproject.FirewallD1.service.
[root@mariadb01 ~]#
[root@mariadb01 ~]# sed -ri s/SELINUX=enforcing/SELINUX=disabled/g
/etc/selinux/config
[root@mariadb01 ~]# setenforce 0
[root@mariadb01 ~]# getenforce
Permissive
[root@mariadb01 ~]#
[root@mariadb01 ~]# yum -y install mariadb-server mariadb
[root@mariadb02 ~]# systemctl stop firewalld
[root@mariadb02 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multiuser.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbusorg.fedoraproject.FirewallD1.service.
[root@mariadb02 ~]#
[root@mariadb02 ~]# sed -ri s/SELINUX=enforcing/SELINUX=disabled/g
/etc/selinux/config
[root@mariadb02 ~]# setenforce 0
[root@mariadb02 ~]# getenforce
Permissive
[root@mariadb02 ~]#
[root@mariadb02 ~]# yum -y install mariadb-server mariadb
[root@mariadb01 ~]# vi /etc/my.cnf
增加:
server_id = 134
log-bin = mysql-bin
[root@mariadb01 ~]# systemctl restart mariadb
[root@mariadb01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multiuser.target.wants/mariadb.service to
/usr/lib/systemd/system/mariadb.service.
[root@mariadb01 ~]#
[root@mariadb01 ~]# mysqladmin -uroot -p password "123456"
Enter password:
[root@mariadb01 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB 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 user 'rep'@'192.168.161.135' identified by
"123456";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant replication slave on . tobr/>'rep'@'192.168.161.135';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax to use
near '' at line 1
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 632 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@mariadb01 ~]#
[root@mariadb02 ~]# vi /etc/my.cnf
[root@mariadb02 ~]# systemctl restart mariadb
[root@mariadb02 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multiuser.target.wants/mariadb.service to
/usr/lib/systemd/system/mariadb.service.
[root@mariadb02 ~]#
[root@mariadb02 ~]# mysqladmin -uroot -p password "123456"
Enter password:
[root@mariadb02 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB 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='192.168.161.134',
-> master_user='rep',
-> master_password='123456',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=632;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G;
主從複製及MariaDB讀寫分離
主從複製及MariaDB讀寫分離

MariaDB讀寫分離****

取消MariaDB主從複製
構建MyCat服務
[root@mariadb01 ~]# vi /etc/my.cnf
註釋掉
-server_id=id_number
-log-bin=mysql-bin
[root@mariadb01 ~]# systemctl restart mariadb
[root@mariadb02 ~]# vi /etc/my.cnf
註釋掉server_id=id_number
[root@mariadb02 ~]# systemctl restart mariadb
##配置JAVA環境
[root@mycat ~]# tar xf jdk-8u191-linux-x64.tar.gz -C /usr/local/
[root@mycat ~]#
[root@mycat ~]# cd /usr/local/
[root@mycat local]# ls
bin etc games include jdk1.8.0_191 lib lib64 libexec sbin share
src
[root@mycat local]# mv jdk1.8.0_191 java191
[root@mycat local]# ls
bin etc games include java191 lib lib64 libexec sbin share src
[root@mycat local]# vi /etc/profile
export JAVA_HOME=/usr/local/java191
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[root@mycat local]# source /etc/profile
[root@mycat local]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
server.xml /usr/local/mycat/conf/server.xml
schema.xml /usr/local/mycat/conf/schema.xml
##配置MyCat服務
$ curl -o mycat.tar.gz http://dl.mycat.io/1.6.6.1/Mycat-server-1.6.6.1-
release-20181031195535-linux.tar.gz
[root@mycat ~]# tar xf Mycat-server-1.6.5-release-20180122220033-
linux.tar.gz -C /usr/local/
[root@mycat mycat]# vim /etc/profile
export MYCAT_HOME=/usr/local/mycat
[root@mycat mycat]# source /etc/profile
[root@mycat mycat]# ln -s /usr/local/mycat/bin/mycat /usr/bin/mycat
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/"&gt;
<system>
<property name="defaultSqlParser">druidparser</property>
</system>
<user name="mycat" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">testDBtest</property>
</user>
<user name="user">
<property name="password">123456</property>
<property name="schemas">testDBtest</property>
<property name="readOnly">true</property>
</user>
</mycat:server>
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/"&gt;
<schema name="testDBtest" checkSQLschema="false" sqlMaxLimit="100"
dataNode="dn1">
</schema>
<dataNode name="dn1" dataHost="localhost1" database="test001" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native"
switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.161.134:3306"
user="mycat"
password="123456">
hosts /etc/hosts
wrapper.conf /usr/local/mycat/wrapper.conf
<readHost host="hostS1" url="192.168.161.135:3306"
user="mycat" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
127.0.0.1 localhost localhost.localdomain localhost4
localhost4.localdomain4
::1 localhost localhost.localdomain localhost6
localhost6.localdomain6
192.168.161.133 mycat
192.168.161.134 mariadb01
192.168.161.135 mariadb02
wrapper.java.command=/usr/local/java191/bin/java
wrapper.java.additional.5=-XX:MaxDirectMemorySize=1G
##啓動mycat並測試
$ mycat start
$ ss -anptu | grep :8066
tcp LISTEN 0 100 :::8066 ::: user:
(("java",pid=11002,fd=79))
[root@mariadb02 ~]# mysql -uroot -p123456
MariaDB [(none)]> use test001
Database changed
MariaDB [test001]> show tables;
Empty set (0.00 sec)
MariaDB [test001]> create table testtest (id bigint not null primary key,
-> user_id varchar(100), travel_date date, fee decimal, days int);
Query OK, 0 rows affected (0.04 sec)
MariaDB [test001]> select
from testtest;
Empty set (0.00 sec)
MariaDB [test001]> insert into testtest (id,user_id,travel_date,fee,days)
-> values (3,'400', 20181111, 1000, 9);
Query OK, 1 row affected (0.00 sec)
MariaDB [test001]> select * from testtest;
+----+---------+-------------+------+------+
| id | user_id | travel_date | fee | days |
+----+---------+-------------+------+------+
| 3 | 400 | 2018-11-11 | 1000 | 9 |
+----+---------+-------------+------+------+
1 row in set (0.00 sec)
MariaDB [test001]>
##測試
[root@mycat mycat]# mysql -uroot -p'123456' -h192.168.161.133 -P8066 -
DtestDBtest
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.29-mycat-1.6.5-release-20180122220033 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 [TEST_DB]> show databases;
+----------+
| DATABASE |
+----------+
|testDBtest|
+----------+
1 row in set (0.00 sec)
MySQL [TEST_DB]> use testDBtest
Database changed
主從複製及MariaDB讀寫分離
主從複製及MariaDB讀寫分離

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章