数据库主从配置–实现数据库读写分离机制–配置数据库双主模式(实现数据库高可用)–mycat实现高可用–数据库高可用测试
1.数据库主从配置
1.1数据库挂载状态
1.1.1检查主库状态(在操作数据库的第三方软件上)
SHOW MASTER STATUS;
1.1.2 (在操作Linux虚拟机的第三方软件上)
要想服务器充当主库,必须开启数据库二进制文件
修改主库的核心配置文件my.cnf: vim /ect/my.cnf
添加server-id=1//服务的id号
添加log-bin=mysql-bin 定义二进制文件名称
检查文件:cd /var/lib/mysql ls 此时没有mysql-bin.000001
1.1.3重启服务器(在操作Linux虚拟机的第三方软件上)
systemctl restart mariadb
SHOW MASTER STATUS检查(在操作数据库的第三方软件上) 检查有没有mysql-bin.000001
1.1.4重复上述步骤布置从库从库(注意server-id不能一样)(在操作Linux虚拟机的第三方软件上)
1.2实现主从的挂载
1.2.1从库去挂载主库(在操作数据库的第三方软件–从库操作编写代码)
需要:主库ip地址/主库端口号/登录用户名和密码/二进制日志文件/读取位置
/* 内网*/
/* 检查状态,*/
SHOW MASTER STATUS;
CHANGE MASTER TO MASTER_HOST="192.168.126.129",
MASTER_PORT=3306,
MASTER_USER="root",
MASTER_PASSWORD="root",
MASTER_LOG_FILE="mysql-bin.000001",
MASTER_LOG_POS=245;
/* 启动主从服务器*/
START SLAVE
/* 检查主从的状态*/
SHOW SLAVE STATUS
1.3实现数据库读写分离机制(mycat数据库分库分表中间件)
1.3.1上传mycat(在操作Linux虚拟机的第三方软件上)
上传mycat文件(直接将Mycat-server拖拽到->操作Linux虚拟机的第三方软件路径上:cd /usr/local/src/)
1.3.2解压(在操作Linux虚拟机的第三方软件上)
解压 :tar -xvf Mycat-server-1.7.0-DEV-20170416134921-linux.tar.gz
将解压包移动到soft目录下:mv Mycat-server-1.7.0-DEV-20170416134921-linux.tar.gz soft
1.3.3编辑mycat中server.xml文件(在本地目录的mycat中编辑)
要想用户与主数据库的连接就得通过server.xml文件进行配置 ip/port/user/passward
server.xml中默认端口号配置:8066
<?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-->
<property name="useHandshakeV10">1</property>
<property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->
<property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 -->
<property name="sequnceHandlerType">2</property>
<!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-