基于docker的mysql主从,及其Mycat实现读写分离笔记

mysql安装

主库master

创建主库挂载文件地址

主库地址:192.168.10.28

mkdir -pv /home/mysql/master/conf
mkdir -pv /home/mysql/master/data
[mysqld]
server_id = 1
log-bin= mysql-bin
expire_logs_days=10
#数据库名称忽略大小写
lower_case_table_names=1
log-bin= mysql-bin
read-only=0
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
 ###引用默认配置
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

启动

docker run --name mastermysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /home/mysql/master/data:/var/lib/mysql -v /home/mysql/master/conf/my.cnf:/etc/mysql/my.cnf mysql:5.7
#进入容器
docker exec -it mastermysql bash
#进入mysql
mysql -u root -p
#创建slaver的账号
GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456';

查看信息

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>


从库slave

从库地址:192.168.10.32

mkdir -pv /home/mysql/slave/conf

mkdir -pv /home/mysql/slave/data

配置文件my.cnf

[mysqld]
server_id = 2
lower_case_table_names=1
log-bin= mysql-bin
read-only=0
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
explicit_defaults_for_timestamp=true
bind-address = 0.0.0.0
###引用默认配置
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/                                  

运行

docker run --name slavemysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /home/mysql/slave/data:/var/lib/mysql -v /home/mysql/slave/conf/my.cnf:/etc/mysql/my.cnf mysql:5.7

docker exec -it slavemysql  bash
# 172.17.0.3
mysql -u root -p
reset slave;
change master to master_host='192.168.10.28',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154,master_port=3306;

start slave;

测试通过

总结问题:

1,远程连接从库出现连接问题:

原因未关闭SELINUX

vi /etc/selinux/config
SELINUX=disabled 

2,从库多次启动造成启动不了

ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository

解决为:

在从库配置主从之前清楚以前的记录:

reset slave;

mycat实现读写分离

mycat的配置,在目录下面创建文件夹

mkdir -p /usr/local/mycat
cd /usr/local/mycat
rule.xml

DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
	<tableRule name="userrule">
		<rule>
			<columns>idcolumns>
			<algorithm>func1algorithm>
		rule>
	tableRule>
	<tableRule name="categoryrule">
		<rule>
			<columns>idcolumns>
			<algorithm>jump-consistent-hashalgorithm>
		rule>
	tableRule>

	<function name="murmur"
		class="io.mycat.route.function.PartitionByMurmurHash">
		<property name="seed">0property>
		<property name="count">2property>
		<property name="virtualBucketTimes">160property>
		
		
	function>

	<function name="crc32slot"
			  class="io.mycat.route.function.PartitionByCRC32PreSlot">
		<property name="count">2property>
	function>
	<function name="hash-int"
		class="io.mycat.route.function.PartitionByFileMap">
		<property name="mapFile">partition-hash-int.txtproperty>
	function>
	<function name="rang-long"
		class="io.mycat.route.function.AutoPartitionByLong">
		<property name="mapFile">autopartition-long.txtproperty>
	function>
	<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
		
		<property name="count">4property>
	function>

	<function name="func1" class="io.mycat.route.function.PartitionByLong">
		<property name="partitionCount">8property>
		<property name="partitionLength">128property>
	function>
	<function name="latestMonth"
		class="io.mycat.route.function.LatestMonthPartion">
		<property name="splitOneDay">24property>
	function>
	<function name="partbymonth"
		class="io.mycat.route.function.PartitionByMonth">
		<property name="dateFormat">yyyy-MM-ddproperty>
		<property name="sBeginDate">2015-01-01property>
	function>
	
	<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
        	<property name="mapFile">partition-range-mod.txtproperty>
	function>
	
	<function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
		<property name="totalBuckets">4property>
	function>
mycat:rule>


server.xml

DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
	<system>
	<property name="nonePasswordLogin">0property> 
	<property name="useHandshakeV10">1property>
	<property name="useSqlStat">0property>  
	<property name="useGlobleTableCheck">0property>  

		<property name="sequnceHandlerType">2property>
	<property name="subqueryRelationshipCheck">falseproperty> 
		<property name="handleDistributedTransactions">0property>

		<property name="useOffHeapForMerge">1property>

        <property name="memoryPageSize">64kproperty>

		<property name="spillsFileBufferSize">1kproperty>

		<property name="useStreamOutput">0property>
		<property name="systemReserveMemorySize">384mproperty>

		<property name="useZKSwitch">falseproperty>

		<property name="strictTxIsolation">falseproperty>
		
		<property name="useZKSwitch">trueproperty>
		
	system>

	<user name="root" >
		<property name="password">123456property>
		<property name="schemas">bmpproperty>
	user>
	<user name="user">
		<property name="password">123456property>
		<property name="schemas">bmpproperty>
		<property name="readOnly">trueproperty>
	user>
mycat:server>
schema.xml

DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    
    <schema name="bmp" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
    schema>
    
    <dataNode name="dn1" dataHost="bmphost" database="bmp"/>
    
    <dataHost name="bmphost" maxCon="1000" minCon="10" balance="2"
              writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100">
        
        <heartbeat>select user()heartbeat>
        
        <writeHost host="hostM1" url="192.168.10.28:3306" user="root" password="123456">
            
            <readHost host="hostS2" url="192.168.10.32:3306" user="root" password="123456"/>
        writeHost>
    dataHost>
mycat:schema>

sequence_conf.properties
TB_USER.HISIDS=
TB_USER.MINID=1
TB_USER.MAXID=20000
TB_USER.CURID=1
docker-compose.yml文件
version: '2'
services:
  mycat:
    image: longhronshens/mycat-docker
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
    volumes:
    - /usr/local/mycat/schema.xml:/usr/local/mycat/conf/schema.xml
    - /usr/local/mycat/rule.xml:/usr/local/mycat/conf/rule.xml
    - /usr/local/mycat/server.xml:/usr/local/mycat/conf/server.xml
    - /usr/local/mycat/sequence_conf.properties:/usr/local/mycat/conf/sequence_conf.properties
    ports:
    - 8066:8066/tcp
    - 9066:9066/tcp

参考:
https://www.cnblogs.com/songwenjie/p/9371422.html
https://blog.csdn.net/Amor_Leo/article/details/85218405

你可能感兴趣的:(java,docker,mycat,主从,读写分离,mysql)