Question

I've being working on a local project recently, and have been synchronising it with an online server.

The problem is, now it involves database functionality. What this means is that whenever I make a database change locally, I have to access the cPanel of the website, enter PHPMyAdmin, delete the whole database and copy in the SQL query I exported from my local version.

Isn't there an easier way to work with MySQL databases, using the local-remote design logic?

Was it helpful?

Solution

This is a one of the more challenging aspects of development as environments change frequently when you have more than one developer and potentially multiple branches and/or servers. However, it's hard to say how it might effect you as we know nothing of the exact circumstances.

One element of your current operation might be to remove the need to login to various interfaces and use scripts to achieve what you need - less manual; more automation... At its most basic, you could create a PHP script that:

  • Backed up your production DB and saved it as a unique backup
  • Applies your changes (or, if needed, an entire re-build)
  • Allows you to revert to the backup if things go wrong

General options:

Updates not re-builds

There's no need to delete an entire DB and re-build in any environment unless all of the data changes (and it needs to). At its most basic, structural/schema changes are needed more than complete re-builds.

Replication

It's possible to synchronise MySQL DBs using master-master or master-slave replication, but sometimes this is overkill in some environments and may break functionality unless it's managed very carefully. If you made changes locally (in your development environment) they may break your remote site functionality...

Use framework tools

If you're using modern frameworks, many of them come with tools that facilitate DB updates and the ability to update (and potentially rollback), but often they aren't perfect as you might need to update specific data or tables that can't easily be rolled back.

My current setup

¬ multiple local developers
  ¬ multiple dev branches 
    ¬ development
      ¬ staging server 
          live

In our case, we use a custom-built script (and linked SQL files) that allow each developer to apply changes and promote changes to branches by running the following from the command-line:

php migrate.php server/branch update 

Or:

php migrate.php server/branch rollback

This could be simplified for a basic setup, but I can't see how without knowing more. By default, it would be worth asking this very question on dba.stackexchange.com too as you'll get exacting DB-focused responses.

OTHER TIPS

I would make a PHP page on the remote with a textbox that takes the ALTER statement, which you can just paste in and click. You'd want to protect that page with a forced https redirect if accessed by port 80, a password, and IP verification so only you can run it - all just a few lines of code.

Yes. But !

First you have to create a MySQL user on the remote server that is allowed to connect from your local PC, or if you are brave from any IP. eg think123@yourIp or think123@% WITH A VERY STRONG PASSWORD

If you cannot or are not allowed to do this then the answer is no. Some hosts allow this others do not. Look in your cPanel.

If they allow this, then you can gain access to the remote server from your phpMyAdmin that you run on your local machine. but you have to do some configuration changes to your local phpMyAdmin to inform it about the other possible connection.

In \wamp\alias\phpmyadmin.conf there should be a section something like this :-

$i = 0;

/*
 * First server
 */
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = false;

What you need to do is add another $cfg['Servers'] to tell phpMyAdmin is has another possible connection. Like this :-

$i = 0;

/*
 * First server
 */
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = false;

/*
 * Second Server
 */
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'REMOTE_SERVERS_IP';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = false;

When you run phpMyAdmin (local) it should add another field to the login screen, a dropdown, for selecting which connection you want to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top