Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Replicating a Remote MySQL Database to Local Environment / Server

Replicating a Remote MySQL Database to Local Environment / Server

2

If you deal with multiple server environments for your project or company, it can be difficult to make sure the environments are equally matched. Take, for example, the situation where you want to run some tests on a production database, but without affecting the production data itself. This is where replication can help.

The Process

The below bash script can dump a remote database (e.g. your production database), and replicate it to a local environment or server (or even a different remote server). It does this in the following order:

  1. Connect to the remote database and dump the structure and data (using mysqldump utility)
  2. Connect to the local database and drop the tables in a given database
  3. Run the SQL dump from Step 1 onto local database, and thus making a copy of the remote database

The Script

Configuration

The script can easily be configured using the variables at the top of the script. These should be self-explanatory, and comments haven been included in the script to give you some more information. However, if you need assistants, feel free to leave a comment below.

Running the Script

Assuming you’ve saved the script to file replicate_db.sh and added the appropriate permissions to execute the file (e.g. chmod 755 replicate_db.sh), you can run the script using:

./replicate_db.sh

The script doesn’t output anything if it runs successfully, but it will output errors if something goes wrong. Errors you may see include MySQL connection errors, user / password errors etc. If you can’t connect to the remote database:

  • make sure you have remote access to the database – the MySQL setup on remote host may only allow local connections
  • make sure the host / IP / port / username / password / database name are correct for the remote and local connections
  • make sure the firewall allows remote connections to the database / port

Limitations

If your particular use-case if more specific, the script may not be suitable for your needs. For example, this script assumes you are using the default MySQL port 3306. If this is not the case in your setup, you’ll need to modify the relevant parts of the script to alter the ports.

Backup, Bash, Command Line, MySQL

2 comments on “Replicating a Remote MySQL Database to Local Environment / Server”

  1. sunwei415 says:
    December 21, 2020 at 2:11 AM

    fixing the shell script: Line 27 should refer to REMOTE_PASS instead of DB_PASS

    Reply
    • Niraj Shah says:
      December 21, 2020 at 8:40 AM

      Thanks. Script has been updated.

      Reply

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy