MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL受權許可MariaDB的目的是徹底兼容MySQL,包括API和命令行,MySQL因爲如今閉源了,而能輕鬆成爲MySQL的代替品.在存儲引擎方面,使用XtraDB來代替MySQL的InnoDB,MariaDB由MySQL的創始人Michael Widenius主導開發,他早前曾以10億美圓的價格,將本身建立的公司MySQL AB賣給了SUN,此後,隨着SUN被甲骨文收購MySQL的全部權也落入Oracle的手中.MariaDB名稱來自MichaelWidenius的女兒Maria的名字.java
MyCat是一個完全開源的,面向企業應用開發的大數據庫集羣,支持事務、ACID、能夠替代MySQL的增強版數據庫,
其功能有能夠視爲MySQL集羣的企業級數據庫,用來替代昂貴的Oracle集羣.融合了內存緩存技術、NoSQL技術、HDFS大數據的新型SQLServer,結合傳統數據庫和新型分佈式數據倉庫的新一代企業級數據庫產品.
mysql
192.168.1.5 MyCat Server 192.168.1.11 Master 主MariaDB 192.168.1.12 Slave1 從1 192.168.1.13 Slave2 從2
1.在配置讀寫分離前請確保你的主機有四臺,分別在每臺主機安裝MariaDB數據庫,並初始化,須要注意的是MyCat服務端的MariaDB不須要初始化.linux
[root@localhost ~]# yum install -y mariadb mariadb-server [root@localhost ~]# systemctl start mariadb [root@localhost ~]# systemctl enable mariadb
2.在MyCat服務端安裝JDK環境,因爲MyCat是使用Java開發的,在這裏咱們要先配置一下JDK環境.web
[root@localhost ~]# tar -xzvf jdk-8u171-linux-x64.tar.gz -C /usr/local/ [root@localhost ~]# mv /usr/local/jdk1.8.0_171/ /usr/local/jdk [root@localhost ~]# vim /etc/profile #------------------------------------------------------------------ #JAVA-JDK-PATH export JAVA_HOME=/usr/local/jdk export JAVA_BIN=/usr/local/jdk/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH #------------------------------------------------------------------ [root@localhost ~]# source /etc/profile [root@localhost ~]# java -version java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
3.此步驟須要進入每個數據庫建立受權用戶,也就是給MyCAT準備的使用數據庫的受權用戶.sql
[root@localhost ~]# mysql -uroot -p Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 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 database MyCatDB; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all on MyCatDB.* to "MyCat"@"%" identified by "123123"; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> grant all on MyCatDB.* to "MyCat"@"localhost" identified by "123123"; Query OK, 0 rows affected (0.00 sec)
4.解壓MyCat工具,並修改MyCat用戶受權文件,寫入MyCat登錄帳戶,這裏配置文件有不少選項,咱們不須要動,只須要修改如下幾個地方就行.數據庫
[root@localhost ~]# wget http://dl.mycat.io/1.6.5/Mycat-server-1.6.5-release-20180122220033-linux.tar.gz [root@localhost ~]# tar -xzvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz -C /usr/local/ [root@localhost ~]# vim /usr/local/mycat/conf/server.xml <user name="root" defaultAccount="true"> #指定MyCat登錄用戶名 <property name="password">123456</property> #指定密碼 <property name="schemas">MyCatDB</property> #指定同步數據庫 </user> <user name="user"> #指定MyCat登錄用戶名 <property name="password">123456</property> #指定密碼 <property name="schemas">MyCatDB</property> #指定同步數據庫 <property name="readOnly">true</property> #指定只讀 </user>
5.修改MyCat讀寫分離策略,這裏咱們刪除源文件,並本身建立一個,寫入如下內容.apache
[root@localhost ~]# 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="MyCatDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn_test"> </schema> <dataNode name="dn_test" dataHost="localhost" database="MyCatDB" /> <dataHost name="localhost" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="Master1" url="192.168.1.11:3306" user="MyCat" password="123123"> #配置寫主機 <readHost host="Slave1" url="192.168.1.12:3306" user="MyCat" password="123123" /> #配置讀主機 <readHost host="Slave2" url="192.168.1.13:3306" user="MyCat" password="123123" /> #配置讀主機 </writeHost> </dataHost> </mycat:schema>
6.啓動MyCat若無報錯則說明啓動成功,查看端口看虛擬端口是否開啓成功.vim
[root@localhost ~]# /usr/local/mycat/bin/mycat start [root@localhost ~]# netstat -an |grep "9066" #虛擬schema管理端口 [root@localhost ~]# netstat -an |grep "8066" #虛擬schema登錄端口
7.在MyCat服務主機登錄數據庫,測試同步效果.緩存
[root@localhost ~]# mysql -uroot -p123456 -h 127.0.0.1 -P 9066 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (monitor) 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 @@heartbeat; #RS_CODE爲1表示心跳正常 +---------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ | NAME | TYPE | HOST | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME | STOP | +---------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ | Master1 | mysql | 192.168.1.11 | 3306 | -1 | 0 | idle | 0 | 0,16,16 | 2018-12-21 01:29:43 | false | | Slave1 | mysql | 192.168.1.12 | 3306 | -1 | 0 | idle | 0 | 34,31,31 | 2018-12-21 01:29:43 | false | | Slave2 | mysql | 192.168.1.13 | 3306 | -1 | 0 | idle | 0 | 1,16,16 | 2018-12-21 01:29:43 | false | +---------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ 3 rows in set (0.00 sec) MySQL [(none)]> show @@datasource; #查看讀寫分離的機器配置狀況 +----------+---------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+ | DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD | +----------+---------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+ | dn_test | Master1 | mysql | 192.168.1.11 | 3306 | W | 0 | 0 | 1000 | 0 | 0 | 0 | | dn_test | Slave1 | mysql | 192.168.1.12 | 3306 | R | 0 | 0 | 1000 | 0 | 0 | 0 | | dn_test | Slave2 | mysql | 192.168.1.13 | 3306 | R | 0 | 0 | 1000 | 0 | 0 | 0 | +----------+---------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+ 3 rows in set (0.01 sec) MySQL [(none)]>
8.登陸MyCat代理端,測試讀寫分離服務.bash
[root@localhost ~]# mysql -uroot -p123456 -h127.0.0.1 -P 8066 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 3 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 [(none)]> MySQL [(none)]> MySQL [(none)]> MySQL [(none)]> show databases; +----------+ | DATABASE | +----------+ | MyCatDB | +----------+ 1 row in set (0.00 sec) MySQL [(none)]>
1.首先咱們先來下載MyCat以及Zookeeper.
[root@localhost ~]# wget http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz [root@localhost ~]# wget http://dl.mycat.io/mycat-web-1.0/Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz
2.在MyCat服務端安裝JDK環境,因爲MyCat是使用Java開發的,在這裏咱們要先配置一下JDK環境.
[root@localhost ~]# tar -xzvf jdk-8u171-linux-x64.tar.gz -C /usr/local/ [root@localhost ~]# mv /usr/local/jdk1.8.0_171/ /usr/local/jdk [root@localhost ~]# vim /etc/profile #------------------------------------------------------------------ #JAVA-JDK-PATH export JAVA_HOME=/usr/local/jdk export JAVA_BIN=/usr/local/jdk/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH #------------------------------------------------------------------ [root@localhost ~]# source /etc/profile [root@localhost ~]# java -version java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
3.配置MariaDB開啓lower_case_table_names
選項,每臺都要開啓.
[root@localhost ~]# yum install -y mariadb mariadb-server [root@localhost ~]# vim /etc/my.cnf [mysqld] lower_case_table_names = 1 [root@localhost ~]# systemctl restart mariadb
4.安裝zookeeper,其主要用來統計數據.
[root@localhost ~]# tar -xzvf zookeeper-3.4.12.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/zookeeper-3.4.12/conf/ [root@localhost ~]# cp zoo_sample.cfg zoo.cfg [root@localhost ~]# vim zoo.cfg dataDir=/usr/local/... dataLogDir=/usr/local/...
5.運行zookeeper
[root@localhost ~]# cd /usr/local/zookeeper-3.4.12/bin/ [root@localhost ~]# ./zkServer.sh start [root@localhost ~]# netstat -ant | grep 2181 tcp 0 0 :::2181 :::* LISTEN 若是出現錯誤:nohup: failed to run command `java’: No such file or directory 能夠在zkServer.sh中的首行添加以下代碼 export JAVA_HOME=/usr/lib/jdk export PATH=$JAVA_HOME/bin:$PATH
6.最後一步,安裝並運行Mycat-Web
[root@localhost ~]# tar -xzvf Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz -C /usr/local [root@localhost ~]# cd /usr/local/mycat-web [root@localhost ~]# ./start.sh & [root@localhost ~]# netstat -an | grep "8082" tcp 0 0 :::8082 :::* LISTEN [root@localhost ~]# curl http://localhost:8082/mycat