MySQL installation on Ubuntu 18.04
Basic installation is an easy process and requires typing just a couple of commands
1 | $ sudo apt update |
Answer the questions provided and your basic installation is finished. Now you need to make sure your can access your database with username/password pair.
1 | $ sudo mysql |
This will take you in MySQL prompt where you need to set a password for the root
user with commands provided below
1 | mysql> SELECT user,authentication_string,plugin,host FROM mysql.user; |
NOTE: Make sure to replace password with your own secure password.
Exit a shell
1 | mysql> FLUSH PRIVILEGES; |
and check you are able to connect with a password created
1 | $ mysql -u root -p |
Let’s create a database so we can connect to it with a help of any client and use it within our applications
1 | mysql> CREATE DATABASE [database-name]; |
Backup and restore your database
Now suppose that you have to migrate between instances and create a database dump which can be transfered as a file and then restored on a new machine. mysqldump will help as to accomplish this.
1 | $ which mysqldump |
NOTE: -u
is an argument for your username and -p
is for password (there is no spaces between keys and their values)
Now from our target machine we can copy a remote dump file and restore it within current MySQL installation
1 | $ scp -r [remote-user]@[remote-ip]:[remote-path] . |
You need to provide username and remote ip address as well as a path on remote machine to the backup file. .
at the end of a command means copy to current directory. Resulting command might look like this scp -r ubuntu@54.154.11.44:/home/ubuntu/db_22_01_19.dump .
Having a dump locally allows us to restore database from a file.
1 | $ mysql -u root -p [database-name] < db_22_01_19.dump |
To make sure restore procedure has been completed successfully you might want to open MySQL shell once again and check content for our database is in place
1 | mysql> show databases; |
That’s it. Be vigilant and don’t forget to periodically create backups of your database not to loose important data.