This is a collection of the most common commands I run while administering Mysql databases. The variables shown between the open and closed tags, "<" and ">", should be replaced with a name you choose. Mysql has multiple shortcut functions, starting with a forward slash, "". Any SQL command that is not a shortcut, must end with a semicolon, ";". You can use the keyboard UP and DOWN keys to scroll the history of previous commands you've run.
https://help.ubuntu.com/14.04/serverguide/mysql.html
sudo apt-get update
sudo apt-get install -y mysql-server
sudo mysql_secure_installation
sudo mysql_install_db
mysql -u <username> -p<password>
ssh -L <local_mysql_port>:<local_addr>:<remote_mysql_port> \
<username>@<remote_addr>
ssh -L 3306:127.0.0.1:3306 \
[email protected]
mysql -h 127.0.0.1 -u <username> -p<password> <databbase_name>
mysql> exit
mysql> quit
mysql> \q
<CTRL + L>
mysql> status;
Ubuntu 14.04 / Debian
sudo nano $(locate -l 1 my.cnf)
sudo service mysqld restart
Redhat / Fedora / CentOS
yum -y install mlocate
updatedb
sudo nano $(locate -l 1 my.cnf)
sudo service mysqld restart
https://dev.mysql.com/doc/refman/5.6/en/server-logs.html
Ubuntu 14.04 / Debian
sudo tail -24 /var/log/mysql.err
sudo tail -24 /var/log/mysql.log
Redhat / Fedora / Centos
sudo tail -24 /var/log/mysqld.log
SHOW VARIABLES LIKE "%version%";
SHOW STATUS;
SHOW VARIABLES;
SELECT USER();
SHOW GRANTS FOR CURRENT_USER;
SHOW DATABASES;
SELECT DATABASE();
SHOW TABLES;
SELECT * FROM mysql.func;
https://dev.mysql.com/doc/refman/5.6/en/show-databases.html
SHOW DATABASES;
http://dev.mysql.com/doc/refman/5.6/en/use.html
USE <database_name>;
SELECT DATABASE();
https://dev.mysql.com/doc/refman/5.6/en/create-database.html
CREATE DATABASE <database_name> IF NOT EXISTS;
http://dev.mysql.com/doc/refman/5.6/en/drop-database.html
DROP DATABASE IF EXISTS <database_name>;
SELECT User,Host,Password,Grant_priv,Super_priv FROM mysql.user;
SELECT * FROM mysql.user;
http://dev.mysql.com/doc/refman/5.6/en/create-user.html
CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';
http://dev.mysql.com/doc/refman/5.6/en/drop-user.html
DROP USER <username>;
http://dev.mysql.com/doc/refman/5.6/en/set-password.html
SET PASSWORD FOR '<username>'@'localhost' = PASSWORD('<password>');
http://dev.mysql.com/doc/refman/5.6/en/grant.html
SHOW GRANTS FOR CURRENT_USER;
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.* TO '<username>'@'localhost';
GRANT SELECT,UPDATE,INSERT,DELETE ON <database_name>.* TO '<username>'@'localhost';
GRANT SELECT,INSERT ON <database_name>.* TO '<username>'@'localhost';
GRANT SELECT ON <database_name>.* TO '<username>'@'localhost';
http://dev.mysql.com/doc/refman/5.6/en/show-tables.html
SHOW TABLES;
DESCRIBE <table_name>;
SHOW COLUMNS FROM <table_name>;
http://dev.mysql.com/doc/refman/5.6/en/create-table.html
CREATE TABLE <table_name> (
<col_name> INT,
<col_name> VARCHAR(128)
);
http://dev.mysql.com/doc/refman/5.6/en/drop-table.html
DROP TABLE <table_name>;
http://dev.mysql.com/doc/refman/5.6/en/rename-table.html
RENAME TABLE <old_name> TO <new_name>;
http://dev.mysql.com/doc/refman/5.6/en/alter-table.html
ALTER TABLE <table_name> IF EXISTS
ADD COLUMN <column_name> <column_type>;
ALTER TABLE <table_name> IF EXISTS
MODIFY COLUMN <column_name> <column_type>;
ALTER TABLE <table_name> IF EXISTS
DROP COLUMN <column_name>;
ALTER TABLE <table_name> IF EXISTS
MODIFY COLUMN <column_name> INT auto_increment;
http://dev.mysql.com/doc/refman/5.6/en/select.html
SELECT * FROM <table_name>;
SELECT * FROM <table_name> LIMIT 1;
SELECT * FROM <table_name> WHERE <column_name> = <value>;
http://dev.mysql.com/doc/refman/5.6/en/insert.html
INSERT INTO <table_name> VALUES();
http://dev.mysql.com/doc/refman/5.6/en/update.html
UPDATE <table_name>
SET <column_1> = <value_1>, <column_2> = <value_2>
WHERE <column_1> = <value_1>;
http://dev.mysql.com/doc/refman/5.6/en/delete.html
DELETE FROM <table_name>;
DELETE FROM <table_name>
WHERE <column_name> = <value>;
SELECT * FROM mysql.func;
https://dev.mysql.com/doc/refman/5.6/en/mysql-batch-commands.html
mysql -u <username> -p<password> <database_name> < mydb.sql
https://dev.mysql.com/doc/mysql-backup-excerpt/5.6/en/mysqldump-sql-format.html
http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html#mysqldump-option-summary
mysqldump -u <username> -p<password> <database_name> > <sql_file>
mysqldump --opt -u <username> -p<password> --no-create-info <database_name> > <sql_file>
mysqldump --opt -u <username> -p<password> --no-data <database_name> > <sql_file>
SELECT *
FROM <table_name>
INTO OUTFILE '<file_pathway>'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
SELECT <column_name1>, <column_name2>
FROM <table_name>
INTO OUTFILE '<file_pathway>'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
http://www.mysqltutorial.org/mysql-export-table-to-csv/
http://dev.mysql.com/doc/refman/5.6/en/explain.html
http://dev.mysql.com/doc/refman/5.6/en/debugging-server.html