mysql防止从节点可写数据_mysql主从复制及mycat读写分离
登录#.#.190.163的服务器,在服务器上配置:
vim /etc/my.cnf
[mysqld]
在此添加如下配置,其中163为服务器的ip最后一位
server_id=163
需要复制的数据库名称
binlog-do-db=xxxy
log-bin=xxxy-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=100
slave_skip_errors=1062
[root@iZ112fail84Z ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.6.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave,replication client on *.* to 'root'@'120.77.##.##' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
在主数据库上模拟准备需要复制的表;
mysql> create database testpre;
Query OK, 1 row affected (0.00 sec)
mysql> use testpre;
Database changed
mysql> create table pre (id int,name varchar(20));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into pre values (1,'a');
Query OK, 1 row affected (0.00 sec)
mysql> insert into pre values (2,'b');
Query OK, 1 row affected (0.00 sec)
mysql> select * from pre;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
+------+------+
2 rows in set (0.00 sec)
为了保证master和slave的数据一致,采用主库备份,从库还原来实现初始数据一致。
先暂时把要复制的表锁定
主数据库上
mysql> insert into pre values (3,'c');
Query OK, 1 row affected (0.01 sec)
mysql> select * from pre;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c |
+------+------+
3 rows in set (0.00 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into pre values (4,'d');
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
进行数据的导出操作
mysql>exit;
Bye
[root@iZ112fail84Z ~]# mysqldump -uroot -p --add-drop-table testpre >/bak/testpre.sql;
Enter password:
[root@iZ112fail84Z bak]# cd /bak/
[root@iZ112fail84Z bak]# ls
bakmysql bakmysqlold shell testpre.sql
[root@iZ112fail84Z bak]# pwd
拷贝主库的的testpre.sql文件到从数据库服务器
修改从slave的mysql配置文件/etc/my.cnf
vim /etc/my.cnf
在[mysqld]中增加
设置server_id,一般设置为ip的最后位
server_id=220
复制过滤:需要备份的数据库
binlog-do-db=xxxy
不需要备份的数据库,一般这2种中只设置一种
binlog-ignore-db=mysql
开启二进制日志,以备slave作为其它slave的master时使用
log-bin=xxxy-slave-bin
为每个session分配内存,
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=100
slave_skip_errors=1062
relay_log配置中继日志
relay_log=xxxy-relay-bin
log_slave_updates 表示slave将复制事件写进自己的二进制日志
log_slave_updates=1
防止从节点的数据改变
read_only=1
service mysql restart
导入主库上的那个脚本到从库
mysqldump -uroot -p testpre < /bak/testpre.sql
或者使用navicat进行数据传输,保证主库和从库中的testpre库中的数据一致
在从库中执行
[root@izwz95v28cpic4beuk0rk0z ~]# mysql -uroot -p
Enter password:
Welcometo the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 110
Server version: 5.6.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to master_host='#.#.190.163',master_user='root',master_password='123456',master_port=3306,master_log_file='xxxy-bin.000001',master_log_pos=30042,master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql>
其中master_log_file和master_log_pos的值分别为在主库上的如下:
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| xxxy-bin.000001 | 30042 | xxxy | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
在从服务器上执行:
mysql> show slave status \G;*************************** 1. row ***************************
Slave_IO_State:
Master_Host: #.#.190.163
Master_User: root
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: xxxy-bin.000001
Read_Master_Log_Pos: 30042
Relay_Log_File: xxxy-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: xxxy-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
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: 30042
Relay_Log_Space: 120
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: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
```来查询从的状态
开启主从复制:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
***************** 1. row *****************
Slave_IO_State: Waiting for master to send event
Master_Host: #.#.190.163
Master_User: root
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: xxxy-bin.000001
Read_Master_Log_Pos: 30042
Relay_Log_File: xxxy-relay-bin.000002
Relay_Log_Pos: 282
Relay_Master_Log_File: xxxy-bin.000001
Slave_IO_Running: Yes
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: 30042
Relay_Log_Space: 454
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 163
Master_UUID: ee963db0-323b-11e7-9fc2-00163e060905
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
如上:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这2个表示主从同步正常,
查询线程状态:
在master上查看slave的i/o线程创建的连接
mysql> show processlist \G;
***************** 1. row *****************
Id: 8
User: root
Host: #.#.190.163:38009
db: xxxy
Command: Sleep
Time: 1159
State:
Info: NULL
测试主从复制是否成功:
之主库中的任意一个表中插入数据后,去从库查看,发现能同步成功了。
测试过程中,如果同步出错,可以在slave上重置主从复制的设置
mysql>reset slave;
mysql>change master to master_host=’#.#.190.163’,master_user=’root’,master_password=’123456’,master_port=3306,master_log_file=’xxxy-bin.000001’,master_log_pos=30042,master_connect_retry=30;
-------------------------------
##mycat主从读写分离
www.mycat.org.cn
mycat的读写分离是基于后端mysql集群的主从同步来实现。mycat的提供语句的分发功能。
[root@iZ112fail84Z ~]# useradd mycat
[root@iZ112fail84Z ~]# passwd mycat
Changing password for user mycat.
New password:
passwd: all authentication tokens updated successfully.
[root@iZ112fail84Z ~]# su mycat
[mycat@iZ112fail84Z root]whoamimycat[mycat@iZ112fail84Zroot] clear
[mycat@iZ112fail84Z root]cd[mycat@iZ112fail84Z] pwd
/home/mycat
[mycat@iZ112fail84Z ~]$ su root
Password:
[root@iZ112fail84Z mycat]# vim /etc/profile
mycat env
export MYCAT_HOME=/usr/local/mycat
export PATH=PATH:MYCAT_HOME/bin
“/etc/profile” 86L, 2060C written
[root@iZ112fail84Z mycat]# source /etc/profile
[root@iZ112fail84Z mycat]# exit
exit
[mycat@iZ112fail84Z ~]whoamimycat[mycat@iZ112fail84Z] pwd
/home/mycat
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
–2017-10-16 22:18:42– http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
Resolving dl.mycat.io… 210.51.26.184
Connecting to dl.mycat.io|210.51.26.184|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 15662280 (15M) [application/octet-stream]
Saving to: “Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz”
100%[============================================================>] 15,662,280 504K/s in 30s
2017-10-16 22:19:13 (502 KB/s) - “Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz” saved [15662280/15662280]
tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
$ ls
mycat Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
su root
Password:
[root@iZ112fail84Z mycat]# ls
mycat Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@iZ112fail84Z mycat]# mv /home/mycat/mycat /usr/local/
[root@iZ112fail84Z mycat]# cd /usr/local/mycat/
[root@iZ112fail84Z mycat]# ls
bin catlet conf lib logs version.txt
[root@iZ112fail84Z mycat]# ll
total 24
drwxrwxr-x 2 mycat mycat 4096 Oct 16 22:21 bin
drwxrwxr-x 2 mycat mycat 4096 Mar 1 2016 catlet
drwxrwxr-x 4 mycat mycat 4096 Oct 16 22:21 conf
drwxrwxr-x 2 mycat mycat 4096 Oct 16 22:21 lib
drwxrwxr-x 2 mycat mycat 4096 Oct 28 2016 logs
-rwxrwxr-x 1 mycat mycat 217 Oct 28 2016 version.txt
mycat的配置说明:
如果主从复制需要复制自定义函数及存储过程,需要在/etc/my.cnf的[mysqld]中添加log_bin_trust_function_creates=true
默认linux版的mysql的大小写不敏感,需要在/etc/my.cnf的[mysqld]中添加lower_case_table_names=1
配置mycat的
[root@iZ112fail84Z conf]# pwd
/usr/local/mycat/conf
mycat的核心配置文件:
schema.xml 用于设置mycat的逻辑库,表,数据节点,datahost等内容
conf目录下主要以下三个需要熟悉。
server.xml是Mycat服务器参数调整和用户授权的配置文件
schema.xml是逻辑库定义和表以及分片定义的配置文件
rule.xml是分片规则的配置文件
vim server.xml
1
启动mycat失败,查看日志
[root@iZ112fail84Z bin]# ./mycat start
Starting Mycat-server...
[root@iZ112fail84Z bin]# tail -200 ../logs/wrapper.log
INFO | jvm 1 | 2017/11/01 19:27:35 | 2017-11-01 19:27:35,781 [ERROR][WrapperSimpleAppMain] 2017-11-01 19:27:35 startup error java.lang.NumberFormatException: Size must be specified as bytes (b), kibibytes (k), mebibytes (m), gibibytes (g), tebibytes (t), or pebibytes(p). E.g. 50b, 100k, or 250m.
INFO | jvm 1 | 2017/11/01 19:27:35 | Failed to parse byte string: -44040192B
vim server.xml添加如下:
0
详见:[错误处理](http://blog.csdn.net/gaojingsong/article/details/53244239)
在启动时还是启动不了,查看日志:
tail -200 ../logs/wrapper.log
提示内存不足
vim /usr/local/mycat/conf/wrapper.conf
wrapper.java.additional.5=-XX:MaxDirectMemorySize=2G
wrapper.java.additional.10=-Xmx4G
wrapper.java.additional.11=-Xms1G
改为:
wrapper.java.additional.5=-XX:MaxDirectMemorySize=100M
wrapper.java.additional.10=-Xmx100M
wrapper.java.additional.11=-Xms100M
其中的2个配置文件内容:
2017-11-02 22:21:16.356 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.releaseConnection(NonBlockingSession.java:341)) - release connection MySQLConnection [id=20, lastTime=1509632476341, user=root, schema=xxxy, old shema=xxxy, borrowed=true, fromSlaveDB=false, threadId=10487, charset=utf8, txIsolation=3, autocommit=true, attachment=rc_dn2{insert into cash (userid) values (345)}, respHandler=SingleNodeHandler [node=rc_dn2{insert into cash (userid) values (345)}, packetId=1], host=#.#.190.163, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
注意看:host=#.#.190.163 ,
在进行select,发现查询在另一个ip上
配置mycat状态检查服务
在mycat的服务主机上添加mycat服务的状态监测脚本,并开放相应的监测端口,以提供给HAProxy对mycat的服务状态进行检测判断。可以使用xinetd来实现,通过xinetd,HAProxy可以用httpchk来检测mycat的存活状态。
xinetd:网络守护进程服务程序
[root@izwz95v28cpic4beuk0rk0z etc]# yum install xinetd
[root@izwz95v28cpic4beuk0rk0z etc]# cat /etc/xinetd.conf
mysql防止从节点可写数据_mysql主从复制及mycat读写分离相关推荐
- mysql防止从节点可写数据_mysql 主从数据不一致 Slave_SQL_Running: No 解决方法
在slave服务器上通过如下命令 MysqL> show slave status\G; 显示如下情况: Slave_IO_Running: Yes Slave_sql_Running: No ...
- mysql双主可以同时写数据_Mysql双主操作
在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果是双主或者多主,就会增加mysql入 ...
- mysql主从配置对解决并发有用_MySQL主从配置,读写分离
大型网站为了缓解大量的并发访问,要网站实现分布式负载均衡.但是数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢失的话,后果不堪设想.这时 ...
- MySQL从服务器写入报错吗_MySQL主从复制读写分离及奇怪的问题
联系邮箱:hi@jackcool.net 一直都没有写blog的习惯,以前总觉得自己的脑子就是最好的记忆容器,现在觉得我好像有个假脑子. 当时是使用阿里云镜像,安装了两台ECS,结果配置MySQL的时 ...
- 运维之道 | Mysql主从复制+mycat读写分离
运维之道 | Mysql主从复制 + Mycat读写分离 1.什么是读写分离 读写分离,基本的原理是让主数据库处理事务性增.删.改操作(INSERT.UPDATE.DROP),而从数据库处理SELEC ...
- MySQL数据库mycat读写分离
mycat读写分离 Mycat的读写分离是建立在Mysq的主从复制的基础上的 修改配置文件 schema.xml <?xml version="1.0"?> <! ...
- mysql高级技术之mycat读写分离技术实践
1,课程回顾 2,本章重点 mysql 主从原理,好处mycat 概念,读写分离好处,读写分离的实现 3,具体内容 3.1 mysql 主从 3.1.1 linux下mysql安装 以mysql5.6 ...
- MySQL MyCAT 读写分离实战
1.MySQL读写分离概念: MYSQL读写分离的原理其实就是让Master数据库处理事务性增.删除.修改.更新操作(CREATE. INSERT.UPDATE.DELETE),而让Slave数据库处 ...
- MYSQL+MYCAT读写分离实战
****1.实战MYSQL+MYCAT读写分离实战,实现MYSQL数据库1主2从架构.**2.写出MYSQL 1主2从架构部署过程和MYCAT实战全部过程,将所有部署过程写出来和划出架构图. 1.配置 ...
最新文章
- linux服务器崩,Linux系统崩溃最快速的解决方法
- 一文读懂P Quant与 Q Quant ,量化交易与金融工程
- opencv mat数据剪裁感兴趣的部分处理方法
- 最大数输出(信息学奥赛一本通-T1053)
- linux中部分命令及快捷键
- 【Unity3D基础教程】给初学者看的Unity教程(一):GameObject,Compoent,Time,Input,Physics...
- 【学习】013 Servlet、Cookie、Session的简述
- JAVA虚拟机创建对象
- SpringBoot使用Ehcache
- matlab pt站下载,PT站RSS下载使用教程 | PT邀请码网
- Jim exclaimed, Damn man, this isn’t funny cheap air max 90
- DOS命令:diskpart
- Confluence 摘要(Excerpt)宏
- python全栈开发包括那些_什么是全栈工程师?有哪些知识?
- 十七、DPM模型参数设置详解
- 网桥,交换机,hub,路由器到底是什么,怎么区别
- 小米华为智能手机未来路不平坦
- 解决每次新建word都有页眉和页脚
- 1785 数据流中的算法(模拟,乱搞)
- 循环比赛日程表(分治 C++)