MySQL+MyCat 實現主從複製 讀寫分離

MySQL 實現主從複製

 

 

概述:

 

        

 

 

 

一,環境

         主機:

                   master 操作系統:centos 7

                   IP:192.168.200.130 (內網IP,以實際IP爲準)

                   MySQL版本:5.7

         從機:          

                   slave 操作系統:centos 7

                   IP:192.168.200.131 (內網IP,以實際IP爲準)

                   MySQL版本:5.7

 

二 ,配置

  •     第一步 配置主服務器(master)

 

   1、編輯數據庫配置文件my.cnf,一般在/etc/目錄下。

    

        命令 : vim/vi   /etc/my.cnf     

                    輸入上面命令後 點擊 字母‘i’編輯

 

        在[mysqld]的下面加入下面代碼:

log-bin=mysql-bin # 設置mysql二進制日誌文件名稱

server-id=190  # 服務唯一id表示  數字可以任意定義 只要是唯一的就行

innodb_flush_log_at_trx_commit=2 # https://blog.csdn.net/codepen/article/details/52160715 具體說明在本地址查看   建議設置爲 “2”

sync_binlog=2 # 同上註解 

binlog-do-db=test # 需要實現複製的數據庫 多個數據庫可用逗號分隔然後添加即可

binlog_ignore_db=mysql #    忽略複製的數據庫 配置

 

說明: 不加binlog-do-dbbinlog_ignore_db,那就表示備份全部數據庫。

 

        修改完成之後保存 

        

        命令: 先點擊 “esc”鍵盤  

                   然後 shift + :  wq

                   最後回車 保存成功

 

    2、然後重啓(master)MySQL:

            

            命令: service mysqld restart 

 

    3、登錄MySQL服務器。

            

            命令:mysql -uroot -p   (第一次進入有時不需要密碼 / 但有時也需要密碼且密碼是隨機的  此情況是版本而定  修改/查找密碼 問度娘

 

        在主服務器新建一個用戶賦予“REPLICATION SLAVE”的權限。你不需要再賦予其它的權限。在下面的命令,192.168.200.131是從服務器的IP,123456 是用戶密碼,這裏可以自行修改。

        mysql>CREATE USER '用戶名'@ 'IP' IDENTIFIED BY '123456';         # IP 可以設置爲 % 表示任何ip的服務都可以連接數據庫

        mysql>GRANT REPLICATION SLAVE ON *.* TO '用戶名'@'IP' IDENTIFIED BY '123456';

 

        你可以查看用戶是否創建成功。

        mysql>use mysql

        mysql>select * from user;

        如果存在自定義的用戶,則說明成功。

        

  •         第二步:配置從服務器(slave)

        

       命令: vim/vi   /etc/my.cnf     

                 輸入上面命令後 點擊 字母‘i’編輯

 

    1、編輯配置文件my.cnf,在[mysqld]下面加入:

            

        server-id=180  #  可以自己定義數字,只要保證唯一的就行。

 

    修改完成之後保存 

        

        命令: 先點擊 “esc”鍵盤  

                   然後 shift + :  wq

                   最後回車 保存成功

    

    2、保存文件並重啓mysqld。

 

       命令: service mysqld restart

    

    3、登錄mysql服務器,執行以下命令。

        mysql>CHANGE MASTER TO

                    MASTER_HOST='master IP',

                    MASTER_USER='用戶名',

                    MASTER_PASSWORD='密碼',

                    MASTER_PORT=3306,

                    MASTER_LOG_FILE='mysql-bin.000001',

                    MASTER_LOG_POS=98,

                    MASTER_CONNECT_RETRY=10;

    參數說明:

        MASTER_HOST:主服務器的IP。

        MASTER_USER:配置主服務器時建立的用戶名

        MASTER_PASSWORD:用戶密碼

        MASTER_PORT:主服務器mysql端口,如果未曾修改,默認即可。

        MASTER_LOG_FILE :對應master my.cnf 配置文件 log-bin 配置的 日誌名稱

    MASTER_CONNECT_RETRY : 每十秒訪問一下master日誌文件

注意  MASTER_LOG_FILE 文件名稱要與master log_bin保持一致,不然導致 Slave_IO_Running: NO

 

    4、啓動slave進程。

        mysql>START SLAVE;

        mysql>show slave status\G;

        如果Slave_IO_Running、Slave_SQL_Running狀態爲Yes則表明設置成功。

顯示如下:

*************************** 1. row ***************************

             Slave_IO_State: Waiting for master to send event

                Master_Host: 192.168.1.222

                Master_User: repl

                Master_Port: 3306

              Connect_Retry: 60

            Master_Log_File: log.000003

        Read_Master_Log_Pos: 98

            Relay_Log_File: mysqld-relay-bin.000002

              Relay_Log_Pos: 229

      Relay_Master_Log_File: log.000003

          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: 98

            Relay_Log_Space: 229

            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

1 row in set (0.00 sec)

顯示紅色那兩行,說明配置成功

 

    5、查看mysql的日誌,一般在/var/log/目錄下的mysqld.log,如果啓動成功,你應該會看到類似下面的日誌。 (如果上面四步成功   此步驟不用看)

        [root@localhost ~]# vi /etc/my.cnf

 

        091104 8:42:02 [Note] Slave I/O thread: connected to master ‘[email protected]:3306?, replication started in log ‘mysql-bin.000001? at position 98

        現在主服務器和從服務器已經配置好了。另外你可能需要設置主服務器的數據庫二進制日誌的過期時間,可以在配置文件中使用參數expire_logs_days來設定。

 

可能出現問題 

 

一、 無法連接主服務器

 

1.查看配置文件my.cnf: 

 

skip-networking #註釋掉 因爲它是屏蔽掉一切TCP/IP連接 

 

bind-address = 127.0.0.1 #它和上一個選項是異曲同工,要想遠程連接,也得註釋掉 

 

2.如果以上工作都做過還是出現: 

 

 

ERROR 2003 (HY000): Can't connect to MySQL server on '*.*.*.*' (113),

那就得考慮防火牆的問題了, 3306 端口可能沒有對外開放

   端口查詢

 

    命令: firewall-cmd --list-ports

 

   開放端口

 

      命令: firewall-cmd --zone=public --add-port=3306/tcp --permanent

 

   重啓防火牆

 

   命令:firewall-cmd --reload

 

 

 

 

二、Slave_IO_Running: No

 

1 mysql> show slave status\G;

2 *************************** 1. row ***************************

3 Slave_IO_State:

4 Master_Host: 101.200.*.*

5 Master_User: backup

6 Master_Port: 3306

7 Connect_Retry: 60

8 Master_Log_File: master-bin.000113

9 Read_Master_Log_Pos: 276925387

10 Relay_Log_File: mysql-relay.000001

11 Relay_Log_Pos: 4

12 Relay_Master_Log_File: master-bin.000113

13 Slave_IO_Running: No

14 Slave_SQL_Running: Yes

15 Replicate_Do_DB:

16 Replicate_Ignore_DB:

17 Replicate_Do_Table:

18 Replicate_Ignore_Table:

19 Replicate_Wild_Do_Table:

20 Replicate_Wild_Ignore_Table:

21 Last_Errno: 0

22 Last_Error:

23 Skip_Counter: 0

24 Exec_Master_Log_Pos: 276925387

25 Relay_Log_Space: 120

26 Until_Condition: None

27 Until_Log_File:

28 Until_Log_Pos: 0

29 Master_SSL_Allowed: No

30 Master_SSL_CA_File:

31 Master_SSL_CA_Path:

32 Master_SSL_Cert:

33 Master_SSL_Cipher:

34 Master_SSL_Key:

35 Seconds_Behind_Master: NULL

36 Master_SSL_Verify_Server_Cert: No

37 Last_IO_Errno: 1236

38 Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

39 Last_SQL_Errno: 0

40 Last_SQL_Error:

41 Replicate_Ignore_Server_Ids:

42 Master_Server_Id: 21

43 Master_UUID: e4a43da7-5b58-11e5-a12f-00163e003632

44 Master_Info_File: /home/data/mysql/master.info

45 SQL_Delay: 0

46 SQL_Remaining_Delay: NULL

47 Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

48 Master_Retry_Count: 86400

49 Master_Bind:

50 Last_IO_Error_Timestamp: 170204 10:48:06

51 Last_SQL_Error_Timestamp:

52 Master_SSL_Crl:

53 Master_SSL_Crlpath:

54 Retrieved_Gtid_Set:

55 Executed_Gtid_Set:

56 Auto_Position: 0

57 1 row in set (0.00 sec)

58

59 ERROR:

60 No query specified

 

解決方法:

    首先在從庫上執行

       命令 :stop slave;

    然後查看主庫master狀態

   

1 mysql> show master status\G;

2 *************************** 1. row ***************************

3 File: mysql-bin.000113

4 Position: 276925387

5 Binlog_Do_DB:

6 Binlog_Ignore_DB:

7 Executed_Gtid_Set:

8 1 row in set (0.00 sec)

9

10 ERROR:

11 No query specified

12

13 mysql> flush logs;

14 Query OK, 0 rows affected (0.11 sec)

 

 刷新binlog

    命令 : flush logs;

 你會返現   File: mysql-bin.000113 會變成 mysql-bin.000114

 

再次查看master狀態

mysql> show master status\G;

2 *************************** 1. row ***************************

3 File: mysql-bin.000114

4 Position: 120

5 Binlog_Do_DB:

6 Binlog_Ignore_DB:

7 Executed_Gtid_Set:

8 1 row in set (0.00 sec)

9

10 ERROR:

11 No query specified

 

然後就不需要在操作master,切換到從庫slave

 

輸入以下命令:

mysql>CHANGE MASTER TO

      MASTER_HOST='master IP',

      MASTER_USER='用戶名', # 在master主服務中通過 gran(mysql創建用戶並授權語句)

      MASTER_PASSWORD='密碼',

      MASTER_PORT=3306,

      MASTER_LOG_FILE='mysql-bin.000114',

      MASTER_LOG_POS=120, # 對應 master主機的 Position

      MASTER_CONNECT_RETRY=10;

 

 

重啓slave 

    命令:start slave;

 

查看slave狀態

1 mysql> show slave status\G;

2 *************************** 1. row ***************************

3 Slave_IO_State: Waiting for master to send event

4 Master_Host: 101.200.*.*

5 Master_User: backup

6 Master_Port: 3306

7 Connect_Retry: 60

8 Master_Log_File: mysql-bin.000114

9 Read_Master_Log_Pos: 11314

10 Relay_Log_File: mysql-relay.000002

11 Relay_Log_Pos: 11477

12 Relay_Master_Log_File: mysql-bin.000114

13 Slave_IO_Running: Yes

14 Slave_SQL_Running: Yes

15 Replicate_Do_DB:

16 Replicate_Ignore_DB:

17 Replicate_Do_Table:

18 Replicate_Ignore_Table:

19 Replicate_Wild_Do_Table:

20 Replicate_Wild_Ignore_Table:

21 Last_Errno: 0

22 Last_Error:

23 Skip_Counter: 0

24 Exec_Master_Log_Pos: 11314

25 Relay_Log_Space: 11646

26 Until_Condition: None

27 Until_Log_File:

28 Until_Log_Pos: 0

29 Master_SSL_Allowed: No

30 Master_SSL_CA_File:

31 Master_SSL_CA_Path:

32 Master_SSL_Cert:

33 Master_SSL_Cipher:

34 Master_SSL_Key:

35 Seconds_Behind_Master: 0

36 Master_SSL_Verify_Server_Cert: No

37 Last_IO_Errno: 0

38 Last_IO_Error:

39 Last_SQL_Errno: 0

40 Last_SQL_Error:

41 Replicate_Ignore_Server_Ids:

42 Master_Server_Id: 21

43 Master_UUID: e4a43da7-5b58-11e5-a12f-00163e003632

44 Master_Info_File: /home/data/mysql/master.info

45 SQL_Delay: 0

46 SQL_Remaining_Delay: NULL

47 Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

48 Master_Retry_Count: 86400

49 Master_Bind:

50 Last_IO_Error_Timestamp:

51 Last_SQL_Error_Timestamp:

52 Master_SSL_Crl:

53 Master_SSL_Crlpath:

54 Retrieved_Gtid_Set:

55 Executed_Gtid_Set:

56 Auto_Position: 0

57 1 row in set (0.00 sec)

58

59 ERROR:

60 No query specified

 

 

成功:然後就可以在主服務(master)中創建數據庫  然後查看從服務(slave) 是否有同樣的數據庫創建  有  則表示成功;

 

Amoeba的安裝  不推薦

缺點:

 

 

  • 先介紹下部署環境:

amoeba:192.168.200.132

masterDB:192.168.200.130

slaveDB:192.168.200.131

以上系統全爲centos7

 

Amoeba框架是居於JDK1.5開發的,採用了JDK1.5的特性,所以還需要安裝java環境,建議使用javaSE1.5+以上的JDK版本

1、安裝java環境

方法一: 先去官網下載:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

安裝

[root@bogon src]# rpm -ivh jdk-8u111-linux-x64.rpm

Preparing... ########################################### [100%]

1:jdk1.8.0_111 ########################################### [100%]

Unpacking JAR files...

tools.jar...

plugin.jar...

javaws.jar...

deploy.jar...

rt.jar...

jsse.jar...

charsets.jar...

localedata.jar...

方法二:

安裝目錄一般在/usr/lib/jvm下面            在這裏提出便於下面 環境變量設置

[root@bogon ~]# yum install java-1.8.0-openjdk* -y  # 雲安裝 jdk           卸載命令:rpm -qa | grep java | xargs rpm -e --nodeps

 

然後設置java環境變量

[root@bogon src]# vim /etc/profile # 輸入命令後點擊 “i” 進行編輯

在文本最後一行輸入下面內如:

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64

export PATH=$PATH:$JAVA_HOME/bin

[root@bogon amoeba]# source /etc/profile # 是配置起作用 此步必須的

測試是否安裝成功

[root@bogon src]# java -version

java version "1.8.0_111"

Java(TM) SE Runtime Environment (build 1.8.0_111-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

2、安裝Amoeba

可以從https://sourceforge.net/projects/amoeba/下載最新版本的Amoeba,我這裏下載的是amoeba-mysql-3.0.5-RC-distribution.zip。Amoeba安裝非常簡單,直接解壓即可使用,這裏將Amoeba解壓到/usr/local/amoeba目錄下,這樣就安裝完成了

將下載的壓縮包放在/usr/local/ 然後解壓

[root@bogon ~] unzip 壓縮包名稱; #注意如果提示 沒有unzip 命令 需要安裝命令 輸入命令: yum -y install unzip 即可

[root@bogon local] mv 解壓後的名稱 重命名之後的名稱(amoeba); # 此命令將解壓包名稱重命名

[root@bogon local] cd 重命名之後的名稱(amoeba);

[root@bogon amoeba]# pwd #查看當前路徑命令

/usr/local/amoeba

  [root@bogon amoeba]# ll #當前目錄文件/文件夾

  總用量 20

  drwxrwxrwx. 2 root root 4096 7月 5 2013 benchmark

  drwxrwxrwx. 2 root root 4096 7月 5 2013 bin

  drwxrwxrwx. 2 root root 4096 7月 5 2013 conf

  -rwxrwxrwx. 1 root root 728 7月 5 2013 jvm.properties

  drwxrwxrwx. 2 root root 4096 7月 5 2013 lib

 

3、配置Amoeba

Amoeba的配置文件在本環境下位於/usr/local/amoeba/conf目錄下。

配置文件比較多,但是僅僅使用讀寫分離功能,只需配置兩個文件即可,

分別是dbServers.xmlamoeba.xml,如果需要配置ip訪問控制,還需要修改access_list.conf文件,

下面首先介紹dbServers.xml

[root@bogon amoeba]# vim conf/dbServers.xml

<?xml version="1.0" encoding="gbk"?>

 

<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">

<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">

 

<!--

Each dbServer needs to be configured into a Pool,

If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:

add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig

such as 'multiPool' dbServer

-->

<dbServer name="abstractServer" abstractive="true">

<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">

<property name="connectionManager">${defaultManager}</property>

<property name="sendBufferSize">64</property>

<property name="receiveBufferSize">128</property>

<!-- mysql port -->

<property name="port">3306</property>  #設置Amoeba要連接的mysql數據庫的端口,默認是3306

<!-- mysql schema -->

<property name="schema">testdb</property>  #設置缺省的數據庫,當連接amoeba時,操作表必須顯式的指定數據庫名,即採用dbname.tablename的方式,不支持 use dbname指定缺省庫,因爲操作會調度到各個後端dbserver

<!-- mysql user -->

<property name="user">test1</property>  #設置amoeba連接後端數據庫服務器的賬號和密碼,因此需要在所有後端數據庫上創建該用戶(在master和slave 上面創建此用戶名並授權),並授權amoeba服務器可連接

<property name="password">111111</property>

</factoryConfig>

 

<poolConfig class="com.meidusa.toolkit.common.poolable.PoolableObjectPool">

<property name="maxActive">500</property>  #最大連接數,默認500

<property name="maxIdle">500</property>    #最大空閒連接數

<property name="minIdle">1</property>    #最新空閒連接數

<property name="minEvictableIdleTimeMillis">600000</property>

<property name="timeBetweenEvictionRunsMillis">600000</property>

<property name="testOnBorrow">true</property>

<property name="testOnReturn">true</property>

<property name="testWhileIdle">true</property>

</poolConfig>

</dbServer>

 

<dbServer name="master" parent="abstractServer">  #設置一個後端可寫(master)的dbServer,這裏自定義爲writedb,這個名字可以任意命名,後面還會用到

<factoryConfig>

<!-- mysql ip -->

<property name="ipAddress">192.168.200.130/property> #設置後端master可寫dbserver

</factoryConfig>

</dbServer>

<dbServer name="slave" parent="abstractServer">  #設置後端可讀(slave)的dbserver

<factoryConfig>

<!-- mysql ip -->

<property name="ipAddress">192.168.200.131</property>

</factoryConfig>

</dbServer>

<dbServer name="myslave" virtual="true">  #設置定義一個虛擬的dbserver,實際上相當於一個dbserver組,這裏將可讀(slave的數據庫ip統一放到一個組中,將這個組的名字命名爲myslave

<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">

<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->

<property name="loadbalance">1</property>  #選擇調度算法,1表示複製均衡,2表示權重,3表示HA, 這裏選擇1

<!-- Separated by commas,such as: server1,server2,server1 -->

<property name="poolNames">slave</property>  #myslave組成員

</poolConfig>

</dbServer>

</amoeba:dbServers>

 

另一個配置文件amoeba.xml

[root@bogon amoeba]# cat conf/amoeba.xml

<?xml version="1.0" encoding="gbk"?>

 

<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">

<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">

 

<proxy>

<!-- service class must implements com.meidusa.amoeba.service.Service -->

<service name="Amoeba for Mysql" class="com.meidusa.amoeba.mysql.server.MySQLService">

<!-- port -->

<property name="port">8066</property>    #設置amoeba監聽的端口,默認是8066

<!-- bind ipAddress -->    #下面配置監聽的接口,如果不設置,默認監聽所以的IP

<!--

<property name="ipAddress">127.0.0.1</property>

-->

<property name="connectionFactory">

<bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">

<property name="sendBufferSize">128</property>

<property name="receiveBufferSize">64</property>

</bean>

</property>

<property name="authenticateProvider">

<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

  1. # 提供客戶端連接amoeba時需要使用這裏設定的賬號 (這裏的賬號密碼和amoeba連接後端數據庫服務器的賬號密碼無關)

<property name="user">root</property>    

 

<property name="password">123456</property>

<property name="filter">

<bean class="com.meidusa.toolkit.net.authenticate.server.IPAccessController">

<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>

</bean>

</property>

</bean>

</property>

</service>

<runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">

<!-- proxy server client process thread size -->

<property name="executeThreadSize">128</property>

<!-- per connection cache prepared statement size -->

<property name="statementCacheSize">500</property>

<!-- default charset -->

<property name="serverCharset">utf8</property>

<!-- query timeout( default: 60 second , TimeUnit:second) -->

<property name="queryTimeout">60</property>

</runtime>

</proxy>

<!--

Each ConnectionManager will start as thread

manager responsible for the Connection IO read , Death Detection

-->

<connectionManagerList>

<connectionManager name="defaultManager" class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper">

<property name="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</property>

</connectionManager>

</connectionManagerList>

<!-- default using file loader -->

<dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">

<property name="configFile">${amoeba.home}/conf/dbServers.xml</property>

</dbServerLoader>

<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">

<property name="ruleLoader">

<bean class="com.meidusa.amoeba.route.TableRuleFileLoader">

<property name="ruleFile">${amoeba.home}/conf/rule.xml</property>

<property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>

</bean>

</property>

<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>

<property name="LRUMapSize">1500</property>

<property name="defaultPool">master</property>  #設置amoeba默認的池,這裏設置爲writedb

#<property name="writePool">master</property>  #這兩個選項默認是註銷掉的,需要取消註釋,這裏用來指定前面定義好的倆個讀寫池

#<property name="readPool">myslave</property>   #

<property name="needParse">true</property>

</queryRouter>

</amoeba:configuration>

4、在masterdb上創建數據庫testdb

mysql> create database testdb;

Query OK, 1 row affected (0.08 sec)

 

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mydb |

| mysql |

| performance_schema |

| test |

| testdb |

+--------------------+

6 rows in set (0.00 sec)

查看slavedb是否複製成功

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mydb |

| mysql |

| performance_schema |

| test |

| testdb |

+--------------------+

6 rows in set (0.00 sec)

分別在masterdb和slavedb上爲amoedb授權

 

mysql> GRANT ALL ON testdb.* TO 'test1'@'192.168.2.203' IDENTIFIED BY '111111'; # 在master 和 slave 上同時創建此用戶

Query OK, 0 rows affected (0.05 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.02 sec)

啓動amoeba服務

[root@bogon amoeba]# /usr/local/amoeba/bin/launcher

Error: JAVA_HOME environment variable is not set. # 報錯 說明jdk環境變量沒有配置

[root@bogon amoeba]# vim /etc/profile

        重複前面的 java環境變量設置操作

        在文本最後一行輸入下面內如:

        export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64

        export PATH=$PATH:$JAVA_HOME/bin

[root@bogon amoeba]# source /etc/profile

[root@bogon amoeba]# /usr/local/amoeba/bin/launcher

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=16m; support was removed in 8.0

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=96m; support was removed in 8.0

 

The stack size specified is too small, Specify at least 228k

Error: Could not create the Java Virtual Machine.

Error: A fatal exception has occurred. Program will exit.

報錯:

Error: Could not create the Java Virtual Machine.

Error: A fatal exception has occurred. Program will exit.

從錯誤文字上看,應該是由於stack size太小,導致JVM啓動失敗,要如何修改呢?

其實Amoeba已經考慮到這個問題,並將JVM參數配置寫在屬性文件裏。現在,讓我們通過該屬性文件修改JVM參數。

修改jvm.properties文件JVM_OPTIONS參數。

[root@bogon amoeba]# vim /usr/local/amoeba/jvm.properties

改成:JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"

原爲:JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"

再次啓動  下面標紅出現表示啓動成功

[root@bogon ~]# /usr/local/amoeba/bin/launcher

at org.codehaus.plexus.classworlds.launcher.Launcher.launchStandard(Launcher.java:329)

at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:239)

at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)

