前に書く
環境:centos6、jdk8、mycat1.6、mysql5.6。
1:jdkをインストールします
少し
2:mysqlをインストールします
2.1:ダウンロード
ではここで、インストールパッケージをダウンロードします。
2.2:インストール
- mysqlユーザーとグループを作成する
groupadd mysql
useradd -g mysql mysql
- に解凍
/usr/local
tar -zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz -C /usr/local
- 解凍したファイルに名前を付けます
mysql
。 - / etcの下にmy.cnfを作成します
[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
- 関連するディレクトリを作成する
mkdir /var/lib/mysql
mkdir /var/lib/mysql/mysql
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/mysql
- 初期化データ
cd /usr/local/mysql
chown -R mysql:mysql ./
./scripts/mysql_install_db --user=mysql --force
chown -R mysql:mysql data
- 構成(起動を含む)
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
- 関連コマンド
service mysqld [start|stop|restart]
3:mycatをインストールします
3.1:ダウンロード
では、このダウンロード。
3.2:解凍
ファイルを/opt
ディレクトリに解凍します。unzipコマンドはtar -zxvf
です。
3.3:開始してみてください
/opt/mycat/bin/mycat start
起動が成功したかどうかを確認します。
[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:正式に開始
4.1:環境の紹介
两台mysql实例:
192.168.2.113:3306 root/12345678
192.168.2.114:3306 root/12345678
一台mycat:
192.168.2.113:8066
4.2:mycatユーザーを構成する
/opt/mycat/conf/server.xml
以下のコンテンツを編集して追加します。
<user name="root" defaultAccount="true">
<property name="password">12345678</property>
<property name="schemas">test_db</property>
</user>
最後に、このアカウント情報を使用してmycatに接続します。
4.3:ルーティングルールを構成する
次のコンテンツを追加します。
<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
<property name="count">2</property>
合計2つのmysqlインスタンスを表すルーティングルールクラスを構成します。
4.4:スキーマ情報を構成する
<?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>
ノード構成データベース情報、テーブル情報、ルーティングルールおよびその他の情報。<dataNode>
データベースノード情報を<dataHost
構成し、特定のデータベース情報、およびヘルスチェック情報を構成します。
4.5:データを準備する
test_db
次のように、2つのmysqlインスタンスにデータベースを作成し、t_userテーブルを作成します。
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:mycatを起動します
/opt/mycat/bin/mycat start
4.7: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:クエリデータ
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:IDに従ってシャーディングをテストする
- 192.168.2.113で次のSQLを実行します
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');
- 192.168.2.114で次のSQLを実行します
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');
- 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
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)
私たちは見ることができますid=3
からデータを192.168.2.113
クエリへid=4
の192.168.2.114
クエリに。
4.10:挿入データの断片化をテストする
- mycatを介して次のSQLを実行します
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)
- では
192.168.2.113
、クエリ
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)
ID 5のデータは書き込まれていますが、ID6のデータは書き込まれていないことがわかります。
- では
192.168.2.114
、クエリ
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)
ID 5のデータが書き込まれておらず、ID6のデータが書き込まれていることがわかります。