Home Database Mysql Tutorial 为MySQL安装配置代理工具Kingshard的基本教程_MySQL

为MySQL安装配置代理工具Kingshard的基本教程_MySQL

May 27, 2016 pm 01:46 PM
mysql acting Tutorial

环境说明

本文仅作为最小实验环境,因此不使用master, slave模式. 单机上使用mysql_mutil运行二个mysql实列
初始化数据目录

# mysql_install_db --datadir=/var/lib/mysql2/ --user=mysql
# mysql_install_db --datadir=/var/lib/mysql3/ --user=mysql
Copy after login


生成配置文件

利用mysqld_multi工具生成配置文件

# mysqld_multi --example > mysqld_multi.conf
Copy after login


修改根据自己的需求修改mysqld_multi.conf
例:

[mysqld_multi]
mysqld   = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user    = multi_admin
password  = my_password
 
[mysqld2]
socket   = /var/lib/mysql2/mysql.sock2
port    = 3307
pid-file  = /var/lib/mysql2/hostname.pid2
datadir  = /var/lib/mysql2
#language  = /usr/share/mysql/english
user    = unix_user1
 
[mysqld3]
socket   = /var/lib/mysql3/mysql.sock3
port    = 3308
pid-file  = /var/lib/mysql3/hostname.pid3
datadir  = /var/lib/mysql3
#language  = /usr/share/mysql/swedish
user    = unix_user2
Copy after login


启动多个实例

# mysqld_multi --defaults-extra-file=./mysqld_multi.conf start
Copy after login

或者 mysqld_multi --defaults-extra-file=./mysqld_multi.conf start 2; mysqld_multi --defaults-extra-file=./mysqld_multi.conf start 3(分别启动)

注意这里的2、3对应conf配置文件 mysqld2、mysqld3,以此来区分。
查看实例状态

[root@testnode kingshard]# mysqld_multi --defaults-extra-file=./mysqld_multi.conf report
Copy after login

Reporting MySQL servers
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld3 is running
Copy after login

说明2个实例都已经启动了。

安装Kingshard

1.安装Go语言环境,具体步骤请Google。

git clone https://github.com/flike/kingshard.git src/github.com/flike/kingshard
cd src/github.com/flike/kingshard
source ./dev.sh
make
Copy after login

设置配置文件
运行kingshard。

./bin/kingshard -config=etc/multi.yaml
Copy after login


2.配置文件说明

# kingshard的地址和端口
addr : 127.0.0.1:9696
 
# 连接kingshard的用户名和密码
user : kingshard
password : kingshard
 
# log级别,[debug|info|warn|error],默认是error
log_level : debug
# 只允许下面的IP列表连接kingshard
allow_ips: 127.0.0.1
 
# 一个node节点表示mysql集群的一个数据分片,包括一主多从(可以不配置从库)
nodes :
  #node节点名字
  name : node1 
 
  # 连接池中默认的空闲连接数
  idle_conns : 16
 
  # kingshard连接该node中mysql的用户名和密码,master和slave的用户名和密码必须一致
  user : kingshard 
  password : kingshard
 
  # master的地址和端口 
  master : 127.0.0.1:3306
 
  # slave的地址和端口,可不配置
  slave : 
  #kingshard在300秒内都连接不上mysql,则会下线该mysql
  down_after_noalive : 300
- 
  name : node2 
  idle_conns : 16
  rw_split: true
  user : kingshard 
  password : kingshard
 
  master : 192.168.59.103:3307
  slave : 
  down_after_noalive: 100
 
# 分表规则
schemas :
-
  db : kingshard
  nodes: [node1,node2]
  rules:
    default: node1
    shard:
    -  
      table: test_shard_hash
      key: id
      nodes: [node1, node2]
      type: hash
      locations: [4,4]
 
    -  
      table: test_shard_range
      key: id
      type: range
      nodes: [node1, node2]
      locations: [4,4]
      table_row_limit: 10000
Copy after login


