mysql多實例+mycat+mycatweb

1、拓撲圖

本次實驗搭建的爲一臺服務器上安裝四個mysql實例,實現兩主兩從的結構;而後兩個主mysql承接mycat寫的操做,兩個從mysql承接mycat讀的操做,實現讀寫分離。同時,使用mycat-web鏈接mycat,實現對mycat性能的監控。java

2、安裝mysql

首先查看java版本node

 mycat須要jdk1.7以上版本。mysql

 

我用的mysql安裝包爲源碼包,由於用免編譯包可能會出現文件路徑或權限各類沒有的狀況,作實驗推薦用mysql5.5之前的版本,比較輕量,編譯速度比較快,我用的5.1.73版本,先建立須要的路徑:linux

使用命令:git

cd /data
wget https://downloads.mysql.com/archives/get/file/mysql-5.1.73.tar.gz

 

進行下載github

mkdir -p /data/3316/etc /data/3316/var /data/3316/log
mkdir -p /data/3317/etc /data/3317/var /data/3317/log
mkdir -p /data/3318/etc /data/3318/var /data/3318/log
mkdir -p /data/3319/etc /data/3319/var /data/3319/log
mkdir -p /tmp/mysql

 

建立mysql用戶web

groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql /data/3316
chown -R mysql:mysql /data/3317
chown -R mysql:mysql /data/3318
chown -R mysql:mysql /data/3319
chown -R mysql:mysql /tmp/mysql

 

編譯配置:sql

cd /data #進入安裝包路徑
tar -xvf mysql-5.1.73.tar.gz
cd mysql-5.1.73
./configure --prefix=/data/3316 --sysconfdir=/data/3316/etc --with-tcp-port=3316 --localstatedir=/data/3316/var --with-unix-socket-path=/tmp/mysql/3316.sock --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312,binary --enable-thread-safe-client --with-plugins=innobase --with-mysqld-user=mysql --with-charset=utf8 --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static
make && make install

而後初始化數據庫,此步驟需在mysql_install_db文件的上層目錄執行,否則會報錯:數據庫

(scripts有的版本在解壓包目錄,有的在本身選的mysql安裝目錄)apache

scripts/mysql_install_db --user=mysql --basedir=/data/3316 --datadir=/data/3316/var --defaults-file=/data/3316/etc/my.cnf

scripts/mysql_install_db --user=mysql --basedir=/data/3316 --datadir=/data/3317/var --defaults-file=/data/3317/etc/my.cnf

scripts/mysql_install_db --user=mysql --basedir=/data/3316 --datadir=/data/3318/var --defaults-file=/data/3318/etc/my.cnf

scripts/mysql_install_db --user=mysql --basedir=/data/3316 --datadir=/data/3319/var --defaults-file=/data/3319/etc/my.cnf

 

 而後建立各個實例的配置文件:

touch /data/3316/etc/my.cnf
touch /data/3317/etc/my.cnf
touch /data/3318/etc/my.cnf
touch /data/3319/etc/my.cnf

 

打開文件

vi /data/3316/etc/my.cnf

把下列配置項複製進去

[client]
port            = 3316
socket          = /tmp/mysql/3316.sock

[mysql]
no-auto-rehash

[mysqld]
user    = mysql
port    = 3316
socket  = /tmp/mysql/3316.sock
basedir = /data/3316
datadir = /data/3316/var
open_files_limit    = 1024
back_log = 600
max_connections = 800
max_connect_errors = 3000
table_open_cache = 614
external-locking = FALSE
max_allowed_packet =8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
#default_table_type = InnoDB
thread_stack = 192K
#transaction_isolation = READ-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
#long_query_time = 1
#log_long_format
log-error = /data/3316/log/error.log
log-slow-queries = /data/3316/log/slow.log
pid-file = /tmp/mysql/3316.pid
log-bin = /data/3316/var/mysql-bin
#binlog-do-db = db1
#relay-log = /data/3316/log/relay-bin
#relay-log-info-file = /data/3316/log/relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
expire_logs_days = 7
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
lower_case_table_names = 1
skip-name-resolve
binlog_format = mixed
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 3316
innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 4M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 2M

