mysql优化之读写分离

常用命令:
service mysqld start
service iptables stop

mysql –u root
在这里插入图片描述

主从复制演示

服务器准备

192.168.110.177  主服务器 master
192.168.110.178  从服务器slave

修改主(master)服务器

vi /etc/my.cnf  新增以下内容
server_id=177  ###服务器id
log-bin=mysql-bin   ###开启日志文件

重启服务器

service mysqld start
service iptables stop

主服务器给从服务器账号授权

GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';

登录主服务器的mysql,查询master的状态

show master status;

如果结果为null,则主服务器my.cf没有配置好.

修改从(slave)服务器

server_id=178
log-bin=mysql-bin
binlog_do_db=test
change master to master_host='192.168.110.177',master_user='mysync',master_password='q123456',
         master_log_file='mysql-bin.000002',master_log_pos=343;

启动同步

start slave

检查从服务器复制功能状态

SHOW SLAVE STATUS

Slave_IO_Running: Yes    //此状态必须YES
Slave_SQL_Running: Yes     //此状态必须YES

读写分离

在这里插入图片描述

什么是读写分离

在这里插入图片描述
在数据库集群架构中,让主库负责处理事务性查询,而从库只负责处理select查询,让两者分工明确达到提高数据库整体读写性能。当然,主数据库另外一个功能就是负责将事务性查询导致的数据变更同步到从库中,也就是写操作。

读写分离的好处

1)分摊服务器压力,提高机器的系统处理效率
2)增加冗余,提高服务可用性,当一台数据库服务器宕机后可以调整另外一台从库以最快速度恢复服务

主从复制原理

依赖于二进制日志,binary-log.
二进制日志中记录引起数据库发生改变的语句
Insert 、delete、update、create table

Scale-up与Scale-out区别

Scale Out是指Application可以在水平方向上扩展。一般对数据中心的应用而言,Scale out指的是当添加更多的机器时,应用仍然可以很好的利用这些机器的资源来提升自己的效率从而达到很好的扩展性。
Scale Up是指Application可以在垂直方向上扩展。一般对单台机器而言,Scale Up值得是当某个计算节点(机器)添加更多的CPU Cores,存储设备,使用更大的内存时,应用可以很充分的利用这些资源来提升自己的效率从而达到很好的扩展性。

MyCat

什么是 Mycat–默认端口号8066

是一个开源的分布式数据库系统,但是因为数据库一般都有自己的数据库引擎,而Mycat并没有属于自己的独有数据库引擎,所有严格意义上说并不能算是一个完整的数据库系统,只能说是一个在应用和数据库之间起桥梁作用的中间件Mycat安装

Mycat使用

Mycat安装

创建表结构

配置server.xml

	<!-- 添加user -->
   <user name="mycat">
    <property name="password">mycat</property>
    <property name="schemas">mycat</property>
    </user>
	
	<!-- 添加user -->
   <user name="mycat_red">
    <property name="password">mycat_red</property>
    <property name="schemas">mycat</property>
	<property name="readOnly">true</property>
    </user>

配置schema.xml

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
    <!-- 与server.xml中user的schemas名一致 -->
    <schema name="mycat" checkSQLschema="true" sqlMaxLimit="100">
        <table name="t_users" primaryKey="user_id" dataNode="dn1" rule="rule1"/>
        <table name="t_message" type="global" primaryKey="messages_id" dataNode="dn1" />
    </schema>
<dataNode name="dn1" dataHost="jdbchost" database="weibo_simple


" />
   
    <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="1"
                writeType="0" dbType="mysql" dbDriver="native" switchType="1"
                slaveThreshold="100">
         <heartbeat>select user()</heartbeat>  
        <writeHost host="hostMaster" url="172.27.185.1:3306" user="root" password="root">
        </writeHost>
        <writeHost host="hostSlave" url="172.27.185.2:3306" user="root" password="root"/>
    </dataHost>
    
</mycat:schema>

配置rule.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:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://org.opencloudb/">
	 <tableRule name="rule1">
        <rule>
            <columns>user_id</columns>
            <algorithm>func1</algorithm>
        </rule>
    </tableRule>
    <function name="func1" class="org.opencloudb.route.function.AutoPartitionByLong">
  	<property name="mapFile">autopartition-long.txt</property>
    </function>
</mycat:rule>

为了更好地定位错误,修改log4j.xml

<level value="debug" />

双击startup_nowrap.bat开始启动

常见问题

SHOW MASTER STATUS 如果为,则在my.ini文件中添加一行
log-bin=mysql-bin

给账号分配权限
grant all privileges on . to ‘root’@‘172.27.185.1’ identified by ‘root’;
查询服务器server_id
SHOW VARIABLES LIKE ‘server_id’

猜你喜欢

转载自blog.csdn.net/weixin_42545256/article/details/82941279