Escreva na frente
Ambiente: centos6, jdk8, mycat1.6, mysql5.6.
1: Instale o jdk
um pouco
2: Instale o mysql
2.1: Download
Em aqui para baixar o pacote de instalação.
2.2: Instalação
- Crie usuários e grupos mysql
groupadd mysql
useradd -g mysql mysql
- Descompacte para
/usr/local
tar -zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz -C /usr/local
- Nomeie o arquivo descompactado
mysql
. - Crie my.cnf em / 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
- Crie diretórios 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
- Dados de inicialização
cd /usr/local/mysql
chown -R mysql:mysql ./
./scripts/mysql_install_db --user=mysql --force
chown -R mysql:mysql data
- Configuração (incluindo inicialização)
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: Instale o mycat
3.1: Download
Em este download.
3.2: Descompacte
Descompacte o arquivo para o /opt
diretório, o comando unzip é tar -zxvf
.
3.3: Tente começar
/opt/mycat/bin/mycat start
Verifique se a inicialização foi bem-sucedida:
[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: Oficialmente iniciado
4.1: Introdução ao meio 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 usuário mycat
Edite /opt/mycat/conf/server.xml
e adicione o seguinte conteúdo.
<user name="root" defaultAccount="true">
<property name="password">12345678</property>
<property name="schemas">test_db</property>
</user>
Por fim, usamos as informações da conta para nos conectar ao mycat.
4.3: Configurar regras de roteamento
Adicione o seguinte conteúdo:
<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 a classe de regra de roteamento, que <property name="count">2</property>
representa um total de 2 instâncias do mysql.
4.4: Configurar informações de 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>
Informações do banco de dados de configuração do nó, informações da tabela, regras de roteamento e outras informações. <dataNode>
Configure as informações do nó do banco de dados, <dataHost
configure as informações específicas do banco de dados e as informações de verificação de funcionamento.
4.5: Prepare os dados
Crie um test_db
banco de dados nas duas instâncias do mysql e crie a tabela t_user, como segue:
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 o mycat
/opt/mycat/bin/mycat start
4.7: Conecte-se ao 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: Dados de consulta
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: Teste de fragmentação de acordo com id
- Execute o seguinte SQL em 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');
- Execute o seguinte SQL em 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');
- Conecte-se ao mycat e 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
os dados da 192.168.2.113
consulta para, id=4
das 192.168.2.114
consultas para.
4.10: Fragmentação de dados de inserção de teste
- Execute o seguinte SQL por meio do 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)
- Em
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)
Você pode ver que os dados com id 5 foram gravados, mas os dados com id 6 não foram gravados.
- Em
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)
Você pode ver que os dados com id 5 não foram gravados e os dados com id 6 foram gravados.