mycat实现mysql读写分离

mysql主从配置

主:3307
从:3308
环境准备
获取软件mysql5.7.2:
[root@VM_0_2_centos soft]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
若下载中断掉客户端,可使用wget -c 命令继续下载

创建mysql用户和目录

新增用户,组groupadd mysql
useradd -r -g mysql -s /bin/false mysql
–false禁止mysql用户登陆
新增目录
/data/app/mysql-3307 /data/app/mysql-3308
将解压后的文件放置在该目录下并赋权,

cp -rf ./mysql-5.7.21-linux-glibc2.12-x86_64/. /data/app/mysql-3307     
cp -rf ./mysql-5.7.21-linux-glibc2.12-x86_64/. /data/app/mysql-3308    
chown -R mysql:mysql /data/app/mysql-3307 chown -R mysql:mysql /data/app/mysql-3308     
    
/data/app/mysql-3307/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql-3307 --datadir=/data/app/mysql-3307/data    
/data/app/mysql-3308/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql-3308 --datadir=/data/app/mysql-3308/data    

配置mysql.cnf文件

/data/app/mysql-3307/my.cnf     
[client]    
port = 3307    
socket=/data/app/mysql-3307/mysql.sock     
[mysqld]    
port =3307    
user=mysql    
server-id=1    
bind-address=0.0.0.0    
basedir=/data/app/mysql-3307    
datadir=/data/app/mysql-3307/data    
socket=/data/app/mysql-3307/mysql.sock     
pid-file=/data/app/mysql-3307/mysql.pid    
log-error=/data/app/mysql-3307/mysqld.log     
skip-name-resolve    
log_bin=/data/app/mysql-3307/mysql-bin    
log-slave-updates    
auto-increment-increment=2    
auto-increment-offset=1    
lower_case_table_names=1     
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'    
binlog_format=mixed    

mysql-3308相同操作


/data/app/mysql-3308/my.cnf     
[client]    
port = 3308    
socket=/data/app/mysql-3308/mysql.sock     
[mysqld]    
port =3308    
user=mysql    
server-id=1    
bind-address=0.0.0.0    
basedir=/data/app/mysql-3308    
datadir=/data/app/mysql-3308/data    
socket=/data/app/mysql-3308/mysql.sock     
pid-file=/data/app/mysql-3308/mysql.pid    
log-error=/data/app/mysql-3308/mysqld.log     
skip-name-resolve    
log_bin=/data/app/mysql-3308/mysql-bin    
log-slave-updates    
#auto-increment-increment=2    
#auto-increment-offset=1    
lower_case_table_names=1     
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'    
binlog_format=mixed    
touch /data/app/mysql-3307/mysqld.log  && chown -R mysql:mysql /data/app/ 
touch /data/app/mysql-3308/mysqld.log  && chown -R mysql:mysql /data/app/    

替代/data/app/mysql-3307/bin/mysqld_safe文件中/usr/local/mysql/data/app/mysql-3307

sed -i 's#/usr/local/mysql#/data/app/mysql-3307#g'  /data/app/mysql-3307/bin/mysqld_safe    
sed -i 's#/usr/local/mysql#/data/app/mysql-3308#g'  /data/app/mysql-3308/bin/mysqld_safe    

mysql启动,修改密码

/data/app/mysql-3307/bin/mysqld_safe --defaults-file=/data/app/mysql-3307/my.cnf  --basedir=/data/app/mysql-3307 --datadir=/data/app/mysql-3307/data --user=mysql & 
/data/app/mysql-3307/bin/mysqld_safe --defaults-file=/data/app/mysql-3307/my.cnf --basedir=/data/app/mysql-3307 --datadir=/data/app/mysql-3307/data --user=mysql   

首次无法登陆,原因为/root/.mysql.secret密码不对 my.cnf增加 skip-grant-tables

登陆mysql

mysql -uroot -S mysql.sock -P 3307    
mysql> alter user root@localhost identified by '123456';    
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement    
mysql> update mysql.user set authentication_string=password('123456') where user='root' and host='localhost';    
Query OK, 1 row affected, 1 warning (0.13 sec)    
Rows matched: 1  Changed: 1  Warnings: 1    

重新启动

$ mysqladmin -S mysql.sock shutdown -uroot -p

/data/app/mysql-3307/bin/mysqld_safe --defaults-file=/data/app/mysql-3307/my.cnf --basedir=/data/app/mysql-3307 --datadir=/data/app/mysql-3307/data --user=mysql &  

数据库主备配置

master端

mysql> create user 'repl'@'%' identified by '123456';    
Query OK, 0 rows affected (0.06 sec)    
    
mysql> grant replication slave on *.* to 'repl'@'%';    
Query OK, 0 rows affected (0.01 sec)    
    
mysql> show master status;    
+------------------+----------+--------------+------------------+-------------------+    
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |    
+------------------+----------+--------------+------------------+-------------------+    
| mysql-bin.000004 |      605 |              |                  |                   |    
+------------------+----------+--------------+------------------+-------------------+    

slave端

mysql> change master to master_host='127.0.0.1',master_port=3307,master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=605;    
Query OK, 0 rows affected, 2 warnings (0.13 sec)    
mysql> flush privileges;    
Query OK, 0 rows affected (0.10 sec)    
mysql> start slave;    
Query OK, 0 rows affected (0.00 sec)    
mysql> show slave status\G    
*************************** 1. row ***************************    
               Slave_IO_State:     
                  Master_Host: 127.0.0.1    
                  Master_User: repl    
                  Master_Port: 3307    
                Connect_Retry: 60    
              Master_Log_File: mysql-bin.000004    
          Read_Master_Log_Pos: 605    
               Relay_Log_File: VM_0_2_centos-relay-bin.000001    
                Relay_Log_Pos: 4    
        Relay_Master_Log_File: mysql-bin.000004    
             Slave_IO_Running: No    
            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: 605    
              Relay_Log_Space: 154    
              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: NULL    
