The installation of mycat is simple to use hello world

Write in front

Environment: centos6, jdk8, mycat1.6, mysql5.6.

1: Install jdk

slightly

2: Install mysql

2.1: Download

In here to download the installation package.

2.2: Installation

  • Create mysql users and groups
groupadd mysql
useradd -g mysql mysql
  • Unzip to/usr/local
    tar -zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz -C /usr/local
  • Name the decompressed file mysql.
  • Create my.cnf under /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
  • Create related directories
mkdir /var/lib/mysql
mkdir /var/lib/mysql/mysql
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/mysql
  • Initialization data
cd /usr/local/mysql
chown -R mysql:mysql ./
./scripts/mysql_install_db --user=mysql --force
chown -R mysql:mysql data 
  • Configuration (including booting)
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
  • Related commands
service mysqld [start|stop|restart]

3: Install mycat

3.1: Download

In this download.

3.2: Unzip

Unzip the file to the /optdirectory, the unzip command is tar -zxvf.

3.3: Try to start

/opt/mycat/bin/mycat start

Check whether the startup is successful:

[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: Officially started

4.1: Introduction to the environment

两台mysql实例:
	192.168.2.113:3306 root/12345678
	192.168.2.114:3306 root/12345678
一台mycat:
    192.168.2.113:8066

4.2: Configure mycat user

Edit /opt/mycat/conf/server.xml, add the following content.

<user name="root" defaultAccount="true">
          <property name="password">12345678</property>
          <property name="schemas">test_db</property>
 </user>

Finally, we use this account information to connect to mycat.

4.3: Configure routing rules

Add the following content:

<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.PartitionByModConfigure the routing rule class, which <property name="count">2</property>represents a total of 2 mysql instances.

4.4: Configure schema information

<?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>Node configuration database information, table information, routing rules and other information. <dataNode>Configure database node information, <dataHostconfigure specific database information, and health check information.

4.5: Prepare data

Create a test_dbdatabase in the two mysql instances , and create the t_user table, as follows:

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: Start mycat

 /opt/mycat/bin/mycat start

4.7: Connect to 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: Query data

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: Test sharding according to id

  • Execute the following SQL at 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 the following SQL at 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');
  • Connect to mycat and query
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)

We can see id=3the data from 192.168.2.113the query to, id=4from 192.168.2.114queries to.

4.10: Test insert data fragmentation

  • Execute the following SQL through 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)
  • In 192.168.2.113query
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)

You can see that the data with id 5 has been written, but the data with id 6 has not been written.

  • In 192.168.2.114query
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)

You can see that the data with id 5 has not been written, and the data with id 6 has been written.

Finally: get out of the way, I want to drink Luckin

Insert picture description here

Guess you like

Origin blog.csdn.net/wang0907/article/details/109411578