[mysqld_safe]
log-error=/data/3316/log/oldboy_3308.err
pid-file=/tmp/mysql/3316.pid

 

一樣,把/data/3317/etc/my.cnf /data/3318/etc/my.cnf /data/3319/etc/my.cnf 也都複製相同的內容,不過要把全部的3316換成對應的端口。

而後啓動mysql:

 

cd /data/3316/bin
./mysqld_safe --defaults-file=/data/3316/etc/my.cnf --user=mysql --basedir=/data/3316 --datadir=/data/3316/var/ --log-error=/data/3316/mysql.err &

 

 

 

 注意:這一步defaults-file配置項必須位於第一個。

一樣在/data/3316/bin目錄下執行

 

./mysqld_safe --defaults-file=/data/3317/etc/my.cnf --user=mysql --basedir=/data/3316 --datadir=/data/3317/var/ --log-error=/data/3317/mysql.err &

./mysqld_safe --defaults-file=/data/3318/etc/my.cnf --user=mysql --basedir=/data/3316 --datadir=/data/3318/var/ --log-error=/data/3318/mysql.err &

./mysqld_safe --defaults-file=/data/3319/etc/my.cnf --user=mysql --basedir=/data/3316 --datadir=/data/3319/var/ --log-error=/data/3319/mysql.err &

 

 

 

這時候輸入命令:

 

ls /tmp/mysql

 

 

 

應該會顯示:

 

到這一步mysql就已經徹底安裝成功了,接下來須要給各個mysql設置密碼,開啓主從複製,接下來以3316,3317爲例;3318,3319同理:

cd /data/3316/bin
./mysqladmin -S /tmp/mysql/3316.sock -uroot password#接下來輸入兩次密碼,我輸入的是123
./mysqladmin -S /tmp/mysql/3317.sock -uroot password#同上

./mysql -uroot -S 3316.sock -p #輸入密碼
grant all privileges  on *.* to root@'%' identified by "password"; #開啓mysql遠程訪問權限,password換成你的密碼
flush privileges;
create database db1; #建立實驗數據庫
GRANT replication slave ON *.* TO 'slave'@'%' IDENTIFIED BY '123';#在主庫建立一個從庫用來訪問的用戶
show master status;

 show master status命令結果如圖:

此時3316不要再動,打開另外一個窗口操做:

cd /data/3316/bin
./mysql -uroot -S 3317.sock -p
create database db1;
stop slave;
reset slaCHANGE MASTER TO MASTER_HOST='10.223.138.154',MASTER_PORT=3316,MASTER_USER='slave',MASTER_PASSWORD='123',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=246;
start slave;
shhow slave status\G;
若是顯示的兩項爲YES,就表明主從複製已經配置好了,能夠實驗下在主庫新建表,對數據增刪改查,從庫是否會作一樣的操做

 

 

再用一樣的操做把331八、3319配置好

3、安裝zookeeper

mkdir /opt/zookeeper
cd /opt/zookeeper
wget http://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.13.tar.gz #推薦從國內鏡像站下載,官網太慢了
tar -xvf zookeeper-3.4.13.tar.gz
cd zookeeper-3.4.13
cp   /opt/zookeeper/zookeeper-3.4.13/conf/zoo_sample.cfg        /opt/zookeeper/zookeeper-3.4.13/conf/zoo.cfg
cd bin
./zkServer.sh start
./zkServer.sh status #查看zookeeper啓動結果

 

 

 4、安裝mycat-web

mkdir /opt/mycat-web #建立mycat-web目錄
cd /opt/mycat-web
wget https://raw.githubusercontent.com/MyCATApache/Mycat-download/master/mycat-web-1.0/Mycat-web-1.0-SNAPSHOT-20160617163048-linux.tar.gz
tar xvf Mycat-web-1.0-SNAPSHOT-20160617163048-linux.tar.gz
cd mycat-web
vi mycat-web/WEB-INF/classes/mycat.properties
zookeeper=10.223.138.154:2181 #修改值,保存退出
./start.sh

 

 5、安裝mycat

wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz #下載mycat
tar -xvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
cd mycat
cd conf 
vi schema.xml

 

 schema.xml文件是mycat的精華,mycat的讀寫分離,分庫分表功能都經過這個文件實現,先貼上個人配置(此部分參考了別人的配置):

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    <!-- TESTDB 是mycat的邏輯庫名稱,連接須要用的 -->
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
        <!-- auto sharding by id (long) -->
        <table name="travelrecord" dataNode="dn1,dn2" rule="auto-sharding-long" />

        <!-- global table is auto cloned to all defined data nodes ,so can join
            with any table whose sharding node is in the same data node -->
        <table name="company" primaryKey="ID" type="global" dataNode="dn1" />
        <table name="goods" primaryKey="ID" type="global" dataNode="dn2" />
        <!-- random sharding using mod sharind rule -->
        <table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1"
               rule="mod-long" />
        <table name="employee" primaryKey="ID" dataNode="dn1,dn2"
               rule="sharding-by-intfile" />
        <table name="customer" primaryKey="ID" dataNode="dn1,dn2"
               rule="sharding-by-intfile">
    </schema>
        <!-- database 是MySQL數據庫的庫名 -->
    <dataNode name="dn1" dataHost="localhost1" database="db1" />
    <dataNode name="dn2" dataHost="localhost1" database="db3" />
    <!--
    dataNode節點中各屬性說明:
    name:指定邏輯數據節點名稱;
    dataHost:指定邏輯數據節點物理主機節點名稱;
    database:指定物理主機節點上。若是一個節點上有多個庫,可以使用表達式db$0-99,     表示指定0-99這100個數據庫;

    dataHost 節點中各屬性說明:
        name:物理主機節點名稱;
        maxCon:指定物理主機服務最大支持1000個鏈接;
        minCon:指定物理主機服務最小保持10個鏈接;
        writeType:指定寫入類型;
            0,只在writeHost節點寫入;
            1,在全部節點都寫入。慎重開啓,多節點寫入順序爲默認寫入根據配置順序,第一個掛掉切換另外一個;
        dbType:指定數據庫類型;
        dbDriver:指定數據庫驅動;
        balance:指定物理主機服務的負載模式。
            0,不開啓讀寫分離機制;
            1,所有的readHost與stand by writeHost參與select語句的負載均衡,簡單的說,當雙主雙從模式(M1->S1,M2->S2,而且M1與 M2互爲主備),正常狀況下,M2,S1,S2都參與select語句的負載均衡;
            2,全部的readHost與writeHost都參與select語句的負載均衡,也就是說,當系統的寫操做壓力不大的狀況下,全部主機均可以承擔負載均衡;
-->
    <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="10.223.138.154:3316" user="root" password="123">
            <!-- 能夠配置多個從庫 -->
            <readHost host="hostS1" url="10.223.138.154:3317" user="root" password="123" />
        </writeHost>
    </dataHost>
    <dataHost name="localhost2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <!-- 能夠配置多個主從 -->
        <writeHost host="hostM2" url="10.223.138.154:3318" user="root" password="123">
            <!-- 能夠配置多個從庫 -->
            <readHost host="hostS2" url="10.223.138.154:3319" user="root" password="123" />
        </writeHost>
    </dataHost>
</mycat:schema>

 

這個配置好了以後配置server.xml

vi server.xml #只要注意以下部分便可,其餘的不要動
 <user name="root">
                <property name="password">mysql</property>
                <property name="schemas">TESTDB</property>

                <!-- 表級 DML 權限設置 -->
                <!--
                <privileges check="false">
                        <schema name="TESTDB" dml="0110" >
                                <table name="tb01" dml="0000"></table>
                                <table name="tb02" dml="1111"></table>
                        </schema>
                </privileges>
                 -->
        </user>

 

 這個是比較容易配置的,只要改下password就能夠,另外要保證 <property name="schemas">TESTDB</property>這個標籤裏的要和schema.xml的schema標籤的name一致

 

cd ../bin
./mycat start #開啓mycat

 6、鏈接mycatweb和mycat

訪問地址:10.223.138.154:8082/mycat/

頁面如圖所示,而後配置mycat:

而後就能夠本身試試mycat-web的豐富功能了。

期間踩了無數的坑,遇到了無數個報錯,要是有朋友耐心看到這裏仍有錯誤解決不了,能夠聯繫個人QQ:343552378,你們一塊兒研究。

相關文章
相關標籤/搜索