一、拓扑图
本次实验搭建的为一台服务器上安装四个mysql实例,实现两主两从的结构;然后两个主mysql承接mycat写的操作,两个从mysql承接mycat读的操作,实现读写分离。同时,使用mycat-web连接mycat,实现对mycat性能的监控。
二、安装mysql
首先查看java版本
mycat需要jdk1.7以上版本。
我用的mysql安装包为源码包,因为用免编译包可能会出现文件路径或权限各种没有的情况,做实验推荐用mysql5.5以前的版本,比较轻量,编译速度比较快,我用的5.1.73版本,先创建需要的路径:
使用命令:
cd /data wget https://downloads.mysql.com/archives/get/file/mysql-5.1.73.tar.gz
进行下载
mkdir -p /data/3316/etc /data/3316/var /data/3316/log mkdir -p /data/3317/etc /data/3317/var /data/3317/log mkdir -p /data/3318/etc /data/3318/var /data/3318/log mkdir -p /data/3319/etc /data/3319/var /data/3319/log mkdir -p /tmp/mysql
创建mysql用户
groupadd mysql useradd -r -g mysql mysql
chown -R mysql:mysql /data/3316
chown -R mysql:mysql /data/3317
chown -R mysql:mysql /data/3318
chown -R mysql:mysql /data/3319
chown -R mysql:mysql /tmp/mysql
编译配置:
cd /data #进入安装包路径
tar -xvf mysql-5.1.73.tar.gz
cd mysql-5.1.73 ./configure --prefix=/data/3316 --sysconfdir=/data/3316/etc --with-tcp-port=3316 --localstatedir=/data/3316/var --with-unix-socket-path=/tmp/mysql/3316.sock --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312,binary --enable-thread-safe-client --with-plugins=innobase --with-mysqld-user=mysql --with-charset=utf8 --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static
make && make install
然后初始化数据库,此步骤需在mysql_install_db文件的上层目录执行,不然会报错:
(scripts有的版本在解压包目录,有的在自己选的mysql安装目录)
scripts/mysql_install_db --user=mysql --basedir=/data/3316 --datadir=/data/3316/var --defaults-file=/data/3316/etc/my.cnf scripts/mysql_install_db --user=mysql --basedir=/data/3316 --datadir=/data/3317/var --defaults-file=/data/3317/etc/my.cnf scripts/mysql_install_db --user=mysql --basedir=/data/3316 --datadir=/data/3318/var --defaults-file=/data/3318/etc/my.cnf scripts/mysql_install_db --user=mysql --basedir=/data/3316 --datadir=/data/3319/var --defaults-file=/data/3319/etc/my.cnf
然后创建各个实例的配置文件:
touch /data/3316/etc/my.cnf touch /data/3317/etc/my.cnf touch /data/3318/etc/my.cnf touch /data/3319/etc/my.cnf
打开文件
vi /data/3316/etc/my.cnf
把下列配置项复制进去
[client] port = 3316 socket = /tmp/mysql/3316.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3316 socket = /tmp/mysql/3316.sock basedir = /data/3316 datadir = /data/3316/var open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_open_cache = 614 external-locking = FALSE max_allowed_packet =8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k #default_table_type = InnoDB thread_stack = 192K #transaction_isolation = READ-COMMITTED tmp_table_size = 2M max_heap_table_size = 2M #long_query_time = 1 #log_long_format log-error = /data/3316/log/error.log log-slow-queries = /data/3316/log/slow.log pid-file = /tmp/mysql/3316.pid log-bin = /data/3316/var/mysql-bin #binlog-do-db = db1 #relay-log = /data/3316/log/relay-bin #relay-log-info-file = /data/3316/log/relay-log.info binlog_cache_size = 1M max_binlog_cache_size = 1M max_binlog_size = 2M expire_logs_days = 7 key_buffer_size = 16M read_buffer_size = 1M read_rnd_buffer_size = 1M bulk_insert_buffer_size = 1M lower_case_table_names = 1 skip-name-resolve binlog_format = mixed slave-skip-errors = 1032,1062 replicate-ignore-db=mysql server-id = 3316 innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 32M innodb_data_file_path = ibdata1:128M:autoextend innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 4M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 [mysqldump] quick max_allowed_packet = 2M [mysqld_safe] log-error=/data/3316/log/oldboy_3308.err pid-file=/tmp/mysql/3316.pid
同样,把/data/3317/etc/my.cnf /data/3318/etc/my.cnf /data/3319/etc/my.cnf 也都复制相同的内容,不过要把所有的3316换成对应的端口。
然后启动mysql:
cd /data/3316/bin ./mysqld_safe --defaults-file=/data/3316/etc/my.cnf --user=mysql --basedir=/data/3316 --datadir=/data/3316/var/ --log-error=/data/3316/mysql.err &
注意:这一步defaults-file配置项必须位于第一个。
同样在/data/3316/bin目录下执行
./mysqld_safe --defaults-file=/data/3317/etc/my.cnf --user=mysql --basedir=/data/3316 --datadir=/data/3317/var/ --log-error=/data/3317/mysql.err & ./mysqld_safe --defaults-file=/data/3318/etc/my.cnf --user=mysql --basedir=/data/3316 --datadir=/data/3318/var/ --log-error=/data/3318/mysql.err & ./mysqld_safe --defaults-file=/data/3319/etc/my.cnf --user=mysql --basedir=/data/3316 --datadir=/data/3319/var/ --log-error=/data/3319/mysql.err &
这时候输入命令:
ls /tmp/mysql
应该会显示:
到这一步mysql就已经完全安装成功了,接下来需要给各个mysql设置密码,开启主从复制,接下来以3316,3317为例;3318,3319同理:
cd /data/3316/bin ./mysqladmin -S /tmp/mysql/3316.sock -uroot password#接下来输入两次密码,我输入的是123 ./mysqladmin -S /tmp/mysql/3317.sock -uroot password#同上 ./mysql -uroot -S 3316.sock -p #输入密码 grant all privileges on *.* to root@'%' identified by "password"; #开启mysql远程访问权限,password换成你的密码 flush privileges;
create database db1; #创建实验数据库
GRANT replication slave ON *.* TO 'slave'@'%' IDENTIFIED BY '123';#在主库创建一个从库用来访问的用户
show master status;
show master status命令结果如图:
此时3316不要再动,打开另一个窗口操作:
cd /data/3316/bin ./mysql -uroot -S 3317.sock -p create database db1; stop slave; reset slaCHANGE MASTER TO MASTER_HOST='10.223.138.154',MASTER_PORT=3316,MASTER_USER='slave',MASTER_PASSWORD='123',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=246; start slave;
shhow slave status\G;
如果显示的两项为YES,就代表主从复制已经配置好了,可以实验下在主库新建表,对数据增删改查,从库是否会做同样的操作
再用同样的操作把3318、3319配置好
三、安装zookeeper
mkdir /opt/zookeeper cd /opt/zookeeper wget http://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.13.tar.gz #推荐从国内镜像站下载,官网太慢了 tar -xvf zookeeper-3.4.13.tar.gz cd zookeeper-3.4.13 cp /opt/zookeeper/zookeeper-3.4.13/conf/zoo_sample.cfg /opt/zookeeper/zookeeper-3.4.13/conf/zoo.cfg cd bin ./zkServer.sh start ./zkServer.sh status #查看zookeeper启动结果
四、安装mycat-web
mkdir /opt/mycat-web #创建mycat-web目录 cd /opt/mycat-web wget https://raw.githubusercontent.com/MyCATApache/Mycat-download/master/mycat-web-1.0/Mycat-web-1.0-SNAPSHOT-20160617163048-linux.tar.gz tar xvf Mycat-web-1.0-SNAPSHOT-20160617163048-linux.tar.gz cd mycat-web vi mycat-web/WEB-INF/classes/mycat.properties zookeeper=10.223.138.154:2181 #修改值,保存退出 ./start.sh
五、安装mycat
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz #下载mycat tar -xvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz cd mycat cd conf vi schema.xml
schema.xml文件是mycat的精华,mycat的读写分离,分库分表功能都通过这个文件实现,先贴上我的配置(此部分参考了别人的配置):
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <!-- TESTDB 是mycat的逻辑库名称,链接需要用的 --> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <!-- auto sharding by id (long) --> <table name="travelrecord" dataNode="dn1,dn2" rule="auto-sharding-long" /> <!-- global table is auto cloned to all defined data nodes ,so can join with any table whose sharding node is in the same data node --> <table name="company" primaryKey="ID" type="global" dataNode="dn1" /> <table name="goods" primaryKey="ID" type="global" dataNode="dn2" /> <!-- random sharding using mod sharind rule --> <table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1" 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"> </schema> <!-- database 是MySQL数据库的库名 --> <dataNode name="dn1" dataHost="localhost1" database="db1" /> <dataNode name="dn2" dataHost="localhost1" database="db3" /> <!-- dataNode节点中各属性说明: name:指定逻辑数据节点名称; dataHost:指定逻辑数据节点物理主机节点名称; database:指定物理主机节点上。如果一个节点上有多个库,可使用表达式db$0-99, 表示指定0-99这100个数据库; dataHost 节点中各属性说明: name:物理主机节点名称; maxCon:指定物理主机服务最大支持1000个连接; minCon:指定物理主机服务最小保持10个连接; writeType:指定写入类型; 0,只在writeHost节点写入; 1,在所有节点都写入。慎重开启,多节点写入顺序为默认写入根据配置顺序,第一个挂掉切换另一个; dbType:指定数据库类型; dbDriver:指定数据库驱动; balance:指定物理主机服务的负载模式。 0,不开启读写分离机制; 1,全部的readHost与stand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且M1与 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡; 2,所有的readHost与writeHost都参与select语句的负载均衡,也就是说,当系统的写操作压力不大的情况下,所有主机都可以承担负载均衡; --> <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="10.223.138.154:3316" user="root" password="123"> <!-- 可以配置多个从库 --> <readHost host="hostS1" url="10.223.138.154:3317" user="root" password="123" /> </writeHost> </dataHost> <dataHost name="localhost2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- 可以配置多个主从 --> <writeHost host="hostM2" url="10.223.138.154:3318" user="root" password="123"> <!-- 可以配置多个从库 --> <readHost host="hostS2" url="10.223.138.154:3319" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema>
这个配置好了之后配置server.xml
vi server.xml #只要注意如下部分即可,其他的不要动 <user name="root"> <property name="password">mysql</property> <property name="schemas">TESTDB</property> <!-- 表级 DML 权限设置 --> <!-- <privileges check="false"> <schema name="TESTDB" dml="0110" > <table name="tb01" dml="0000"></table> <table name="tb02" dml="1111"></table> </schema> </privileges> --> </user>
这个是比较容易配置的,只要改下password就可以,另外要保证 <property name="schemas">TESTDB</property>这个标签里的要和schema.xml的schema标签的name一致
cd ../bin
./mycat start #开启mycat
六、连接mycatweb和mycat
访问地址:10.223.138.154:8082/mycat/
页面如图所示,然后配置mycat:
然后就可以自己试试mycat-web的丰富功能了。