Skip to content

Instantly share code, notes, and snippets.

@andyvanee
Last active October 29, 2019 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyvanee/dbfae2f0e3385598aed0e04b5563a504 to your computer and use it in GitHub Desktop.
Save andyvanee/dbfae2f0e3385598aed0e04b5563a504 to your computer and use it in GitHub Desktop.
Steps to run MySQL from a given data directory.

Sometimes it's nice to run MySQL (MariaDB) on your local machine in a completely contained environment. This outlines the minimum steps to make that happen.

  • Start with setup.sh to get a root login.
  • Then create a non-root user with the following:

GRANT ALL PRIVILEGES ON *.* TO 'user-name'@'localhost' IDENTIFIED BY 'user-password';

  • Update data/my.cnf with the client username and password you just created.
  • You should probably also add the entire data directory to your .gitignore file.
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
port=3306
datadir=data/
socket=mysqld.sock
[client]
port=3306
socket=data/mysqld.sock
user=root
password=change-this-password
# Create database files in the directory of your choice
mysql_install_db --datadir=$(pwd)/data --auth-root-authentication-method=normal
# Start the database. To stop the server, send SIGQUIT (Control-\)
mysqld --defaults-file=$(pwd)/data/my.cnf --datadir=$(pwd)/data
# In another tab, set the root password
mysqladmin -S $(pwd)/data/mysqld.sock -u root password 'root-password'
# Now connect as root
mysql --defaults-file=data/my.cnf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment