π¬ = MySQL 8
π¦ = MariaDB
Why?
- Localize your database into one single folder of your choosing.
- No sudo / root requirement.
- Run multiple database at the same time on the same machine- Full bare metal performance.
- These are great starter databases moving to "slow single machine but horizontal scale": CockroachDB, ScyllaDB, etc.
- π¬ Download + extract to
database: https://dev.mysql.com/downloads/mysql/ - π¦ Download + extract to
database: https://mariadb.org/download - Open terminal,
cd database. Create data folder:mkdir -p data - π¬ Create system tables:
bin/mysqld --no-defaults --user=$USER --initializeSave the temporary password! - π¦ Create system tables:
scripts/mysql_install_db --datadir=./data --basedir=./ --user=$USER
π¬ Create ./database/my.cnf and add:
[mysqld]
max_connections = 8000
innodb_buffer_pool_size = 30G
innodb_log_file_size = 2G
innodb_log_buffer_size = 2G
innodb_flush_log_at_trx_commit = 2
π¦ Create ./database/my.cnf and add:
[mysqld]
data=./data
key_buffer_size=256M
max_connections = 8000
innodb_buffer_pool_size = 30G
innodb_log_file_size = 2G
innodb_log_buffer_size = 2G
Careful about making innodb_log* too large, because it's 2x on disk.
- π¬ Run:
bin/mysqld --defaults-file="./my.cnf" - π¦ Run:
bin/mariadbd --defaults-file="./my.cnf" --basedir=./ --user=$USER --bind-address=127.0.0.1 --socket=/tmp/mariadb
- π¬ Run:
bin/mysql --user=root -p
- π¦ Backup
- π¬
xtrabackup --backup --defaults-file="./my.cnf" --target-dir="./data_backup_$(date +%Y%m%d_%H%M%S)" --no-server-version-check --host=127.0.0.1 -u root -p- π¬ Use xtrabackup for MySQL 8 and Percona. Does not block database.
- π¦
./bin/mariabackup --defaults-file="./my.cnf" --backup --target-dir="./data_backup_$(date +%Y%m%d_%H%M%S)" --host=127.0.0.1 -u root -p- π¦ Mariabackup is a fork of xtrabackup, only supported for MariaDB. Does not block database.
- S3 or cluster: https://github.com/wal-g/wal-g
- π¬
- π Table Migrations: MoveTables in Vitess, or https://github.com/Shopify/ghostferry for vanilla MySQL.
- β Restore: It's just your
./datadirectory.- π¦ Prepare:
./bin/mariabackup --prepare --target-dir=...Afterwards, it's a fully functional data directory... stop server, replace./data, start server.
- π¦ Prepare:
- β« Upgrades: Just copy your config and
./datadirectory into a new MySQL. - π Export (too slow for backup, but useful to migrate to another database):
./bin/mysqldump test -h 127.0.0.1 > backup.sql - βοΈ Replication: https://youtu.be/s4oYWBGy_FE
- Logical (binary log) replication is ideal. Allows you to do upgrades, keeps bandwidth requirements low.
- Download new version: https://dev.mysql.com/downloads/mysql/
killall mysqldCheck to ensure server has shut down:pgrep mysqld- Keep
./dataand./my.cnf. Replace all other files. - Start up mysqld again, auto-upgrade sould start and report when completed.
- Your tables will be stored in
datadir=(./datain the above). basedir=is the extraction directory.mysqldwill be ran as$USER
- Use
ALGORITHM=INSTANT.. it's supported for most things (create, drop, rename column, etc.) except:- Primary, Secondary Indexes (Creation and rename).
- Re-ordering columns.
- Changing datatype or extending size.
- You should be using
BIGINTandVARCHAR(255)right away, see below.
- You should be using
- https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html
- Eventually: https://github.com/github/gh-ost
- Gh-ost is the successor to both pt-online-schema-change and Facebook online schema change.
- Vitess migrations are the successor to Gh-ost, but are Vitess-only.
- π¬ Change the password:
ALTER USER 'root'@'localhost' identified by 'password'; - π¦ Set default character set:
set character_set_server = utf8mb4;π¬ Will already beutf8mb4- Make sure your database and tables are all set to the utf8mb4 character set and collation or you will have garbled data when working with unicode! Emoji's, etc. will break!
- π¦ Change the password:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('cleartext password'); - Create a new user:
CREATE USER 'guy'@'localhost' IDENTIFIED BY 'password'; - Grant admin privileges:
GRANT ALL PRIVILEGES ON * . * TO 'guy'@'localhost';thenFLUSH PRIVILEGES; - Check out other options, example:
./bin/mysqld --help --verbose | grep "bind"
- Create a database:
CREATE DATABASE test; - Show databases:
show databases; - Use database:
use test; - Create table:
CREATE TABLE users (id BIGINT UNSIGNED PRIMARY KEY
, name VARCHAR(255)
, email VARCHAR(255)
, created_at DATETIME
, updated_at DATETIME);- Show table schema:
SHOW CREATE TABLE test;
Remaining compatible with SQLite and CockroachDB:
- Use
BIGINTorUUIDfor primary keys and indexes (BIGINTfor Snowflake IDUUIDfor uuid or ulid).- Do not use
SERIAL. Do not useAUTO INCREMENT. - https://gist.github.com/gnat/774bc540c0b300146cdedb0744bad081
- Do not use
- Use
TEXTorVARCHAR(255)(indexes must beVARCHAR(255)in MySQL) - Use
TINYINTinstead ofBOOLor tinyENUM(Ex: on/off/disabled) - Use
SMALLINTinstead ofENUM - Use
JSON - Use
VARCHAR(255)for Dates/Times. - Note
VARCHAR(255)is the same asVARCHAR(100)in MySQL becauseVARCHARstores 1 byte length.- The only advantage to using a lower number is a length check.
SHOW PROCESSLIST;
- https://github.com/shlomi-noach/awesome-mysql
MariaDB Xpand(Clustrix) is basically MariaDB done in the CockroachDB way. Like Vitess or Citus.MariaDB Xpandis rebranded Clustrix, brought into MariaDB core.- Backups are done the same as CockroachDB. Endpoint is given to all clients, and they push their pieces to it (SFTP / FTP)
- Vitess doesn't have indexes or transactions outside the local shard.
- Vitess may not support MariaDB in the future.. may support Postgres: https://github.com/shlomi-noach/awesome-mysql#sharding
- Probably not a huge issue with
MariaDB Xpand.
- Probably not a huge issue with
Galera Cluster(andPercona XtraDB,Maria Galera Cluster) doesnβt scale writes. Write all, read once. Shotgun architecture- Generally you dont want to scale past 5 nodes because diminishing returns. No raft or pax can cause deadlocks. Multi-master in the most basic sense. Should not be used: https://github.com/shlomi-noach/awesome-mysql#haMariaDB Xpandis designed to obsoleteGalera Cluster.
MariaDB MaxScaleLoad balancing forward proxy, likeProxySQLorMySQL Router- Used on top of
MariaDB Xpand - Forward proxy not always required. MariaDB / MySQL is "thread per connection" vs Postgres "process per connection".
- Used on top of
XtraDBis N/A. Was an alternative engine vsInnoDBbut was rolled intoInnoDB
RETURNINGclause.uuidtype.
- Ops experience tends to be smoother.
- Vitess VTOrc is the successor/fork to Github Orchestrator
- Vitess VReplication is the spiritual successor to Github gh-ost