🐬 = 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 --initialize
Save 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 = 30G
innodb_log_buffer_size = 512M
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 = 30G
innodb_log_buffer_size = 512M
- 🐬 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
./data
directory.- 🦭 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
./data
directory 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 mysqld
Check to ensure server has shut down:pgrep mysqld
- Keep
./data
and./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=
(./data
in the above). basedir=
is the extraction directory.mysqld
will 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
BIGINT
andVARCHAR(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
BIGINT
orUUID
for primary keys and indexes (BIGINT
for Snowflake IDUUID
for uuid or ulid).- Do not use
SERIAL
. Do not useAUTO INCREMENT
. - https://gist.github.com/gnat/774bc540c0b300146cdedb0744bad081
- Do not use
- Use
TEXT
orVARCHAR(255)
(indexes must beVARCHAR(255)
in MySQL) - Use
TINYINT
instead ofBOOL
or tinyENUM
(Ex: on/off/disabled) - Use
SMALLINT
instead ofENUM
- Use
JSON
- Use
VARCHAR(255)
for Dates/Times. - Note
VARCHAR(255)
is the same asVARCHAR(100)
in MySQL becauseVARCHAR
stores 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 Xpand
is 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 Xpand
is designed to obsoleteGalera Cluster
.
MariaDB MaxScale
Load balancing forward proxy, likeProxySQL
orMySQL 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
XtraDB
is N/A. Was an alternative engine vsInnoDB
but was rolled intoInnoDB
RETURNING
clause.uuid
type.
- 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