at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:127)

at org.codehaus.classworlds.Launcher.main(Launcher.java:110)

Caused by: com.meidusa.toolkit.common.bean.util.InitialisationException: default pool required!,defaultPool=writedb invalid

at com.meidusa.amoeba.route.AbstractQueryRouter.init(AbstractQueryRouter.java:469)

at com.meidusa.amoeba.context.ProxyRuntimeContext.initAllInitialisableBeans(ProxyRuntimeContext.java:337)

... 11 more

2016-10-24 18:46:37 [INFO] Project Name=Amoeba-MySQL, PID=1577 , System shutdown ....

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=16m; support was removed in 8.0

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=96m; support was removed in 8.0

2016-10-24 18:50:19 [INFO] Project Name=Amoeba-MySQL, PID=1602 , starting...

log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml

2016-10-24 18:50:21,668 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA

log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf

2016-10-24 18:50:22,852 INFO net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:8066.

查看端口

[root@bogon ~]# netstat -unlpt | grep java ##註解### 如果查不到說明端口沒開放 執行命令:開放端口 命令: firewall-cmd --zone=public --add-port=3306/tcp --permanent 重啓防火牆  命令:firewall-cmd --reload

tcp 0 0 :::8066 :::* LISTEN 1602/java

由此可知Amoeba啓動正常

 

5、測試

遠程登陸mysql客戶端通過指定amoeba配置文件中指定的用戶名、密碼、和端口以及amoeba服務器ip地址鏈接mysql數據庫

[root@lys2 ~]# mysql -h192.168.200.132(amoeba ip) -u用戶名t -p -P8066 # 再次輸入的是在配置文件中amoebs.xml 的user和password

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1364055863

Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA Source distribution

 

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

在testdb中創建表test並插入數據

mysql> use testdb;

Database changed

mysql> create table test_table(id int,password varchar(40) not null);

Query OK, 0 rows affected (0.19 sec)

 

mysql> show tables;

+------------------+

| Tables_in_testdb |

+------------------+

| test_table |

+------------------+

1 row in set (0.02 sec)

 

mysql> insert into test_table(id,password) values('1','test1');

Query OK, 1 row affected (0.04 sec)

 

mysql> select * from test_table;

+------+----------+

| id | password |

+------+----------+

| 1 | test1 |

+------+----------+

1 row in set (0.02 sec)

分別登陸masterdb和slavedb查看數據

masterdb:

mysql> use testdb;

Database changed

mysql> show tables;

+------------------+

| Tables_in_testdb |

+------------------+

| test_table |

+------------------+

1 row in set (0.00 sec)

 

mysql> select * from test_table;

+------+----------+

| id | password |

+------+----------+

| 1 | test1 |

+------+----------+

1 row in set (0.03 sec)

slavedb:

mysql> use testdb;

Database changed

mysql> show tables;

+------------------+

| Tables_in_testdb |

+------------------+

| test_table |

+------------------+

1 row in set (0.00 sec)

 

mysql> select * from test_table;

+------+----------+

| id | password |

+------+----------+

| 1 | test1 |

+------+----------+

1 row in set (0.00 sec)

停掉masterdb,然後在客戶端分別執行插入和查詢功能

masterdb:

  [root@bogon ~]# service mysqld stop

  Shutting down MySQL. SUCCESS!

客戶端:

mysql> insert into test_table(id,password) values('2','test2');

ERROR 1044 (42000): Amoeba could not connect to MySQL server[192.168.2.204:3306],拒絕連接

mysql> select * from test_table;

+------+----------+

| id | password |

+------+----------+

| 1 | test1 |

+------+----------+

1 row in set (0.01 sec)

可以看到,關掉masterdb和寫入報錯,讀正常

開啓masterdb上的msyql 關閉slave上的mysql

masterdb:

[root@bogon ~]# service mysqld start

Starting MySQL.. SUCCESS!

slavedb:

[root@localhost ~]# service mysqld stop

Shutting down MySQL. SUCCESS!

客戶端再次嘗試

mysql> insert into test_table(id,password) values('2','test2');

Query OK, 1 row affected (0.19 sec)

 

mysql> select * from test_table;

ERROR 1044 (42000): poolName=myslave, no valid pools

可以看到插入成功,讀取失敗

 

開啓slavedb上的mysql,查看數據是否自動同步

slavedb:

[root@localhost ~]# service mysqld start

Starting MySQL... SUCCESS!

客戶端:

mysql> select * from test_table;

+------+----------+

| id | password |

+------+----------+

| 1 | test1 |

| 2 | test2 |

+------+----------+

2 rows in set (0.01 sec)

接着客戶端:

mysql> insert into test_table(id,password) values('3','test3');

Query OK, 1 row affected (0.03 sec)

 

mysql> select * from test_table;

+------+----------+

| id | password |

+------+----------+

| 1 | test1 |

| 2 | test2 |

| 3 | test3 |

+------+----------+

3 rows in set (0.02 sec)

OK 一切正常,到此全部結束

MyCat安裝: 推薦

    

 

  1.     環境說明:

    • mycat 需要jdk支持 所以需要安裝jdk 並配置其環境變量 amoeba安裝中有講解

    • centos 7   jdk1.7

  2.           創建mycat用戶

    • 命令:useradd -d /home/mycat mycat

  3.         下載沒有cat http://dl.mycat.io/1.6.5/

    • 選擇Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

    • 解壓並放到local目錄下

      • tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

      • mv Mycat-server-1.6.5-release-20180122220033-linux  mycat  #修改文件名爲mycat

  4.          修改配置文件

    • 主要修改兩個配置文件: schema.xml和server.xml    # 位置在mycat文件夾的conf下面

