Escribir al frente
Entorno: centos6, jdk8, mycat1.6, mysql5.6.
1: instalar jdk
levemente
2: instalar mysql
2.1: Descargar
En aquí para descargar el paquete de instalación.
2.2: Instalación
- Crear usuarios y grupos de mysql
groupadd mysql
useradd -g mysql mysql
- Descomprimir para
/usr/local
tar -zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz -C /usr/local
- Nombra el archivo descomprimido
mysql
. - Cree my.cnf en / etc
[mysql]
#客户端默认字符集
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端口
port=3306
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
- Crea directorios relacionados
mkdir /var/lib/mysql
mkdir /var/lib/mysql/mysql
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/mysql
- Datos de inicialización
cd /usr/local/mysql
chown -R mysql:mysql ./
./scripts/mysql_install_db --user=mysql --force
chown -R mysql:mysql data
- Configuración (incluido el arranque)
chown 777 /etc/my.cnf
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld # 拷贝开机启动脚本
chmod +x /etc/rc.d/init.d/mysqld # 赋予启动脚本执行权限
chkconfig --add mysqld # 加入到服务中
chkconfig --list mysqld # 检查是否生效,类似如下即为生效:mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
- Comandos relacionados
service mysqld [start|stop|restart]
3: instalar mycat
3.1: Descargar
En esta descarga.
3.2: Descomprimir
Descomprima el archivo en el /opt
directorio, el comando de descompresión es tar -zxvf
.
3.3: Intenta empezar
/opt/mycat/bin/mycat start
Verifique si la puesta en marcha es exitosa:
[root@localhost mycat]# tail -10 /opt/mycat/logs/wrapper.log
STATUS | wrapper | 2020/10/31 20:44:34 | --> Wrapper Started as Daemon
STATUS | wrapper | 2020/10/31 20:44:35 | Launching a JVM...
INFO | jvm 1 | 2020/10/31 20:44:35 | Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO | jvm 1 | 2020/10/31 20:44:36 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2020/10/31 20:44:36 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2020/10/31 20:44:36 |
INFO | jvm 1 | 2020/10/31 20:44:39 | log4j:WARN No appenders could be found for logger (io.mycat.memory.MyCatMemory).
INFO | jvm 1 | 2020/10/31 20:44:39 | log4j:WARN Please initialize the log4j system properly.
INFO | jvm 1 | 2020/10/31 20:44:39 | log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
INFO | jvm 1 | 2020/10/31 20:44:40 | MyCAT Server startup successfully. see logs in logs/mycat.log
4: iniciado oficialmente
4.1: Introducción al medio ambiente
两台mysql实例:
192.168.2.113:3306 root/12345678
192.168.2.114:3306 root/12345678
一台mycat:
192.168.2.113:8066
4.2: Configurar el usuario de mycat
Edite /opt/mycat/conf/server.xml
y agregue el siguiente contenido.
<user name="root" defaultAccount="true">
<property name="password">12345678</property>
<property name="schemas">test_db</property>
</user>
Finalmente, usamos esta información de cuenta para conectarnos a mycat.
4.3: Configurar reglas de enrutamiento
Agrega el siguiente contenido:
<tableRule name="mod-long-111">
<rule>
<columns>id</columns>
<algorithm>mod-long-dongshidaddy</algorithm>
</rule>
</tableRule>
<function name="mod-long-dongshidaddy" class="io.mycat.route.function.PartitionByMod">
<property name="count">2</property>
</function>
io.mycat.route.function.PartitionByMod
Configure la clase de regla de enrutamiento, que <property name="count">2</property>
representa un total de 2 instancias de mysql.
4.4: Configurar la información del esquema
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="test_db" checkSQLschema="false" sqlMaxLimit="100">
<table name="t_user" primaryKey="id" autoIncrement="true" dataNode="dn1,dn2"
ruleRequired="true" rule="mod-long-111"></table>
</schema>
<dataNode name="dn1" dataHost="localhost1" database="test_db" />
<dataNode name="dn2" dataHost="localhost2" database="test_db" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.2.114:3306" user="root" password="12345678"></writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostS1" url="192.168.2.113:3306" user="root" password="12345678" />
</dataHost>
</mycat:schema>
<schema>
Información de la base de datos de configuración del nodo, información de la tabla, reglas de enrutamiento y otra información. <dataNode>
Configure la información del nodo de la base de datos, <dataHost
configure la información específica de la base de datos y la información de verificación de estado.
4.5: preparar datos
Cree una test_db
base de datos en las dos instancias de mysql y cree la tabla t_user, de la siguiente manera:
mysql> create table t_user (id int not null primary key,name varchar(100));
Query OK, 0 rows affected (0.24 sec)
mysql> insert into t_user(id,name) values(1,'hello mycat');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t_user(id,name) values(2,'javacoder.cn');
Query OK, 1 row affected (0.00 sec)
4.6: Iniciar mycat
/opt/mycat/bin/mycat start
4.7: Conectarse a mycat
[root@localhost logs]# mysql -uroot -p -h192.168.2.113 -P8066
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.29-mycat-1.6.6.1-release-20181031195535 MyCat Server (OpenCloudDB)
Copyright (c) 2000, 2020, 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>
4.8: Consultar datos
mysql> show databases;
+----------+
| DATABASE |
+----------+
| test_db |
+----------+
1 row in set (0.03 sec)
mysql> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t_user where id=1;
+----+-------------+
| id | name |
+----+-------------+
| 1 | hello mycat |
+----+-------------+
1 row in set (0.31 sec)
4.9: Prueba de fragmentación según id
- Ejecute el siguiente SQL en 192.168.2.113
insert into t_user(id,name) values(3,'id=3,192.168.2.113');
insert into t_user(id,name) values(4,'id=4,192.168.2.113');
- Ejecute el siguiente SQL en 192.168.2.114
insert into t_user(id,name) values(3,'id=3,192.168.2.114');
insert into t_user(id,name) values(4,'id=4,192.168.2.114');
- Conéctese a mycat y consulte
xbdeMacBook-Air:finalshell root# mysql -uroot -p -h192.168.2.113 -P8066
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.6.29-mycat-1.6.6.1-release-20181031195535 MyCat Server (OpenCloudDB)
Copyright (c) 2000, 2019, 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> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t_user where id=3;
+----+--------------------+
| id | name |
+----+--------------------+
| 3 | id=3,192.168.2.113 |
+----+--------------------+
1 row in set (0.01 sec)
mysql> select * from t_user where id=4;
+----+--------------------+
| id | name |
+----+--------------------+
| 4 | id=4,192.168.2.114 |
+----+--------------------+
1 row in set (0.01 sec)
Podemos ver id=3
los datos de 192.168.2.113
la consulta a, id=4
de 192.168.2.114
consultas a.
4.10: Fragmentación de datos de inserción de prueba
- Ejecute el siguiente SQL a través de mycat
xbdeMacBook-Air:finalshell root# mysql -uroot -p -h192.168.2.113 -P8066
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
...
mysql> insert into t_user(id,name) values(5,'id=5,insert by mycat middleware');
Query OK, 1 row affected (0.10 sec)
mysql> insert into t_user(id,name) values(6,'id=6,insert by mycat middleware');
Query OK, 1 row affected (0.31 sec)
- En
192.168.2.113
consulta
xbdeMacBook-Air:finalshell xb$ mysql -uroot -p -h192.168.2.113 -P3306
Enter password:
...
mysql> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t_user where id=5;
+----+---------------------------------+
| id | name |
+----+---------------------------------+
| 5 | id=5,insert by mycat middleware |
+----+---------------------------------+
1 row in set (0.00 sec)
mysql> select * from t_user where id=6;
Empty set (0.04 sec)
Puede ver que los datos con id 5 se han escrito, pero los datos con id 6 no se han escrito.
- En
192.168.2.114
consulta
xbdeMacBook-Air:finalshell xb$ mysql -uroot -p -h192.168.2.114 -P3306
Enter password:
...
mysql> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t_user where id=5;
Empty set (0.01 sec)
mysql> select * from t_user where id=6;
+----+---------------------------------+
| id | name |
+----+---------------------------------+
| 6 | id=6,insert by mycat middleware |
+----+---------------------------------+
1 row in set (0.01 sec)
Puede ver que los datos con id 5 no se han escrito y los datos con id 6 se han escrito.