1. 程式人生 > >amoeba搭建mysql讀寫分離

amoeba搭建mysql讀寫分離

實驗環境5臺centos7 amoeba192.168.10.1, master192.168.10.10, slave1192.168.10.20, slave2192.168.10.21, client192.168.10.100, 用到的軟體amoeba 優點:負載均衡,高可用性,sql過濾,讀寫分離 先實現主從複製 master搭建ntp時間同步伺服器 修改 /etc/ntp.conf 21 server 127.127.1.0 22 fudge 127.127.1.0 startum 8 啟動服務 [[email protected] ~]# service ntpd start Redirecting to /bin/systemctl start ntpd.service 然後在slave1,slave2同步時間 yum -y install ntpdate [

[email protected] ~]# ntpdate 192.168.10.10 24 Aug 11:22:16 ntpdate[59598]: step time server 192.168.10.10 offset 13.194224 sec 修改master上面的/etc/my.cnf [[email protected] ~]# cd /usr/share/mysql [[email protected] mysql]# cp my-medium.cnf /etc/my.cnf cp: overwrite ‘/etc/my.cnf’? y [[email protected]
~]# vim /etc/my.cnf 54 log-bin=mysql-bin 55 log-slave-updates=true 63 server-id = 10 [[email protected] ~]# service mariadb restart Redirecting to /bin/systemctl restart mariadb.service 給slave伺服器授權 [[email protected] ~]# mysql -uroot -p123123 -e “grant replication slave on . to ‘myslave’@’%’ identified by ‘123123’” [
[email protected]
~]# mysql -uroot -p123123 -e “flush privileges” [[email protected] ~]# mysql -uroot -p123123 -e “show master status” +——————+———-+————–+——————+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +——————+———-+————–+——————+ | mysql-bin.000001 | 465 | | | +——————+———-+————–+——————+ 配置slave伺服器 修改my.cnf 55 relay-log=relay-log-bin 56 relay-log-index=slave-relay-bin.index 64 server-id = 20 [[email protected] mysql]# service mariadb restart Redirecting to /bin/systemctl restart mariadb.service [[email protected] mysql]# mysql -uroot -p123123 -e “change master to master_host=’192.168.10.10’,master_user=’myslave’,master_password=’123123’,master_log_file=’mysql-bin.000001’,master_log_pos=465” [[email protected] mysql]# mysql -uroot -p123123 -e “start slave” [[email protected] mysql]# mysql -uroot -p123123 -e “show slave status \G” Slave_IO_Running: Yes Slave_SQL_Running: Yes 配置slave2 把ID=10改為21 其他的步驟一樣 ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MariaDB error log報錯解決辦法 slave stop; reset slave;成功

