Creating a mysql db, user and set password to it from cli

By | 27th July 2015

While developing a CMS we don’t have phpmyadmin, plesk or cpanel rather a simple cli. So to create a quick DB with a user who has all privileges to the DB and a password to the user just give these commands. this method will works for mariadb too.

$ mysql -u adminusername -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 13
Server version: 5.6.25-3 (Debian)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> CREATE DATABASE databasename;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
 
mysql> GRANT ALL PRIVILEGES ON databasename.* TO "username"@"hostname";
Query OK, 0 rows affected (0.00 sec)
  
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> EXIT
Bye
$

For reseting root password

MariaDB [(none)]> use mysql;
MariaDB [(none)]> update user set password=PASSWORD("NEWPASSWORD") where User='root';

Don’t forget to replace NEWPASSWORD with new password