3.Tips
kingshard采用的是yaml方式解析配置文件,需要注意的是yaml配置文件不允许出现tab键,且冒号后面需要跟一个空格。配置文件编写完成后,可以在yaml lint网站验证是否有格式错误。

配置Kingshard

修改/etc/hosts文件, 添加如下二行

127.0.0.1 node1
127.0.0.1 node2
Copy after login


配置如下

# server listen addr
addr : 127.0.0.1:9696
 
# server user and password
user : kingshard
password : kingshard
 
# log level[debug|info|warn|error],default error
log_level : debug
# only allow this ip list ip to connect kingshard
#allow_ips: 127.0.0.1
 
# node is an agenda for real remote mysql server.
nodes :
- 
  name : node1 
 
  # default max idle conns for mysql server
  idle_conns : 16
 
  # if rw_split is true, select will use slave server
  rw_split: true
 
  # all mysql in a node must have the same user and password
  user : root
  password : root
 
  # master represents a real mysql master server 
  master : 127.0.0.1:3307
 
  # slave represents a real mysql salve server,and the number after '@' is 
  #read load weight of this slave.
  #slave : 192.168.0.11:3307@2,192.168.0.12:3307@5
  slave : 
  #down_after_noalive : 300
- 
  name : node2 
 
  # default max idle conns for mysql server
  idle_conns : 16
 
  # if rw_split is true, select will use slave server
  rw_split: true
 
  # all mysql in a node must have the same user and password
  user : root
  password : root
 
  # master represents a real mysql master server 
  master : 127.0.0.1:3308
 
  # slave represents a real mysql salve server 
  slave : 
 
  # down mysql after N seconds noalive
  # 0 will no down
  down_after_noalive: 100
 
# schema defines which db can be used by client and this db's sql will be executed in which nodes
schemas :
-
  db : kingshard
  nodes: [node1,node2]
  rules:
    default: node1
    shard:
    -  
      table: test_shard_hash
      key: id
      nodes: [node1, node2]
      type: hash
      locations: [4,4]
 
    -  
      table: test_shard_range
      key: id
      type: range
      nodes: [node1, node2]
      locations: [4,4]
      table_row_limit: 10000
Copy after login


设置mysql实例信息

设置用户
分类登陆mysqld2, mysqld3, 创建root用户(该用户是给kingshard管理的,测试为了方便所以直接使用root) 若用户存在,跳过此步

/usr/bin/mysqladmin -h 127.0.0.1 -P 3307 -u root password 'root'
/usr/bin/mysqladmin -h 127.0.0.1 -P 3308 -u root password 'root'
Copy after login


建数据库
分类登陆mysqld2, mysqld2,创建kingshard数据库

/usr/bin/mysql -h 127.0.0.1 -P 3307 -u root -proot -e "create database kingshard;"
/usr/bin/mysql -h 127.0.0.1 -P 3308 -u root -proot -e "create database kingshard;"
Copy after login


启动Kingshard

# ./bin/kingshard -config=etc/multi.yaml
Copy after login


测试shard功能

使用test_shard_hash测试 shard hash分表功能.
创建分表
创建test_shard_hash分表(_0000~_0007), _0001~_0003在node1(mysqld2)上创建, _0004~_0007在node2(mysqld3)上创建。

for i in `seq 0 3`;do /usr/bin/mysql -h 127.0.0.1 -P 3307 -u root -proot kingshard -e "CREATE TABLE IF NOT EXISTS test_shard_hash_000"${i}" ( id BIGINT(64) UNSIGNED NOT NULL, str VARCHAR(256), f DOUBLE, e enum('test1', 'test2'), u tinyint unsigned, i tinyint, ni tinyint, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";done
for i in `seq 4 7`;do /usr/bin/mysql -h 127.0.0.1 -P 3308 -u root -proot kingshard -e "CREATE TABLE IF NOT EXISTS test_shard_hash_000"${i}" ( id BIGINT(64) UNSIGNED NOT NULL, str VARCHAR(256), f DOUBLE, e enum('test1', 'test2'), u tinyint unsigned, i tinyint, ni tinyint, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";done
Copy after login


