backup & restore mysql and open it to outside access in ubuntu

Open mysql to outside access

MySQL by default listens only on the localhost (127.0.0.1) interface, which means it can only be accessed internally and is much more secure.
Since we have applications that access the database directly (i.e. not through locally hosted php, etc) we want to open it up for outside access.
The first thing is to bind mysql from 127.0.0.1 (local host interface) to 0.0.0.0

So open the my.cnf file in ubuntu, it could be in one of these locations:

/etc/my.cnf
/etc/mysql/my.cnf
$MYSQL_HOME/my.cnf
[datadir]/my.cnf
~/.my.cnf

On my machine it was at /etc/mysql/my.cnf

$ sudo nano /etc/mysql/my.cnf
Find the line that says
bind-address = 127.0.0.1
or the commented version
#bind-address = 127.0.0.1

Replace it by
bind-address = 0.0.0.0
save the file and restart mysql
$ sudo service mysql restart

That should allow outside connections to the database.

I also want to enable access from outside the office, so I make sure to open ports 3306 UDP and TCP and point them to the ubuntu server.
That way I can access the database from outside the local network.

Backup

mysqldump -u [username] -p [database-name] > [filename]
It will then ask you for the password.

Or you can create the file in something like phpmyadmin if you have it installed.

Restore

If the .sql file already has the sql stateents for creating and using the database, you can just:
mysql -u root -p < changehistory.sql Otheriwse you will have to create the database first and then specify it in the command line: Login to mysql and create your database $ mysql -u [username] -p Type in password > CREATE DATABASE testdb;
> exit

$ mysql -u root -p testdb < changehistory.sql

Leave a Reply

Your email address will not be published. Required fields are marked *