分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
分表:在台服务器上,优点是易维护,类似表分区,缺点是在一台db服务器上,无法分担IO、负载集中。
分库:在多台服务器上,优点是分担IO、负载均衡,缺点是较不易维护、数据统计以及jion操作有些难度。
数据库切分的目的是为了分担IO、负载均衡,分表无法达到最佳的要求,所以无法成为主流。
tar -xvf mysql-5.6.12.tar.gzcd mysql-5.6.12time cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql56m1 -DMYSQL_DATADIR=/home/data/mysql56m1/data -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR==/usr/local/mysql56m1/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_citime maketime make install chown -R mysql /home/data/ chgrp -R mysql /home/data/ chown -R mysql /usr/local/mysql chown -R mysql /usr/local/mysql56m1 chgrp -R mysql /usr/local/mysql chgrp -R mysql /usr/local/mysql56m1/ mkdir -p /home/data/mysql56m1/binlog/ chown -R mysql.mysql /home/data/mysql56m1/binlog/ mkdir -p /home/data/mysql5610/binlog/ chown -R mysql.mysql /home/data/cd /usr/local/mysql56m1time scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql56m1 --datadir=/home/data/mysql56m1/data --defaults-file=/usr/local/mysql56m1/my.cnfcp support-files/mysql.server /etc/init.d/mysql56m1chmod 700 /etc/init.d/mysql56m1echo "export PATH=$PATH:/usr/local/mysql56m1/bin">>/etc/profile source /etc/profilechkconfig --add mysql56m1service mysql56m1 start
tar -xvf mysql-5.6.12.tar.gzcd mysql-5.6.12time cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql56s1 -DMYSQL_DATADIR=/home/data/mysql56s1/data -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR==/usr/local/mysql56s1/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_citime maketime make install chown -R mysql /home/data/ chgrp -R mysql /home/data/ chown -R mysql /usr/local/mysql chown -R mysql /usr/local/mysql56s1 chgrp -R mysql /usr/local/mysql chgrp -R mysql /usr/local/mysql56s1/ mkdir -p /home/data/mysql56s1/binlog/ chown -R mysql.mysql /home/data/mysql56s1/binlog/ mkdir -p /home/data/mysql5610/binlog/ chown -R mysql.mysql /home/data/cd /usr/local/mysql56s1time scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql56s1 --datadir=/home/data/mysql56s1/data --defaults-file=/usr/local/mysql56s1/my.cnfcp support-files/mysql.server /etc/init.d/mysql56s1chmod 700 /etc/init.d/mysql56s1echo "export PATH=$PATH:/usr/local/mysql56s1/bin">>/etc/profile source /etc/profilechkconfig --add mysql56s1service mysql56s1 start
在一台服务器,主3317端口,从3327端口。
主库备库上,添加复制帐号:
GRANT REPLICATION SLAVE ON . TO ‘repl’@’10.254.%’ IDENTIFIED BY ‘mycatms’;
在备库上3327端口上设置复制:
stop slave;reset slave;change master to master_user='repl', master_password='mycatms', master_host='127.0.0.1',master_port=3317, master_log_file='mysql-bin.000003',master_log_pos=840;start slave;show slave status\G;
主库:3317端口
从库:3327端口
数据同步测试:
配置schema.xml:
然后修改mycat的schema.xml:
balance为1:让全部的readHost及备用的writeHost参与select的负载均衡。
switchType为2:基于MySQL主从同步的状态决定是否切换。
heartbeat:主从切换的心跳语句必须为show slave status。
mysql> explain create table company(id int not null primary key,name varchar(100));+-----------+---------------------------------------------------------------------+| DATA_NODE | SQL |+-----------+---------------------------------------------------------------------+| dn1 | create table company(id int not null primary key,name varchar(100)) || dn2 | create table company(id int not null primary key,name varchar(100)) || dn3 | create table company(id int not null primary key,name varchar(100)) |+-----------+---------------------------------------------------------------------+3 rows in set (0.00 sec)mysql> create table company(id int not null primary key,name varchar(100));Query OK, 0 rows affected (0.01 sec)mysql> explain insert into company(id,name) values(1,'alibaba');+-----------+--------------------------------------------------+| DATA_NODE | SQL |+-----------+--------------------------------------------------+| dn1 | insert into company(id,name) values(1,'alibaba') || dn2 | insert into company(id,name) values(1,'alibaba') || dn3 | insert into company(id,name) values(1,'alibaba') |+-----------+--------------------------------------------------+3 rows in set (0.10 sec)mysql> insert into company(id,name) values(1,'alibaba');Query OK, 1 row affected (0.00 sec)mysql>
[root@wgq_idc_squid_1_11 logs]# /usr/local/mysql56m1/bin/mysql -uroot -p -P3317 --socket=/usr/local/mysql56m1/mysql.sock -e "select @@port;select * from db1.company";Enter password: +--------+| @@port |+--------+| 3317 |+--------++----+---------+| id | name |+----+---------+| 1 | alibaba |+----+---------+[root@wgq_idc_squid_1_11 logs]#
[root@wgq_idc_squid_1_11 logs]# /usr/local/mysql56s1/bin/mysql -uroot -p -P3327 --socket=/usr/local/mysql56s1/mysql.sock -e "select @@port;select * from db1.company";Enter password: +--------+| @@port |+--------+| 3327 |+--------++----+---------+| id | name |+----+---------+| 1 | alibaba |+----+---------+[root@wgq_idc_squid_1_11 logs]#
balance为1:让全部的readHost及备用的writeHost参与select的负载均衡。
switchType为2:基于MySQL主从同步的状态决定是否切换。
heartbeat:主从切换的心跳语句必须为show slave status。
有配置读节点:
[root@wgq_idc_squid_1_11 conf]# vim log4j.xmlvalue="info" /> 变成 value="debug" />
在mycat窗口上执行select操作:
mysql> explain select * from company where id=1;+-----------+----------------------------------------------+| DATA_NODE | SQL |+-----------+----------------------------------------------+| dn3 | SELECT * FROM company WHERE id = 1 LIMIT 100 |+-----------+----------------------------------------------+1 row in set (0.14 sec)mysql> select * from company where id=1;+----+---------+| id | name |+----+---------+| 1 | alibaba |+----+---------+1 row in set (0.01 sec)mysql>
后台日志显示出只在3327上执行:
01/18 01:35:01.536 DEBUG [$_NIOREACTOR-3-RW] (PhysicalDBPool.java:452) -select read source hostS2 for dataHost:wgq_idc_mon_1_1101/18 01:35:01.537 DEBUG [$_NIOREACTOR-3-RW] (MySQLConnection.java:445) -con need syn ,total syn cmd 2 commands SET names latin1;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:false con:MySQLConnection [id=14, lastTime=1453052101537, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=0, autocommit=true, attachment=dn1{SELECT *FROM companyWHERE id = 1LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *FROM companyWHERE id = 1LIMIT 100}, packetId=0], host=127.0.0.1, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]01/18 01:35:01.546 DEBUG [$_NIOREACTOR-2-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=14, lastTime=1453052101529, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=3, autocommit=true, attachment=dn1{SELECT *FROM companyWHERE id = 1LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *FROM companyWHERE id = 1LIMIT 100}, packetId=5], host=127.0.0.1, port=3327, statusSync=org.opencloudb.mysql.nio.MySQLConnection$StatusSync@7fb43f0f, writeQueue=0, modifiedSQLExecuted=false]01/18 01:35:01.547 DEBUG [$_NIOREACTOR-2-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=14, lastTime=1453052101529, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]^C
Mycat窗口写操作:
后台log显示写操作在3317上:
01/18 01:39:54.550 DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=7, lastTime=1453052394535, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=163, charset=latin1, txIsolation=3, autocommit=true, attachment=dn3{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]01/18 01:39:54.550 DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=7, lastTime=1453052394535, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=163, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]01/18 01:39:54.550 DEBUG [$_NIOREACTOR-3-RW] (MultiNodeQueryHandler.java:171) -received ok response ,executeResponse:true from MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=dn2{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]01/18 01:39:54.551 DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=dn2{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]01/18 01:39:54.551 DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
Check下主从数据是否都已经写入了:
主库3317端口:
从裤3327端口:
<dataHost name="wgq_idc_mon_1_11" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100"> <heartbeat>show slave statusheartbeat> <writeHost host="hostM1" url="127.0.0.1:3317" user="root" password=""> <readHost host="hostR1" url="127.0.0.1:3327" user="root" password=""/> writeHost> <writeHost host="hostS2" url="127.0.0.1:3327" user="root" password="" /> dataHost>
关闭主库
[root@wgq_idc_squid_1_11 conf]# service mysql56m1 stopShutting down MySQL.... [确定][root@wgq_idc_squid_1_11 conf]#
Mycat后台报错:
01/18 01:50:01.037 INFO [Timer0] (PhysicalDatasource.java:373) -not ilde connection in pool,create new connection for hostM1 of schema db101/18 01:50:01.038 INFO [$_NIOConnector] (AbstractConnection.java:458) -close connection,reason:java.net.ConnectException: 拒绝连接 ,MySQLConnection [id=0, lastTime=1453053001035, user=root, schema=db1, old shema=db1, borrowed=false, fromSlaveDB=false, threadId=0, charset=utf8, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]01/18 01:50:01.038 INFO [$_NIOConnector] (SQLJob.java:111) -can't get connection for sql :show slave status01/18 01:50:01.038 WARN [$_NIOREACTOR-1-RW] (MySQLDetector.java:139) -found MySQL master/slave Replication err !!! DBHostConfig [hostName=hostR1, url=127.0.0.1:3327]error reconnecting to master 'repl@127.0.0.1:3317' - retry-time: 60 retries: 10
看到主从失效,因为主down了。
因为我们通过mycat配置了主从切换模式,现在3317端口主库down了,那么写库应该自动切换到从裤3327上面,如果在mycat上面写入,就应该写到3327端口的从库了,验证如下:
(1)在mycat窗口录入数据:
(2)在3327端口从裤验证数据:
看到3327端口的数据有新录入的4,meituan的数据。表明验证成功。
(3) dnindex属性文件中writeHost已经变成了第二个了,如下所示:
这个时候就会报错,主从连接失败,原因是从库3327会再重新从主库3317同步所有的数据,但是从库已经有了,所以就会报错,如下所示:
我采用
stop slave; set global sql_slave_skip_counter=1; start slave; show slave status\G;
一个个忽略过后,就正常了,没有报错:
但是这个时候如果再在mycat窗口上录入数据,debug分析面,还是会写入到3327里面去:
后台debug日志:
OK,至此,mycat主从切换成功。