What's Actually Happening

Vitess queries fail with errors. Applications cannot execute queries against the Vitess cluster.

The Error You'll See

```bash $ mysql -h vtgate -P 15306 -u user -p

Error: target is not available (target: keyspace.shard) ```

Query error:

bash
Error: unsupported construct in query

Tablet error:

bash
Error: vttablet: rpc error: code = Unavailable

Schema error:

bash
Error: table 'mytable' not found in schema

Why This Happens

  1. 1.Keyspace not configured - Target keyspace not found
  2. 2.Shard not available - VTTablet not serving
  3. 3.Query not supported - SQL syntax not compatible
  4. 4.Schema mismatch - VSchema doesn't match database
  5. 5.VTGate configuration - Wrong routing rules
  6. 6.Topology issues - Topology service unreachable

Step 1: Check Vitess Cluster Status

```bash # List keyspaces: vtctlclient ListKeyspaces

# List shards: vtctlclient ListShards

# List tablets: vtctlclient ListAllTablets

# Check specific keyspace: vtctlclient GetKeyspace my_keyspace

# Check shard: vtctlclient GetShard my_keyspace/0

# Check tablet: vtctlclient GetTablet zone1-0000000100

# Check serving status: vtctlclient GetServingKeyspace my_keyspace

# List all serving tablets: vtctlclient ListServingTablets my_keyspace

# Check VTGate: kubectl get pods -l component=vtgate

# Check VTTablet: kubectl get pods -l component=vttablet ```

Step 2: Verify VTGate Status

```bash # Check VTGate pods: kubectl get pods -l component=vtgate

# View VTGate logs: kubectl logs -l component=vtgate

# Check VTGate health: curl http://vtgate:15001/debug/health

# Check VTGate query stats: curl http://vtgate:15001/debug/query_stats

# VTGate configuration: kubectl describe deploy vtgate | grep -A20 "Args:"

# Check VTGate connections: mysql -h vtgate -P 15306 -u user -p -e "SHOW PROCESSLIST"

# VTGate port: # MySQL: 15306 # gRPC: 15991 # HTTP: 15001

# Test connectivity: kubectl run mysql-client --image=mysql:8.0 --rm -it --restart=Never \ -- mysql -h vtgate -P 15306 -u user -p -e "SELECT 1" ```

Step 3: Check VTTablet Status

```bash # Check VTTablet pods: kubectl get pods -l component=vttablet

# View VTTablet logs: kubectl logs -l component=vttablet

# Check tablet health: vtctlclient GetTablet zone1-0000000100

# Check tablet serving state: vtctlclient GetServingKeyspace my_keyspace

# Check replication: vtctlclient ReplicationStatus my_keyspace/0

# Check tablet health in UI: curl http://vttablet:15001/debug/health

# Rebuild tablet if stuck: vtctlclient RebuildVSchemaGraph

# Repair tablet: vtctlclient RepairShard my_keyspace/0

# Check MySQL status: kubectl exec -it vttablet-0 -- mysqladmin -u root status ```

Step 4: Fix Keyspace and Shard Issues

```bash # Check keyspace exists: vtctlclient GetKeyspace my_keyspace

# Create keyspace if missing: vtctlclient CreateKeyspace my_keyspace

# Check shard: vtctlclient GetShard my_keyspace/0

# Create shard if missing: vtctlclient CreateShard my_keyspace/0

# Set keyspace serving: vtctlclient SetKeyspaceServing my_keyspace/0

# Check serving state: vtctlclient GetServingKeyspace my_keyspace

# Initialize shard: vtctlclient InitShardPrimary my_keyspace/0

# For sharded keyspaces: vtctlclient CreateKeyspace --sharded my_keyspace vtctlclient CreateShard my_keyspace/-80 vtctlclient CreateShard my_keyspace/80-

# Check VSchema: vtctlclient GetVSchema my_keyspace ```

Step 5: Check VSchema Configuration

```bash # View VSchema: vtctlclient GetVSchema my_keyspace

# Apply VSchema: vtctlclient ApplyVSchema --sql="$(cat vschema.sql)" my_keyspace

# Example VSchema: { "sharded": true, "vindexes": { "user_id": { "type": "hash" } }, "tables": { "users": { "column_vindexes": [ { "column": "user_id", "name": "user_id" } ] } } }

# Rebuild VSchema graph: vtctlclient RebuildVSchemaGraph

# Validate VSchema: vtctlclient ValidateVSchema my_keyspace

# Check routing rules: vtctlclient GetRoutingRules

# Apply routing rules: vtctlclient ApplyRoutingRules --rules='{"rules": []}' ```

Step 6: Fix Query Syntax Issues

```bash # Vitess supports subset of MySQL syntax

# Common unsupported queries:

# Cross-shard JOINs: # Error: unsupported cross-shard query # Solution: Query each shard separately or use reference tables

# Subqueries in WHERE: # Error: unsupported subquery # Solution: Rewrite as JOIN

# Aggregate functions on large results: # Error: result too large # Solution: Use streaming or limit results

# Check query plan: vtctlclient GetQueryPlan "SELECT * FROM users WHERE id = 1"

# Test query: mysql -h vtgate -e "SELECT * FROM users WHERE id = 1"

# Enable query logging: vtctlclient SetVTGateDebugVars --key=debug_query_log --value=true

# View query log: kubectl logs -l component=vtgate | grep -i query ```

Step 7: Check Topology Service

```bash # Check topology implementation: # etcd2, consul, or zk

# For etcd: etcdctl --endpoints=http://etcd:2379 get /vitess/

# Check topology path: etcdctl get /vitess/global/keyspaces --prefix

# Verify topology registration: vtctlclient GetTabletMap

# Repair topology: vtctlclient RebuildKeyspaceGraph my_keyspace

# Check cell: vtctlclient GetCellInfo zone1

# Create cell if missing: vtctlclient AddCellInfo --server-address=etcd:2379 --root=/vitess zone1

# List cells: vtctlclient GetCells

# Prune orphaned tablets: vtctlclient PruneOrphanedTablets ```

Step 8: Handle Replication Issues

```bash # Check replication status: vtctlclient ReplicationStatus my_keyspace/0

# Check tablet types: vtctlclient ListAllTablets | grep my_keyspace

# Promote replica to primary: vtctlclient TabletExternallyReparented zone1-0000000100

# Emergency reparent: vtctlclient EmergencyReparentShard my_keyspace/0 new_primary_alias

# Planned reparent: vtctlclient PlannedReparentShard my_keyspace/0 new_primary_alias

# Check reparent journal: vtctlclient GetShardReplicationPositions my_keyspace/0

# Fix broken replication: # 1. Check MySQL replication: kubectl exec -it vttablet-0 -- mysql -e "SHOW REPLICA STATUS\G"

# 2. Skip replication error: kubectl exec -it vttablet-0 -- mysql -e "SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START REPLICA;"

# 3. Rebuild replica if needed: vtctlclient ReparentTablet zone1-0000000101 ```

Step 9: Monitor Vitess Health

```bash # VTGate metrics: curl http://vtgate:15001/metrics | grep vtgate_

# VTTablet metrics: curl http://vttablet:15001/metrics | grep vttablet_

# Key metrics to monitor: # vtgate_queries_total - Query count # vtgate_query_latency - Query latency # vttablet_qps - Queries per second # vttablet_replication_lag - Replication lag

# Check query patterns: vtctlclient GetQueryPatterns

# Check error counts: vtctlclient GetErrors

# Prometheus integration: # Vitess exports metrics in Prometheus format

# Grafana dashboards: # Import Vitess dashboards from: # https://github.com/vitessio/vitess/tree/master/docker/grafana/dashboards ```

Step 10: Vitess Query Verification Script

```bash # Create verification script: cat << 'EOF' > /usr/local/bin/check-vitess.sh #!/bin/bash

echo "=== Vitess Cluster Status ===" vtctlclient ListKeyspaces vtctlclient ListShards

echo "" echo "=== VTGate Pods ===" kubectl get pods -l component=vtgate

echo "" echo "=== VTTablet Pods ===" kubectl get pods -l component=vttablet

echo "" echo "=== Serving Tablets ===" vtctlclient ListServingTablets my_keyspace 2>/dev/null || echo "Keyspace not found"

echo "" echo "=== Tablet Health ===" for tablet in $(vtctlclient ListAllTablets | awk '{print $1}'); do echo "Tablet: $tablet" vtctlclient GetTablet $tablet | grep -i "health|state" | head -3 done

echo "" echo "=== Replication Status ===" vtctlclient ReplicationStatus my_keyspace/0 2>/dev/null || echo "Shard not found"

echo "" echo "=== VSchema ===" vtctlclient GetVSchema my_keyspace 2>/dev/null | head -20 || echo "VSchema not found"

echo "" echo "=== VTGate Health ===" curl -s http://vtgate:15001/debug/health 2>/dev/null || echo "VTGate not reachable"

echo "" echo "=== Test Query ===" mysql -h vtgate -P 15306 -u user -e "SELECT 1" 2>/dev/null && echo "Query OK" || echo "Query failed"

echo "" echo "=== Recommendations ===" echo "1. Ensure keyspace and shard exist" echo "2. Check VTGate and VTTablet pods are running" echo "3. Verify tablet serving state" echo "4. Check VSchema matches your schema" echo "5. Review query for unsupported syntax" echo "6. Verify topology service connectivity" echo "7. Check replication status for each shard" EOF

chmod +x /usr/local/bin/check-vitess.sh

# Usage: /usr/local/bin/check-vitess.sh ```

Vitess Query Checklist

CheckExpected
KeyspaceExists and serving
ShardAll replicas healthy
VTGateRunning and connected
VTTabletServing state
VSchemaValid and applied
QuerySupported syntax
TopologyAll nodes registered

Verify the Fix

```bash # After fixing Vitess query issues

# 1. List keyspaces vtctlclient ListKeyspaces // Keyspace listed

# 2. Check serving vtctlclient GetServingKeyspace my_keyspace // Serving state: SERVING

# 3. Test query mysql -h vtgate -P 15306 -e "SELECT * FROM users LIMIT 1" // Returns results

# 4. Check VTGate curl http://vtgate:15001/debug/health // OK

# 5. Verify tablets vtctlclient ListServingTablets my_keyspace // All tablets serving

# 6. Check replication vtctlclient ReplicationStatus my_keyspace/0 // Replication running ```

  • [Fix MySQL Replication Broken](/articles/fix-mysql-replication-broken)
  • [Fix MySQL Connection Refused](/articles/fix-mysql-connection-refused)
  • [Fix MySQL Slow Query](/articles/fix-mysql-slow-query)