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.****.net/codepen/article/details/52160715 具体说明在本地址查看 建议设置为 “2”
sync_binlog=2 # 同上注解
binlog-do-db=test # 需要实现复制的数据库 多个数据库可用逗号分隔然后添加即可
binlog_ignore_db=mysql # 忽略复制的数据库 配置
说明: 不加binlog-do-db和binlog_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,如果启动成功,你应该会看到类似下面的日志。 (如果上面四步成功 此步骤不用看)
[[email protected] ~]# 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
安装
[[email protected] 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下面 在这里提出便于下面 环境变量设置
[[email protected] ~]# yum install java-1.8.0-openjdk* -y # 云安装 jdk 卸载命令:rpm -qa | grep java | xargs rpm -e --nodeps
然后设置java环境变量
[[email protected] 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
[[email protected] amoeba]# source /etc/profile # 是配置起作用 此步必须的
测试是否安装成功
[[email protected] 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/ 然后解压
[[email protected] ~] unzip 压缩包名称; #注意如果提示 没有unzip 命令 需要安装命令 输入命令: yum -y install unzip 即可
[[email protected] local] mv 解压后的名称 重命名之后的名称(amoeba); # 此命令将解压包名称重命名
[[email protected] local] cd 重命名之后的名称(amoeba);
[[email protected] amoeba]# pwd #查看当前路径命令
/usr/local/amoeba
[[email protected] 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.xml和amoeba.xml,如果需要配置ip访问控制,还需要修改access_list.conf文件,
下面首先介绍dbServers.xml
[[email protected] 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
[[email protected] 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">
-
# 提供客户端连接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服务
[[email protected] amoeba]# /usr/local/amoeba/bin/launcher
Error: JAVA_HOME environment variable is not set. # 报错 说明jdk环境变量没有配置
[[email protected] 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
[[email protected] amoeba]# source /etc/profile
[[email protected] 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参数。
[[email protected] 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"
再次启动 下面标红出现表示启动成功
[[email protected] ~]# /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.
查看端口
[[email protected] ~]# 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数据库
[[email protected] ~]# 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:
[[email protected] ~]# 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:
[[email protected] ~]# service mysqld start
Starting MySQL.. SUCCESS!
slavedb:
[[email protected] ~]# 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:
[[email protected] ~]# 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安装: 推荐
-
环境说明:
-
mycat 需要jdk支持 所以需要安装jdk 并配置其环境变量 amoeba安装中有讲解
-
centos 7 jdk1.7
-
-
创建mycat用户
-
命令:useradd -d /home/mycat mycat
-
-
下载没有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
-
-
-
修改配置文件
-
主要修改两个配置文件: 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># 成功。。。。。