MySQL - Common Command Syntaxes
Disclaimer:
The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author assumes no responsibility for any loss, damage, or issues arising from the use or misuse of the content provided.
User Management
Show current user list:
mysql> SELECT user, host, plugin FROM mysql.user;
Add new user:
mysql> CREATE USER '<new_username>'@'<host>' IDENTIFIED BY '<new_password>';
Where:
<new_username> is unique new username to create/add.
<host> is the hostname or IP address of host where user will login from. If local, use 'localhost'.
<new_password> is the new password for <new_username>.
NOTE: Username is actually a combination of '<new_username>'@'<host>', such as 'user'@'localhost'.
To change user password:
mysql> ALTER USER '<username>'@'<host>' IDENTIFIED BY '<new_password>';
Where:
<username> is the username to change the password.
<host> is the hostname or IP address of host where user will login from. If local, use 'localhost'.
<new_password> is the new password for <new_username>.
NOTE: Username is actually a combination of '<username>'@'<host>', such as 'user'@'localhost'.
To remove/delete a user from MySQL:
mysql> DROP USER '<username>'@'<host>';
Where:
<username> is the username to change the password.
<host> is the hostname or IP address of host where user will login from. If local, use 'localhost'.
NOTE: Username is actually a combination of '<username>'@'<host>', such as 'user'@'localhost'.
To See Access Levels for User:
mysql> SHOW GRANTS FOR '<username>'@'<host>';
Where:
<username> is the username to see the list of access this user has.
<host> is the hostname or IP address of host where user will login from. If local, use 'localhost'.
NOTE: Username is actually a combination of '<username>'@'<host>', such as 'user'@'localhost'.
To Add Permissions to User:
mysql> GRANT <ACCESS>,<ACCESS> ON <database>.* TO '<username>'@'<host>';
Where:
<ACCESS> is type of access or privilege to add to this user. To add more than one, add them separated by comma.
<username> is the username to add access.
<database> is the specific database for user access. For all databases use asterisk(*).
NOTE: Username is actually a combination of '<username>'@'<host>', such as 'user'@'localhost'.
Example: Grant all privilege of a specific database to user:
mysql> GRANT ALL PRIVILEGES ON <database>.* TO 'user'@'localhost';
Example: Grant DML access (select/insert/update/delete)
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON <database>.* TO 'user'@'localhost';
mysql> GRANT SELECT ON <database>.* TO 'user'@'localhost';
Example: Grant replication access (on all data: *.*)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'user'@'localhost';
Example: Grant Backup/Clone access (on all data: *.*)
mysql> GRANT BACKUP_ADMIN, CLONE_ADMIN ON *.* TO 'user'@'localhost';
Example: Grant all privilege (ADMIN RIGHTS) to user:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
To Remove/Revoke Permissions to User:
mysql> REVOKE <ACCESS>,<ACCESS> ON <database>.* FROM '<username>'@'<host>';
NOTE: Not REVOKE "TO" user, but REVOKE "FROM" user.
Where:
<ACCESS> is type of access or privilege to remove/revoke from this user. To revoke more than one, add them separated by comma.
<username> is the username to remove access from
<database> is the specific database for user access. For all databases use asterisk(*).
NOTE: Username is actually a combination of '<username>'@'<host>', such as 'user'@'localhost'.
Example: Revoke ALL privilege from user:
mysql> REVOKE ALL PRIVILEGES ON *.* FROM 'user'@'localhost';
Example: Revoke all privilege of a specific database from user:
mysql> REVOKE ALL PRIVILEGES ON <database>.* FROM 'user'@'localhost';
Create Test Database with Mock Data for Demo or Testing:
1. Create test database, testdb:
mysql> CREATE DATABASE testdb;
2. Switch or make database active for work
3. Create first database table (empty), users, for testdb:
mysql> CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
4. Add mockup data to table, users:
mysql> INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'), ('Bob', 'bob@example.com'), ('Charlie', 'charlie@example.com');
5. Add additional data to table, users:
mysql> INSERT INTO users (name, email) VALUES ('Dale', 'dale@example.com'), ('Edgar', 'ed@example.com'), ('Fred', 'fred@example.com');
Related Articles
MySQL - Installing Latest Percona mysql on Debian 12
Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...
MySQL - How to Clone All Data from DONOR MySQL server to another using MySQL Clone Plugin
Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...
MySQL - How to use mysqldump
Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...
MySQL - Setup multi-threaded replication
Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...
MySQL - Enable SSL connection between MySQL hosts using self-signed SSL
Disclaimer: The information provided in this guide is for educational and informational purposes only. It is provided “as is” without any representations or warranties, express or implied. You are using this information at your own risk. The author ...