Reference for working with .
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
Copy sudo apt-get install mysql-server
macOS
Secure MySQL installation
Copy mysql_secure_installation
macOS post install instructions via homebrew
Copy unset TMPDIR
mysql_install_db
mysql.server start
mysql_secure_installation
Show installed MySQL version
Control MySQL Server
Linux
Copy service mysql start | stop | restart | status
macOS
Copy mysql.server start | stop | restart | status
mysql.server reload | force-reload
Run MySQL as a background service (Homebrew / macOS)
Copy brew services start mysql@<version>
macOS homebrew executable location
Copy /usr/local/Cellar/mysql/<version>/support-files
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
Copy sudo du -skh <mysql-directory>
Manage Users
Create a user
Copy mysql> CREATE USER '<user>'@'<host>' IDENTIFIED BY '<password>';
mysql> FLUSH PRIVILEGES;
Delete a user
Copy mysql> DROP USER '<user>'@'<host>';
Change a user's password (as root)
Copy mysql> SET PASSWORD FOR '<user>'@'<host>' = PASSWORD('<password>');
Show list of users
Copy mysql> SELECT User FROM mysql.user;
Manage Privileges
Assign database privileges
Privileges:
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
Copy mysql> GRANT ALL PRIVILEGES ON <database>.* TO '<user>'@'<host>';
mysql> FLUSH PRIVILEGES;
Grant general privileges
Copy mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON <database>.* TO '<user>'@'<host>';
mysql> FLUSH PRIVILEGES;
Grant table privileges
Copy mysql> GRANT CREATE, DROP, ALTER ON <database>.* TO '<user>'@'<host>';
Revoke all privileges
Copy mysql> REVOKE ALL ON <database>.* FROM '<user>'@'<host>';
Revoke specific privileges
Copy mysql> REVOKE DELETE, INSERT ON <database>.* FROM '<user>'@'<host>';
Show privileges
Copy mysql> SHOW GRANTS FOR '<user>'@'<host>';
Navigate Databases and Tables
Show list of available databases
Copy mysql> SHOW DATABASES;
Select database to use for subsequent SQL commands
Copy mysql> USE <database>;
Show tables for selected database
Show the column names and column types for a table
Copy mysql> DESCRIBE <table>;
Show all rows that exist in a column
Copy mysql> SELECT * FROM <column>;
Create and Delete databases
Create a new database
Copy mysql> CREATE DATABASE <database>;
Delete an existing database
Copy mysql> DROP DATABASE <database>;
Create, Delete and Alter Tables
Create a table and columns
Copy mysql> CREATE TABLE <table> (<column> <type> <settings>);
Create a table and columns (example)
Copy mysql> CREATE TABLE <table> (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, <column> VARCHAR(45));
Rename table
Copy mysql> RENAME TABLE <table> to <new table>;
Delete table
Copy mysql> DROP TABLE <table>;
Add table column
Copy mysql> ALTER TABLE <table> ADD <column> <type> <settings>;
Add table column in location (example)
Copy mysql> ALTER TABLE <table> ADD <new column> VARCHAR(45) NULL AFTER `<column>`;
Insert record
Copy mysql> INSERT INTO <table> (<column>, <other column>) VALUES ('<value>', '<other value>');
Select, Update and Delete Rows
Select records
Copy mysql> SELECT * FROM <table>;
Update record
Copy mysql> UPDATE <table> SET <column> = '<value>';
Update record with condition
Copy mysql> UPDATE <table> SET <column> = '<value>' WHERE <table>.<other column> = <condition>;
Delete all rows
Copy mysql> DELETE FROM <table>;
Delete specific record
Copy mysql> DELETE FROM <table> WHERE <table>.<column> = '<value>';
MySQL Database Import / Export
Export database
Copy mysqldump -u root -p <database> > <file.sql>
Import database
Copy mysqladmin -u root -p create <database>
mysql -u root -p <database> < <file.sql>
Executing .sql files
Copy mysql> source <file.sql>
Alternative way to execute .sql files
Output MySQL settings
Rename Database
Copy mysqldump -u root -p -v <database> > <file.sql>
mysqladmin -u root -p create <new database>
mysql -u root -p <new database> < <file.sql>
Check Database Configuration
Show current character set configuration
Copy mysql> show variables like 'char%';