schema.xml 注:本次僅僅配置讀寫分離,所以多餘的都刪掉了

                    

<?xml version="1.0"?>

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">

<mycat:schema xmlns:mycat="http://io.mycat/">

<!-- TESTDB1 是mycat的邏輯庫名稱,鏈接需要用的 -->

<schema name="TESTDB1" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>

<!-- database 是MySQL數據庫的庫名 -->

<dataNode name="dn1" dataHost="localhost1" database="數據名稱" />

<!--

dataNode節點中各屬性說明:

name:指定邏輯數據節點名稱;可自定義但要與<schema>標籤中的dataNode保持一致

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="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">

<heartbeat>select user()</heartbeat>

<!-- 可以配置多個主從 -->

<writeHost host="master" url="主庫IP:3306" user="root" password="mysql1234">

<!-- 可以配置多個從庫 -->

<readHost host="slave" url="從庫IP:3306" user="root" password="mysql1234" />

</writeHost>

</dataHost>

</mycat:schema>

 

 

server.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- - - Licensed under the Apache License, Version 2.0 (the "License");

- you may not use this file except in compliance with the License. - You

may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0

- - Unless required by applicable law or agreed to in writing, software -

distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT

WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the

License for the specific language governing permissions and - limitations

under the License. -->

<!DOCTYPE mycat:server SYSTEM "server.dtd">

<mycat:server xmlns:mycat="http://io.mycat/">

<system>

<property name="nonePasswordLogin">0</property> <!-- 0爲需要密碼登陸、1爲不需要密碼登陸 ,默認爲0,設置爲1則需要指定默認賬戶-->

<property name="useHandshakeV10">1</property>

<property name="useSqlStat">1</property> <!-- 1爲開啓實時統計、0爲關閉 -->

<property name="useGlobleTableCheck">0</property> <!-- 1爲開啓全加班一致性檢測、0爲關閉 -->

<property name="sequnceHandlerType">2</property>

<property name="subqueryRelationshipCheck">false</property> <!-- 子查詢中存在關聯查詢的情況下,檢查關聯字段中是否有分片字段 .默認 false -->

<!-- <property name="useCompression">1</property>--> <!--1爲開啓mysql壓縮協議-->

<!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--設置模擬的MySQL版本號-->

<!-- <property name="processorBufferChunk">40960</property> -->

<!--

<property name="processors">1</property>

<property name="processorExecutor">32</property>

-->

<!--默認爲type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->

<property name="processorBufferPoolType">0</property>

<!--默認是65535 64K 用於sql解析時最大文本長度 -->

<!--<property name="maxStringLiteralLength">65535</property>-->

<!--<property name="sequnceHandlerType">0</property>-->

<!--<property name="backSocketNoDelay">1</property>-->

<!--<property name="frontSocketNoDelay">1</property>-->

<!--<property name="processorExecutor">16</property>-->

<!--

<property name="serverPort">8066</property> <property name="managerPort">9066</property>

<property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>

<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->

<!--分佈式事務開關,0爲不過濾分佈式事務,1爲過濾分佈式事務(如果分佈式事務內只涉及全局表,則不過濾),2爲不過濾分佈式事務,但是記錄分佈式事務日誌-->

<property name="handleDistributedTransactions">0</property>

<!--

off heap for merge/order/group/limit 1開啓 0關閉

-->

<property name="useOffHeapForMerge">1</property>

<!--

單位爲m

-->

<property name="memoryPageSize">64k</property>

<!--

單位爲k

-->

<property name="spillsFileBufferSize">1k</property>

<property name="useStreamOutput">0</property>

<!--

單位爲m

-->

<property name="systemReserveMemorySize">384m</property>

<!--是否採用zookeeper協調切換 -->

<property name="useZKSwitch">false</property>

<!-- XA Recovery Log日誌路徑 -->

<!--<property name="XARecoveryLogBaseDir">./</property>-->

<!-- XA Recovery Log日誌名稱 -->

<!--<property name="XARecoveryLogBaseName">tmlog</property>-->

</system>

<!-- 全局SQL防火牆設置 -->

<!--白名單可以使用通配符%或着*-->

<!--例如<host host="127.0.0.*" user="root"/>-->

<!--例如<host host="127.0.*" user="root"/>-->

<!--例如<host host="127.*" user="root"/>-->

<!--例如<host host="1*7.*" user="root"/>-->

<!--這些配置情況下對於127.0.0.1都能以root賬戶登錄-->

<!--

<firewall>

<whitehost>

<host host="1*7.0.0.*" user="root"/>

</whitehost>

<blacklist check="false">

</blacklist>

</firewall>

-->

<!-- 讀寫都可用的用戶 提供給客戶端使用的 -->

<user name="root" defaultAccount="true">

<property name="password">123456</property>

<property name="schemas">TESTDB1</property>  #與schame.xml  <schema name="TESTDB1" ...></schema>  保持一致

 

<!-- 表級 DML 權限設置 -->

<!--

<privileges check="false">

<schema name="TESTDB" dml="0110" >

<table name="tb01" dml="0000"></table>

<table name="tb02" dml="1111"></table>

</schema>

</privileges>

-->

</user>

 

<!-- 只讀用戶 提供給客戶端使用的-->

<user name="user">

<property name="password">user</property>

<property name="schemas">TESTDB1</property>

<property name="readOnly">true</property>

</user>

 

</mycat:server>

 

 

啓動之前最好將log的級別改爲debug,這樣方便查錯

修改方法:

進入mycat/conf

vim log4j2.xml

<asyncRoot level="info" includeLocation="true">

改爲

<asyncRoot level="trace" includeLocation="true">

 

 

啓動

進入mycat/bin

    cd ~/local/mycat/bin

  ./mycat start

驗證:

客戶端服務器 安裝mysql後

 

mysql -u用戶名 -p -h mycat服務ip -P8066 TESTDB1   # 連接mycat   注意mycat服務器 8066 和 9066 端口開放

 

mysql># 成功。。。。。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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