You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS7环境下如何用Docker镜像结合Vitess实现MySQL数据库分片?

后续操作步骤与Docker环境下Vitess MySQL分片指南

Alright, let's pick up right where you left off and walk through how to get Vitess sharding working with your Docker setup on CentOS 7. You've already done the heavy lifting building Vitess and setting up those environment variables, so let's dive into the next steps.

1. Spin up the Topology Service (etcd)

Vitess relies on a topology store to keep track of cluster metadata (like shard locations, tablet states, etc.). The default is etcd, so let's start that first inside your vitess/root container:

# Start etcd in the background
etcd --data-dir=$VTDATAROOT/etcd --listen-client-urls=http://0.0.0.0:2379 --advertise-client-urls=http://localhost:2379 &

Next, initialize the base Vitess topology structure and create your first keyspace (think of a keyspace as a logical database that will be split into shards):

# Create the keyspace (we'll use "test_keyspace" as an example)
vtctl CreateKeyspace test_keyspace

2. Launch MariaDB Backends (for Vitess Tablets)

You already have the MariaDB image pulled, so let's create a Docker network first to make communication between Vitess and MariaDB containers seamless (no need to mess with IP addresses):

# Run this on your CentOS 7 host, not inside the Vitess container
docker network create vitess-network

Now launch your first MariaDB container (for shard 0) attached to this network:

docker run -d --name vitess-mariadb-0 --network vitess-network -e MYSQL_ROOT_PASSWORD=your_secure_password mariadb:latest

Don't forget to replace your_secure_password with a real password you want to use.

If your existing vitess/root container isn't on this network yet, restart it to join:

# Stop the existing container if it's running
docker stop vitess-root
# Restart it with the network and environment variables
docker run -it --name vitess-root --network vitess-network -e VTROOT=/vt -e VTDATAROOT=/vt/vtdataroot vitess/root /bin/bash

3. Start VTTablets (Vitess-MySQL Middleware)

VTTablets are the workhorses that sit between Vitess and your MySQL instances—they handle shard routing, read/write splitting, and more. Launch your first tablet for shard 0 inside the vitess/root container:

vttablet \
  --topo_implementation etcd2 \
  --topo_global_server_address localhost:2379 \
  --topo_global_root /vitess/global \
  --tablet-path test_keyspace-0000000100 \
  --tablet-hostname vitess-mariadb-0 \
  --init_keyspace test_keyspace \
  --init_shard 0 \
  --init_tablet_type replica \
  --mysql_host vitess-mariadb-0 \
  --mysql_port 3306 \
  --mysql_user root \
  --mysql_password your_secure_password \
  --port 15000 \
  --grpc_port 16000 \
  --service_map 'grpc-vttablet' \
  &

Notice we're using the MariaDB container name vitess-mariadb-0 as the mysql_host—the Docker network handles DNS resolution for us here.

4. Define Sharding Rules

Now let's set up how we want to split our data. Let's say we want to shard based on user_id using a hash split into 2 shards. Run this in the vitess/root container:

vtctl CreateShardingRule -keyspace test_keyspace \
  --rule '{"sharding_column": "user_id", "type": "hash", "num_shards": 2}'

Adjust num_shards to match how many shards you need, and sharding_column to your actual shard key.

5. Add a Second Shard

Repeat steps 2 and 3 to launch a second MariaDB instance and VTTablet for shard 1:

# On your CentOS host:
docker run -d --name vitess-mariadb-1 --network vitess-network -e MYSQL_ROOT_PASSWORD=your_secure_password mariadb:latest

# Inside vitess/root container:
vttablet \
  --topo_implementation etcd2 \
  --topo_global_server_address localhost:2379 \
  --topo_global_root /vitess/global \
  --tablet-path test_keyspace-0000000101 \
  --tablet-hostname vitess-mariadb-1 \
  --init_keyspace test_keyspace \
  --init_shard 1 \
  --init_tablet_type replica \
  --mysql_host vitess-mariadb-1 \
  --mysql_port 3306 \
  --mysql_user root \
  --mysql_password your_secure_password \
  --port 15001 \
  --grpc_port 16001 \
  --service_map 'grpc-vttablet' \
  &

6. Launch VTGate (The Vitess Entry Point)

VTGate is the single entry point for your applications to access the sharded database. Start it inside the vitess/root container:

vtgate \
  --topo_implementation etcd2 \
  --topo_global_server_address localhost:2379 \
  --topo_global_root /vitess/global \
  --port 15999 \
  --grpc_port 16999 \
  --mysql_server_port 15306 \
  --service_map 'grpc-vtgate,mysql-vtgate' \
  &

Now you can connect to Vitess just like a regular MySQL database using this port:

mysql -h localhost -P 15306 -u root

7. Use Orchestrator for Replication Management (Optional)

You pulled the vitess/orchestrator image—it's a great tool for monitoring and managing MySQL replication topologies. Launch it on your CentOS host:

docker run -d --name vitess-orchestrator --network vitess-network -p 3000:3000 vitess/orchestrator:latest

You can now access the Orchestrator web UI by visiting http://your-centos-ip:3000 in your browser, then add your MariaDB instances to monitor their replication status.

Verify Your Sharding Setup

Once everything is running, let's make sure it's working. Connect to VTGate and create a table with your shard key:

CREATE TABLE users (
  user_id INT,
  name VARCHAR(50),
  PRIMARY KEY(user_id)
) ENGINE=InnoDB;

Insert some test data:

INSERT INTO users (user_id, name) VALUES (1, 'Alice'), (2, 'Bob'), (1000000, 'Charlie');

Then use vtctl to see how the query is routed across shards:

vtctl VExplain -keyspace test_keyspace "SELECT * FROM users"

You should see that queries are directed to the correct shard based on the user_id hash.


内容的提问来源于stack exchange,提问作者Jayd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:01:37