Login
Usually needs root user level access
sudo su -
Login as a user, enter password at prompt e.g.:
mysql -u root -p
mysql -u wordpress -p
Avoid prompt
mysql -u wordpress -ppassword
Typical commands
SHOW
, CREATE
, USE
, DROP
Databases
Show all databases
SHOW DATABASE;
Create a database
CREATE DATABASE databasename;
Open the database
USE databasename;
Delete database
DROP DATABASE databasename;
Permissions
The phrase "grant all" works just as well as "grant all privileges"
GRANT ALL PRIVILEGES ON databasename.* TO 'username'@'%' IDENTIFIED BY 'password';
Allow username to access everything in database using password
GRANT ALL ON databasename.* TO 'username' IDENTIFIED BY 'password';
Give username the ability to grant permissions to other users
GRANT ALL ON databasename.* TO 'username' IDENTIFIED BY 'password' WITH GRANT OPTION;
Configure remote access for users/servers
GRANT ALL ON databasename.* TO 'username'@'%' IDENTIFIED BY 'password';
Tables
Show all tables
SHOW TABLES;
Table Columns
Show all columns in a table
SHOW COLUMNS FROM databasename.tablename;
Queries
Typical query
SELECT * FROM databasename.tablename;
Replace string found in a field
UPDATE databasename.tablename SET url = REPLACE(url, 'http://appstore', 'https://appstore');
UPDATE blog.wp_options SET option_value = REPLACE(option_value, 'https://website.domain/blog', 'http://localhost');
Manage Users
Show all users
SELECT User,Host FROM mysql.user;
Remove a user called "wordpress"
DROP USER wordpress@localhost;
Tips
Use carats instead of single quotes when database name has a dash
CREATE DATABASE `databasename-with-dash`; AND GRANT ALL ON `databasename-with-dash`.* TO 'username'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
Always run flush privileges after creating users or changing any grant commands
flush privileges;
Revoke/Remove users
DROP USER 'username'@'localhost';
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'username'@'*';
DROP USER 'username'@'*';
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'username'@'10.251.61.174';
DROP USER 'username'@'10.251.61.174';
flush privileges;
exit```