mysql主从+读写
Posted wanglinyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主从+读写相关的知识,希望对你有一定的参考价值。
开启主库binlog,从库不用开,配置主从库server-id(不能相同)
yum -y install mysql mysql-server
vim /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
/etc/init.d/mysqld restart
主库创建同步的用户
mysql -uroot -p123456
mysql> grant replication slave on *.* to ‘rep‘@‘10.0.0.%‘ identified by ‘123456‘;
mysql> flush privileges;
查看主服务器状态
#记录下日志文件名和位置,备用
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 467 | | | |
+------------------+----------+--------------+------------------+-------------------+
或者
mysql> show master statusG
配置mysql从服务器
yum -y install mysql mysql-server
vim /etc/my.cnf
[mysqld]
server-id=2
relay-log=mysql-relay
/etc/init.d/mysqld restart
关闭从服务器的复制功能
mysql -uroot -p123456
mysql> stop slave;
配置从服务器指向master
change master to master_host=‘192.168.15.129‘,
master_user=‘rep‘,
master_password=‘123456‘,
master_log_file=‘mysql-bin.000003‘,
master_log_pos=467;
开启从服务器的复制功能
mysql> start slave;
检查从服务器的复制功能状态
#检查画横线的两行是否变为yes
mysql> show slave statusG
mysql读写分离部署配置
上传jdk-8u60-linux-x64.tar.gz配置jdk环境
[root@ amoeba ~]# tar -zxvf jdk-8u60-linux-x64.tar.gz -C /usr/local/
[root@ amoeba ~]# vim /etc/profile
#添加如下三行到全局环境变量
export JAVA_HOME=/usr/local/jdk1.8.0_60
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
[root@ amoeba ~]# source /etc/profile
[root@ amoeba ~]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
下载安装Amoeba
[root@ amoeba ~]# yum -y install unzip
#解压并改名
[root@ amoeba ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /usr/local/
[root@ amoeba ~]# mv /usr/local/amoeba-mysql-3.0.5-RC /usr/local/amoeba
配置Amoeba
[root@ amoeba ~]# cd /usr/local/amoeba/conf/
[root@ amoeba conf]# cat 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指定缺省库,因为操作会调度到各个后端mysql
<!-- mysql user -->
<property name="user">amoeba</property> #设置amoeba连接后端数据库服务器的账号
<property name="password">123456</property> #设置amoeba连接后端数据库服务器的密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接
</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"> #设置一个后端可写的mysql,这里定义为master,这个名字可以任意命名,后面还会用到
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">10.0.0.21</property> #设置后端可写mysql地址
</factoryConfig>
</dbServer>
<dbServer name="slave" parent="abstractServer"> #设置后端可读mysql
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">10.0.0.22</property>
</factoryConfig>
</dbServer>
<dbServer name="myslave" virtual="true"> #设置定义一个虚拟的dbserver,实际上相当于一个dbserver组,这里将可读的数据库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
<