安裝amoeba需要java環境,建議jdk1.5或者1.6 [[email protected] ~]# rpm -qa |grep java xz-java-1.3-3.el7.noarch java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 tzdata-java-2018e-3.el7.noarch python-javapackages-3.4.1-11.el7.noarch javapackages-tools-3.4.1-11.el7.noarch java-1.7.0-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64 java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64 java-1.8.0-openjdk-devel-1.8.0.181-3.b13.el7_5.x86_64 httpcomponents-client-javadoc-4.2.5-5.el7_0.noarch javassist-3.16.1-10.el7.noarch httpunit-javadoc-1.7-15.el7.noarch httpcomponents-core-javadoc-4.2.4-6.el7.noarch javamail-1.4.6-8.el7.noarch java-1.7.0-openjdk-headless-1.7.0.191-2.6.15.4.el7_5.x86_64 [[email protected] ~]# rpm -e java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 –nodeps && rpm -e java-1.7.0-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64 –nodeps && rpm -e java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64 –nodeps && rpm -e java-1.8.0-openjdk-devel-1.8.0.181-3.b13.el7_5.x86_64 –nodeps && rpm -e java-1.7.0-openjdk-headless-1.7.0.191-2.6.15.4.el7_5.x86_64 –nodeps 下載jdk1.5或者1.6原始碼 [[email protected] ~]# java -version java version “1.6.0_14” Java(TM) SE Runtime Environment (build 1.6.0_14-b08) Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode) 安裝好了然後安裝amoeba 放入原始碼包,mkdir /usr/local/amoeba tar xf amoeba* -C /usr/local/amoeba 然後設定環境變數 vim /etc/profile 加入export JAVA_HOME=/usr/local/java export CLASSPATH=CLASSPATH:JAVA_HOME/lib:JAVAHOME/jre/libexportPATH=PATH:JAVAHOME/bin:JAVA_HOME/jre/bin export AMOEBA_HOME=/usr/local/amoeba/ export PATH=PATH:AMOEBA_HOME/bin . /etc/profile 執行[[email protected] ~]# /usr/local/amoeba/bin/amoeba amoeba start|stop 出現如上即為正確 下面配置mysql 以上皆為amoeba主機的配置 接下來配置3臺mysql以及授權amoeba master [[email protected] ~]# service mariadb start Redirecting to /bin/systemctl start mariadb.service [[email protected] ~]# mysqladmin -uroot password 123123 [[email protected] ~]# mysql -uroot -p -e “grant all on . to ‘admin’@’%’ identified by ‘123123’” [[email protected] ~]# mysql -uroot -p123123 -e “flush privileges” slave1 [[email protected] ~]# service mariadb start Redirecting to /bin/systemctl start mariadb.service [[email protected] ~]# mysqladmin -uroot password 123123 [[email protected] ~]# mysql -uroot -p123123 -e “grant all on . to ‘admin’@’%’ identified by ‘123123’” [[email protected] ~]# mysql -uroot -p123123 -e “flush privileges” slave2 [[email protected] ~]# service mariadb start Redirecting to /bin/systemctl start mariadb.service [[email protected] ~]# mysqladmin -uroot password 123123 [[email protected] ~]# mysql -uroot -p123123 -e “grant all on . to ‘admin’@’%’ identified by ‘123123’” [[email protected] ~]# mysql -uroot -p123123 -e “flush privileges” 接下來配置amoeba 修改amoeba配置檔案 [[email protected] ~]# cd /usr/local/amoeba/conf/ [[email protected] conf]# ls access_list.conf dbserver.dtd functionMap.xml rule.dtd amoeba.dtd dbServers.xml log4j.dtd ruleFunctionMap.xml amoeba.xml function.dtd log4j.xml rule.xml [[email protected] conf]# cp amoeba.xml{,.ori} 118066預設埠號不用修改 30amoeba後端連線的使用者名稱 32123123後端連線的密碼 115master預設伺服器池 117master寫伺服器池 118slaves讀伺服器池 接下來編輯[[email protected] conf]# cp dbServers.xml{,.ori} 設定mysql伺服器的使用者密碼,mysql節點的ip地址以及負載均衡演算法 203306設定mysql的埠 26admin設定mysql授權使用者名稱 27123123設定mysql授權密碼 43 設定master節點 44 45 46 192.168.10.10 47 masterip節點ip地址 48 49 50 設定slave1節點 51 52 53 192.168.10.20 54 slave1節點ip地址 55 56 57 設定slave2節點 58 59 60 192.168.10.21 61 slave2節點ip地址 62 63 64 設定slave 65 設定演算法 66 67 1 68 69 70 slave1,slave2 71 slave1,slave2 72 然後啟動[[email protected] conf]# /usr/local/amoeba/bin/amoeba start & [[email protected] conf]# netstat -anputl |grep java tcp6 0 0 :::8066 :::* LISTEN 59802/java tcp6 0 0 127.0.0.1:37768 :::* LISTEN 59802/java tcp6 0 0 192.168.10.2:37668 192.168.10.21:3306 ESTABLISHED 59802/java tcp6 0 0 192.168.10.2:46038 192.168.10.20:3306 ESTABLISHED 59802/java tcp6 0 0 192.168.10.2:45414 192.168.10.10:3306 ESTABLISHED 59802/java 、 測試client mysql -uamoeba -p123123 -h 192.168.10.2 -P 8066 MySQL [(none)]> select * from aaa.test; +———–+ | user_name | +———–+ | aaa | +———–+ 1 row in set (0.00 sec)

MySQL [(none)]> select * from aaa.test; +———–+ | user_name | +———–+ | bbb | +———–+ 讀輪詢 MySQL [(none)]> insert into aaa.test values(‘ddd’); 檢視master MariaDB [aaa]> select * from aaa.test; +———–+ | user_name | +———–+ | sujianyu | | ccc | | ddd | +———–+ 寫主,實現了讀寫分離重點內容