MySQL Database and User
MySQL Database and User Creation
Sometimes you need to add an user to a database with proper permission.
You can follow below steps to create database and user and grant permissions.
- First login to mysql with root user. Say mysql root user password is root123.mysql -u root -p
password: root123 - Assume we will create database my_database
- Assume we will create user my_user with password as my_password
- Delete the database if existsdrop database if exists my_database;
- Delete the user if existsdrop user 'my_user'@'localhost';
drop user 'my_user'@'%';The % above means user at any host.
- Now create the user with permissions both for local machine and remote access of DBCREATE USER 'my_user'@'localhost' IDENTIFIED BY 'my_password';
CREATE USER 'my_user'@'%' IDENTIFIED BY 'my_password'; - Now grant the permissionGRANT ALL ON *.* TO 'my_user'@'localhost';
GRANT ALL ON *.* TO 'my_user'@'%'; - Create databasecreate database my_database;
- Lets allow user my_user to connect to the server from localhost using the password my_password.grant usage on *.* to my_user@'localhost' identified by 'my_password';
grant usage on *.* to my_user@'%' identified by 'my_password'; - And finally we grant all privileges on the my_database to my_user.grant all privileges on my_database.* to my_user@'localhost' ;
grant all privileges on my_database.* to my_user@'%' ;
Thats all. Now you have created database my_database, user my_user with password as my_password.
Example
Say you want to create database lm_database with user as lm_user and password as lm_password.
Lets combine all the steps given above, so one you need execute below bunch of commands at mysql prompt.
CREATE USER 'lm_user'@'localhost' IDENTIFIED BY 'lm_password';
CREATE USER 'lm_user'@'%' IDENTIFIED BY 'lm_password';
CREATE USER 'lm_user'@'%' IDENTIFIED BY 'lm_password';
GRANT ALL ON *.* TO 'lm_user'@'localhost';
GRANT ALL ON *.* TO 'lm_user'@'%';
create database lm_database;
grant usage on *.* to lm_user@'localhost' identified by 'lm_password';
grant usage on *.* to lm_user@'%' identified by 'lm_password';
grant all privileges on lm_database.* to lm_user@'localhost' ;
grant all privileges on lm_database.* to lm_user@'%' ;
MySQL Database Backup and Restore
From command line you can easily backup and restore the database. From phpMyAdmin or cpanel, importing database of higher size involves bit complexity, however from terminal you can do it easily.
- Assume we will backup database my_database to a file my_backup_database.sql
- Executemysqldump -u root -p my_database > my_backup_database.sql
- Lets say we want to restore that database to system
- Executemysql -u root -p my_database < my_backup_database.sql
Importing and exporting database is so easy from terminal. This is frequently used for Drupal install.
How to Reset a Database (MySQL)
Say you have a database, and you have 100s of tables. You want to just delete all the tables, and keep database, user and password intact. Use below comamnd to reset the database.
mysql --user=YOUR_USER --password=YOUR_PASSWORD -BNe "show tables" YOUR_DATABASE | tr '\n' ',' | sed -e 's/,$//' | awk '{print "SET FOREIGN_KEY_CHECKS = 0;DROP TABLE IF EXISTS " $1 ";SET FOREIGN_KEY_CHECKS = 1;"}' | mysql --user=YOUR_USER --password=YOUR_PASSWORD YOUR_DATABASE