Master_SSL_Verify_Server_Cert: No    
                Last_IO_Errno: 1593    
                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).    
               Last_SQL_Errno: 0    
               Last_SQL_Error:     
  Replicate_Ignore_Server_Ids:     
             Master_Server_Id: 1    
                  Master_UUID:     
             Master_Info_File: /data/app/mysql-3308/data/master.info    
                    SQL_Delay: 0    
          SQL_Remaining_Delay: NULL    
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates    
           Master_Retry_Count: 86400    
                  Master_Bind:     
      Last_IO_Error_Timestamp: 180714 11:58:14    
     Last_SQL_Error_Timestamp:     
               Master_SSL_Crl:     
           Master_SSL_Crlpath:     
           Retrieved_Gtid_Set:     
            Executed_Gtid_Set:     
                Auto_Position: 0    
         Replicate_Rewrite_DB:     
                 Channel_Name:     
           Master_TLS_Version:     
1 row in set (0.00 sec) 

安装生产JDK

  1. 删除openjdk
rpm -qa|grep java    
yum -y remove java*
  1. 安装java
 rpm -ivh jdk-8u171-linux-x64.rpm     
java -version    

mycat 配置

解压并修改文件

解压缩到/data/app目录下
修改/data/app/mycat/conf/schema.xml

   
    
    
<mycat:schema xmlns:mycat="http://io.mycat/">    
    
        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">    
                    
                <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />    
    
                    
                <table name="temp" primaryKey="ID" type="global" dataNode="dn1" />    
                <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />    
                <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />    
                    
                <table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"    
                           rule="mod-long" />    
                    
                <table name="employee" primaryKey="ID" dataNode="dn1,dn2"    
                           rule="sharding-by-intfile" />    
                <table name="customer" primaryKey="ID" dataNode="dn1,dn2"    
                           rule="sharding-by-intfile">    
                        <childTable name="orders" primaryKey="ID" joinKey="customer_id"    
                                                parentKey="id">    
                                <childTable name="order_items" joinKey="order_id"    
                                                        parentKey="id" />    
                        childTable>    
                        <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"    
                                                parentKey="id" />    
                table>    
                    
        schema>    
            
        <dataNode name="dn1" dataHost="localhost1" database="test" />    
        <dataNode name="dn2" dataHost="localhost1" database="test" />    
        <dataNode name="dn3" dataHost="localhost1" database="test" />    
            
        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"    
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">    
                <heartbeat>select user()heartbeat>    
                    
                <writeHost host="hostM1" url="localhost:3307" user="dba"    
                                   password="123456">    
                            
                        <readHost host="hostS1" url="localhost:3308" user="dba" password="123456" />    
                writeHost>    
                   <writeHost host="hostM2" url="localhost:3309" user="dba"    
                                   password="123456">    
                            
                        <readHost host="hostS2" url="localhost:3310" user="dba" password="123456" />    
                writeHost>    
                    
        dataHost>    
            
            
server.xml文件注意部分,用于登陆mycat的用户名密码    
        <user name="root" defaultAccount="true">    
                <property name="password">123456property>    
                <property name="schemas">TESTDBproperty>    

schema.xml文件说明

  1. TESTDB是默认的库,在启动时会默认启动配置,生成指定的一个逻辑库,库名TESTDB,可修改server和schmas中TESTDB。
  2. 表名指定,主键指定,类型对应的数据节点指定几个,写入节点就为几个。该例子中一旦dataNode=“dn1,dn2,dn3”,写入的时候会向对应位置写入,即便对应位置相同。
  3. dataNode可以认为是写入节点。相同的一套数据源指定一个dataNode。

各参数含义详情参考mycat帮助文档。

dbDriver 属性
指定连接后端数据库使用的 Driver,目前可选的值有 native 和 JDBC。使用 native 的话,因为这个值执行的是二进制的 mysql 协议,所以可以使用 mysql 和 maridb。其他类型的数据库则需要使用 JDBC 驱动来支持。
从 1.6 版本开始支持 postgresql 的 native 原始协议。
如果使用 JDBC 的话需要将符合 JDBC 4 标准的驱动 JAR 包放到 MYCAT\lib 目录下,并检查驱动 JAR 包中包括如下目录结构的文件:META-INF\services\java.sql.Driver。在这个文件内写上具体的 Driver 类名,例如:com.mysql.jdbc.Driver。

balance 属性
负载均衡类型,目前的取值有 3 种:
balance=”0”, 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。
balance=”1”,全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且 M1 与 M2 互为主备),正常情况下,M2,S1,S2 都参与 select 语句的负载均衡。
balance=”2”,所有读操作都随机的在 writeHost、readhost 上分发。
balance=”3”,所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压
力,注意 balance=3 只在 1.4 及其以后版本有,1.3 没有。

writeType 属性
负载均衡类型,目前的取值有 3 种:
writeType=”0”, 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个
writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
writeType=”1”,所有写操作都随机的发送到配置的 writeHost,1.5 以后废弃不推荐。

switchType 属性
-1 表示不自动切换
1 默认值,自动切换
2 基于 MySQL 主从同步的状态决定是否切换
心跳语句为 show slave status

启动mycat

/data/app/mycat/bin/mycat start
启动日志:/data/app/mycat/logs/wrapper.log查看启动进度

登陆mycat

-h处指定
mysql -uroot -p -h127.0.0.1 -P8066

出现的问题

swap空间不足报错

2018-07-16T03:39:46.301178Z 0 [ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12    
2018-07-16T03:39:46.301200Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool```

你可能感兴趣的:(MySQL,Linux,Mycat)