MySQL

Reference for working with MySQL.

For database, table and column names that are reserved words or irregular names, surround the names with backticks `. For string values, wrap the values with single ' or double " quotes.

If you are operating on a local MySQL database, <host> in the examples below will be localhost.

Install MySQL Server

Linux

sudo apt-get install mysql-server

macOS

brew install mysql

Secure MySQL installation

mysql_secure_installation

macOS post install instructions via homebrew

unset TMPDIR
mysql_install_db
mysql.server start
mysql_secure_installation

MySQL Information

Show installed MySQL version

Control MySQL Server

Linux

macOS

Run MySQL as a background service (Homebrew / macOS)

macOS homebrew executable location

Log In/Out

Log into MySQL as root

Log into MySQL as <user>

Exit MySQL

Database Files

Linux

macOS

Report total size of MySQL databases

Manage Users

Create a user

Delete a user

Change a user's password (as root)

Show list of users

Manage Privileges

Assign database privileges

Privileges:

  • SELECT - Read only

  • INSERT - Insert rows/data

  • UPDATE - Change inserted rows/data

  • DELETE - Delete drop rows of data

  • CREATE - Create new tables

  • ALTER - Change table/column names

  • DROP - Drop columns/tables

Grant all privileges

Grant general privileges

Grant table privileges

Revoke all privileges

Revoke specific privileges

Show privileges

Show list of available databases

Select database to use for subsequent SQL commands

Show tables for selected database

Show the column names and column types for a table

Show all rows that exist in a column

Create and Delete databases

Create a new database

Delete an existing database

Create, Delete and Alter Tables

Create a table and columns

Create a table and columns (example)

Rename table

Delete table

Add table column

Add table column in location (example)

Insert record

Select, Update and Delete Rows

Select records

Update record

Update record with condition

Delete all rows

Delete specific record

Delete all non-matching records

Using Wildcards

Wildcards via % can be used for grants on database names (literal underscores must be escaped \_)

Grant select for databases that match foo_* (eg. foo_bar, foo_qux)

Revoke all for foo_*

Note: Grants and revokes made in this manner must match the same wildcard value (eg. foo\_%)

MySQL Database Import / Export

Export database

Export database (and include CREATE DATABASE and USE commands)

Export multiple databases

Export all databases

Import database

Executing .sql files

Alternative way to execute .sql files

Output MySQL settings

Rename Database

Check Database Configuration

Show current character set configuration

Show database variables

Last updated