Run Vitess Locally

Instructions for using Vitess on your machine for testing purposes

This guide covers installing Vitess locally for testing purposes, from pre-compiled binaries. We will launch multiple copies of mysqld, so it is recommended to have greater than 4GB RAM, as well as 20GB of available disk space.

Download Vitess Package

Download and extract the latest .tar.gz release from GitHub.

Install MySQL and etcd

Vitess supports MySQL 5.6+ and MariaDB 10.0+. We recommend MySQL 5.7 if your installation method provides a choice:

  1. # Ubuntu based
  2. sudo apt install -y mysql-server etcd
  3. # Debian
  4. sudo apt install -y default-mysql-server default-mysql-client etcd
  5. # Yum based
  6. sudo yum -y localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  7. sudo yum -y install mysql-community-server etcd

On apt-based distributions the services mysqld and etcd will need to be shutdown, since etcd will conflict with the etcd started in the examples, and mysqlctl will start its own copies of mysqld:

  1. # Debian and Ubuntu
  2. sudo service mysql stop
  3. sudo service etcd stop
  4. sudo systemctl disable mysql
  5. sudo systemctl disable etcd

Disable AppArmor or SELinux

AppArmor/SELinux will not allow Vitess to launch MySQL in any data directory by default. You will need to disable it:

AppArmor:

  1. # Debian and Ubuntu
  2. sudo ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/
  3. sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld
  4. # The following command should return an empty result:
  5. sudo aa-status | grep mysqld

SELinux:

  1. # CentOS
  2. sudo setenforce 0

Configure Environment

Add the following to your .bashrc file. Make sure to replace /path/to/extracted-tarball with the actual path to where you extracted the latest release file:

  1. export VTROOT=/path/to/extracted-tarball
  2. export VTTOP=$VTROOT
  3. export VTDATAROOT=${HOME}/vtdataroot
  4. export PATH=${VTROOT}/bin:${PATH}

You are now ready to start your first cluster!

Start a Single Keyspace Cluster

A keyspace in Vitess is a logical database consisting of potentially multiple shards. For our first example, we are going to be using Vitess without sharding using a single keyspace. The file 101_initial_cluster.sh is for example 1 phase 01. Lets execute it now:

  1. cd examples/local
  2. ./101_initial_cluster.sh

You should see output similar to the following:

  1. ~/...vitess/examples/local> ./101_initial_cluster.sh
  2. enter etcd2 env
  3. add /vitess/global
  4. add /vitess/zone1
  5. add zone1 CellInfo
  6. etcd start done...
  7. enter etcd2 env
  8. Starting vtctld...
  9. Access vtctld web UI at http://morgox1:15000
  10. Send commands with: vtctlclient -server morgox1:15999 ...
  11. enter etcd2 env
  12. Starting MySQL for tablet zone1-0000000100...
  13. Starting MySQL for tablet zone1-0000000101...
  14. Starting MySQL for tablet zone1-0000000102...
  15. Starting vttablet for zone1-0000000100...
  16. Access tablet zone1-0000000100 at http://morgox1:15100/debug/status
  17. Starting vttablet for zone1-0000000101...
  18. Access tablet zone1-0000000101 at http://morgox1:15101/debug/status
  19. Starting vttablet for zone1-0000000102...
  20. ..

You can also verify that the processes have started with pgrep:

  1. ~/...vitess/examples/local> pgrep -fl vtdataroot
  2. 26563 etcd
  3. 26626 vtctld
  4. 26770 mysqld_safe
  5. 26771 mysqld_safe
  6. 26890 mysqld_safe
  7. 29910 mysqld
  8. 29925 mysqld
  9. 29945 mysqld
  10. 30035 vttablet
  11. 30036 vttablet
  12. 30037 vttablet
  13. 30218 vtgate

The exact list of processes will vary. For example, you may not see mysqld_safe listed.

If you encounter any errors, such as ports already in use, you can kill the processes and start over:

  1. pkill -f '(vtdataroot|VTDATAROOT)' # kill Vitess processes

Connect to Your Cluster

You should now be able to connect to the VTGate server that was started in 101_initial_cluster.sh. To connect to it with the mysql command line client:

  1. ~/...vitess/examples/local> mysql -h 127.0.0.1 -P 15306
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 1
  4. Server version: 5.5.10-Vitess (Ubuntu)
  5. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> show tables;
  11. +-----------------------+
  12. | Tables_in_vt_commerce |
  13. +-----------------------+
  14. | corder |
  15. | customer |
  16. | product |
  17. +-----------------------+
  18. 3 rows in set (0.01 sec)

It is recommended to configure the MySQL command line to default to these settings, as the user guides omit -h 127.0.0.1 -P 15306 for brevity:

  1. cat << EOF > ~/.my.cnf
  2. [client]
  3. host=127.0.0.1
  4. port=15306
  5. EOF

Repeating the previous step, you should now be able to use the mysql client without any settings:

  1. ~/...vitess/examples/local> mysql
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 1
  4. Server version: 5.5.10-Vitess (Ubuntu)
  5. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> show tables;
  11. +-----------------------+
  12. | Tables_in_vt_commerce |
  13. +-----------------------+
  14. | corder |
  15. | customer |
  16. | product |
  17. +-----------------------+
  18. 3 rows in set (0.00 sec)

You can also browse to the vtctld console using the following URL:

  1. http://localhost:15000

Summary

In this example, we deployed a single unsharded keyspace named commerce. Unsharded keyspaces have a single shard named 0. The schema reflects a common ecommerce scenario:

  1. create table product (
  2. sku varbinary(128),
  3. description varbinary(128),
  4. price bigint,
  5. primary key(sku)
  6. );
  7. create table customer (
  8. customer_id bigint not null auto_increment,
  9. email varbinary(128),
  10. primary key(customer_id)
  11. );
  12. create table corder (
  13. order_id bigint not null auto_increment,
  14. customer_id bigint,
  15. sku varbinary(128),
  16. price bigint,
  17. primary key(order_id)
  18. );

The schema has been simplified to include only those fields that are significant to the example:

  • The product table contains the product information for all of the products.
  • The customer table has a customer_id that has an auto_increment. A typical customer table would have a lot more columns, and sometimes additional detail tables.
  • The corder table (named so because order is an SQL reserved word) has an order_id auto-increment column. It also has foreign keys into customer(customer_id) and product(sku).

Next Steps

You can now proceed with Vertical Split.

Or alternatively, if you would like to teardown your example:

  1. ./401_teardown.sh