Preloader
Web development
  • Estimated reading time: 1 Minute

How to create a MySQL database with the command line and set privileges to user

How to create a MySQL database with the command line and set privileges to user

In order to manipulate users and privileges from users in MySQL you will need to access the MySQL console as root/admin or an user with elevated privileges:

# Start MySQL console as root user and prompt for password
mysql -u root -p

This will start the console as the root user to execute queries from the CLI. Here you will able to grant access to any database to other users using the queries that we've wrote in this article.

1. Create database

Initially, you need a database to grant permissions on, so in case that you don't have any or you're learning, create a database using the following statement:

create database MyDatabase;

In this case the name of our database is MyDatabase.

2. Grant usage to user with password

As next you need to allow the access to the database, that in our case is MyDatabase to an user, which in this case is MyDatabaseUsername and is identified with MyPassword:

grant usage on *.* to MyDatabaseUsername@localhost identified by 'MyPassword';

3. Grant all privileges to database for user

Finally grant all privileges for the database to the previously granted user:

grant all privileges on MyDatabase.* to MyDatabaseUsername@localhost;

Now you should be able to access the database with the specified user in MySQL (in PHP applications etc).

Happy coding !

Share:
Carlos Delgado

Carlos Delgado

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Related articles
The Convergence of AI, Cybersecurity & Modern Web Development | OST
16 Jun, 2026
  • Estimated reading time: 4 Minutes
WordPress vs Custom Website: Which is Better for Your Business?
23 Apr, 2026
  • Estimated reading time: 5 Minutes
Guidelines To Follow In Test Case Writing
4 Mar, 2026
  • Estimated reading time: 4 Minutes
How to Build a Secure Authentication System in a Web Application
27 Feb, 2026
  • Estimated reading time: 5 Minutes
Weekly trending
What Makes a Portable Power Station Worth the Investment?
17 Jun, 2026
  • Estimated reading time: 4 Minutes
Building a Modern Lead Generation Workflow With Public Social Data
17 Jun, 2026
  • Estimated reading time: 3 Minutes
Best Instant Crypto Exchange for Lazy Investors
17 Jun, 2026
  • Estimated reading time: 6 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.