Creating a MySQL User
If you are using MySQL databases, you may want to control database access by creating specific database users. Each user can be restricted to certain databases, tables, and privileges.
You can create MySQL users using:
Using cPanel to Add MySQL Users
If you aren’t familiar with using the command line interface, use cPanel to create MySQL users.
Note: |
| This article assumes that you have logged into cPanel for the specific domain you wish to create a database user in. If you are unsure how to locate your cPanel accounts and log in, please see our articles: Logging Into cPanel and Creating a cPanel User Account |
- Log into the cPanel account for the database’s domain.
- Scroll down to the Databases section on the cPanel home page and click on MySQL Databases. This is where you can create new databases, manage current databases, create users, and manage users.
- Scroll down to the Add New User section. Choose a Username for your new database user, then choose and confirm a strong Password.
Tip:
Using the password generator will help you to create a strong password, more information can be found in our article: Best Practice: Creating a Secure Password?. - Click Create User. You will see a message confirming that your user has been created.
You’ve created a MySQL user! The next step is Adding MySQL Users to Databases.
Using Command Line to Add MySQL Users
If you’re comfortable with the command line, it only takes a few minutes to add MySQL users.
- Using the terminal program of your choice, log into your server as root. If you haven’t logged into your server using the command line before, read Logging into Your Server via Secure Shell (SSH) first.
- Now you’ll log into your MySQL server by typing:
mysql -u root -p
The -u root flag tells MySQL you want to log in as the root user, and the -p flag prompts MySQL to ask you for a password. When prompted, enter your root password and press Enter.
- You’ll now see a MySQL prompt that looks like:
mysql>
- Now you’ll use a few simple MySQL commands to create a user. In the command prompt, type:
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password1!';
Replace “testuser” with the username you’d like and “password1!” with the password you’d like.
You’ve created a MySQL user! The next step is to use the command line for Adding MySQL Users to Databases.