Categories
Server

MySQL: create database with user and password

The following instructions are to create:

  • one database
  • one user with all privileges on this database

Connect to MySQL with root user.

mysql -u root -p

Create a database and give it a name.
For this example we name it “db_name”.

create database db_name;

Create a user.
For this example we name it “db_users”;

create user db_user;

Now one stone three birds:
– we are giving all privileges to the user “db_user” on all the tables of the database “db_name”.
– we say that the user can login only locally.
– we create the user password.

grant all on db_name.* to 'db_user'@'localhost' identified by 'db_password';

To test it, first log out from root.

exit

Connect as your new user to the new database

mysql -u db_user -p db_name

Congratulations!