插入数据
mysql连接到kingshard插入数据

for i in `seq 1 10`;do mysql -h 127.0.0.1 -P 9696 -u kingshard -pkingshard -e "insert into test_shard_hash (id, str, f, e, u, i) values(${i}, 'abc$i', 3.14, 'test$i', 255, -127)";done
Copy after login


kingshard日志如下:

2015/07/29 07:39:15 - INFO - 127.0.0.1:40135->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40135->127.0.0.1:3307:insert into test_shard_hash_0001(id, str, f, e, u, i) values (1, 'abc1', 3.14, 'test1', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40136->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40136->127.0.0.1:3307:insert into test_shard_hash_0002(id, str, f, e, u, i) values (2, 'abc2', 3.14, 'test2', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40137->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40137->127.0.0.1:3307:insert into test_shard_hash_0003(id, str, f, e, u, i) values (3, 'abc3', 3.14, 'test3', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40138->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40138->127.0.0.1:3308:insert into test_shard_hash_0004(id, str, f, e, u, i) values (4, 'abc4', 3.14, 'test4', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40139->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40139->127.0.0.1:3308:insert into test_shard_hash_0005(id, str, f, e, u, i) values (5, 'abc5', 3.14, 'test5', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40140->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40140->127.0.0.1:3308:insert into test_shard_hash_0006(id, str, f, e, u, i) values (6, 'abc6', 3.14, 'test6', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40141->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40141->127.0.0.1:3308:insert into test_shard_hash_0007(id, str, f, e, u, i) values (7, 'abc7', 3.14, 'test7', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40142->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40142->127.0.0.1:3307:insert into test_shard_hash_0000(id, str, f, e, u, i) values (8, 'abc8', 3.14, 'test8', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40143->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40143->127.0.0.1:3307:insert into test_shard_hash_0001(id, str, f, e, u, i) values (9, 'abc9', 3.14, 'test9', 255, -127)
2015/07/29 07:39:15 - INFO - 127.0.0.1:40144->127.0.0.1:3307:select @@version_comment limit 1
2015/07/29 07:39:15 - INFO - 127.0.0.1:40144->127.0.0.1:3307:insert into test_shard_hash_0002(id, str, f, e, u, i) values (10, 'abc10', 3.14, 'test10', 255, -127)
Copy after login


通过kingshard的日志可以看到数据插入时根据不同的hash值,插入到不同的子表里面去了。


查看数据

[root@testnode kingshard]# mysql -h 127.0.0.1 -P 9696 -u kingshard -pkingshard -e "select * from test_shard_hash where id in (2, 3, 4, 5)"
Copy after login

+----+------+------+-------+------+------+------+
| id | str | f  | e   | u  | i  | ni  |
+----+------+------+-------+------+------+------+
| 2 | abc2 | 3.14 | test2 | 255 | -127 | NULL |
| 3 | abc3 | 3.14 |    | 255 | -127 | NULL |
| 4 | abc4 | 3.14 |    | 255 | -127 | NULL |
| 5 | abc5 | 3.14 |    | 255 | -127 | NULL |
+----+------+------+-------+------+------+------+
Copy after login

 
注意kingshard不支持 select * from test_hard_hash查询, 只支持带条件的查询。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer Jul 16, 2024 pm 09:02 PM

The expansion of the virtual market is inseparable from the circulation of virtual currency, and naturally it is also inseparable from the issue of virtual currency transfers. A common transfer error is the address copy error, and another error is the chain selection error. The transfer of virtual currency to the wrong chain is still a thorny problem, but due to the inexperience of transfer operations, novices often transfer the wrong chain. So how to recover the wrong chain of virtual currency? The wrong link can be retrieved through a third-party platform, but it may not be successful. Next, the editor will tell you in detail to help you better take care of your virtual assets. How to retrieve the wrong chain of virtual currency? The process of retrieving virtual currency transferred to the wrong chain may be complicated and challenging, but by confirming the transfer details, contacting the exchange or wallet provider, importing the private key to a compatible wallet, and using the cross-chain bridge tool

See all articles