jueves, 23 de marzo de 2017

Create a MySQL Database


First we’ll login to the MySQL server from the command line with the following command:
mysql -u root -p
In this case, I’ve specified the user root with the -u flag, and then used the -p flag so MySQL prompts for a password. Enter your current password to complete the login.
If you need to change your root (or any other) password in the database, then follow this tutorial on changing a password for MySQL via the command line.
You should now be at a MySQL prompt that looks very similar to this:
mysql>
To create a database with the name tutorial_database type the following command:
CREATE DATABASE tutorial_database;
If a database of the same name already exists, then a new database will not be created and you’ll receive this error:
ERROR 1007 (HY000): Can't create database 'tutorial_database'; database exists
To avoid seeing this error use the following command instead:
CREATE DATABASE IF NOT EXISTS tutorial_database;
The above command will only create the database tutorial_database if a database of that name does not already exist.
Database Management Made Easy

View All MySQL Databases

To view the database you’ve created simply issue the following command:
SHOW DATABASES;
Your result should be similar to this:
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| tutorial_database  |
+--------------------+
4 rows in set (0.00 sec)

How to Back Up MySQL Databases From The Command Line


While automated backups are important, sometimes you just want to take a quick and dirty snapshot before making a change to your data. When changing files in Linux, you can simply cp a file to another name, and cp it back if your change does not workout. With MySQL, it is not quite that simple, but it is by no means difficult.

Creating A Backup

The mysqldump command is used to create textfile “dumps” of databases managed by MySQL. These dumps are just files with all the SQL commands needed to recreate the database from scratch. The process is quick and easy.
If you want to back up a single database, you merely create the dump and send the output into a file, like so:
mysqldump database_name > database_name.sql
Multiple databases can be backed up at the same time:
mysqldump --databases database_one database_two > two_databases.sql
In the code above, database_one is the name of the first database to be backed up, and database_two is the name of the second.
It is also simple to back up all of the databases on a server:
mysqldump --all-databases > all_databases.sql
Database Management Made Easy

Restoring a Backup

Since the dump files are just SQL commands, you can restore the database backup by telling mysql to run the commands in it and put the data into the proper database.
mysql database_name < database_name.sql
In the code above, database_name is the name of the database you want to restore, and database_name.sql is the name of the backup file to be restored..
If you are trying to restore a single database from dump of all the databases, you have to let mysql know like this:
mysql --one-database database_name < all_databases.sql

Restoring Databases From cPanel Backups

There is no special wisdom to pass on to you regarding restoring databases from cPanel backups. If you do not wish to use cPanel's automatic methods of restoring entire accounts, you can open up the individual backup on the command line, find the mysql dump file you wish to restore, and use the above commands to perform the restore.
As always, if you have any questions about how to use this tutorial on your Liquid Web server, do contact our Heroic Support.