I even use it for simple tasks just so I can see what SQL it generated and it even has a handy "Create PHP Code" button to give me a baseline to start hacking the query to suit my situation.
This is great for local setups, but sometimes I need to do a query a production database where PhpMyAdmin is not installed and its alot easier to inspect the data of big tables with a gui (at least for me anyway).
Another helpful tip that I picked up from my new work place (thanks Rich) is to make PhpMyAdmin connect to a remote database via an SSH tunnel.
It requires a small config change to PhpMyAdmin, but it works a charm!
To differentiate between a tunnelled database and the local one, you need to configure your web server to serve up PhpMyAdmin from an additional URL, such as http://localhost/phpremoteadmin in addition to the standard http://localhost/phpmyadmin.
I added a second alias for PhpMyAdmin in /etc/apache2/conf.d/phpmyadmin.conf to look like this:
Alias /phpmyadmin /usr/share/phpmyadmin
Alias /phpremoteadmin /usr/share/phpmyadmin
Then, I modified my /etc/phpmyadmin/config.inc.php to include this:
// If phpremoteadmin is requested, use an ssh tunnel on 6666
// Note: This doesn't create the tunnel, use ~/bin/mysql_remote
if (substr_count($_SERVER["REQUEST_URI"], "phpremoteadmin") > 0) {
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['port'] = '6666';
$cfg['Servers'][$i]['host'] = '127.0.0.1';
}
Basically that means if the request is for /phpremoteadmin, look for the database on port 6666 instead of the preconfigured database port.
You'l have to reload apache for these changes to take effect:
sudo /etc/init.d/apache2 reload
Then you need a SSH tunnel from your local port 6666 to the servers port 3306 (where mysql is normally listening). To create this tunnel, you can use:
ssh -L 6666:localhost:3306 $USER@$HOST
But, to make it easier, I use (a slight variation) of this script:
#!/bin/sh
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo "Usage: $0 host1 [host2]"
exit 2
fi
USER="steve"
HOST=$1
DBHOST="localhost"
[ -z "$2" ] || DBHOST="$2"
echo "Browse to: http://localhost/phpremoteadmin"
ssh -L 6666:$DBHOST:3306 $USER@$HOST
The script also allows you to pass in an optional argument of the host where the database is.
By default, that is localhost (the remote server's localhost).
This is useful if you can only ssh to a gateway server and the database is on another server within that remote network that you wouldn't normally be able to ssh directly to.
This has been really helpful, so I wanted to share :)
6 comments:
Hi Steven,
Little help required.
Till the step
sudo /etc/init.d/apache2 reload
I am done and on checking localhost/phpremoteadmin
gives a login page like the normal localhost/phpmyadmin and upon logging it goes to show my local db. (Is it OK? Should it come like that?)
And the next step of establishing SSH where should i put it ?
Thanks
Unni
Hi Unni,
If you log in and see your local database, I'm guessing you haven't been able to configure phpmyadmin to connect to the database on the new port.
By default, MySql listens on 3306, which is what PhpMyAdmin is configured to connect to.
By modifying PhpMyAdmin's config, you are telling it to connect to port 6666 instead (if it is accessed from a URI that includes "phpremoteadmin")
By default, you probably dont have anything listening on port 6666, that's where the tunnel comes in.
You need to tunnel the remote server's 3306 port to your local 6666.
The command I provided does this, which you have to run in order for PhpMyAdmin to connect to it.
The script at the end of the post is just a convenient wrapper for that command. I saved that script in ~/bin/mysql_remote so it is accessible from anywhere.
That means whenever I want to connect to a remote host, I just run that script and provide the hostname as a parameter, which makes the remote MySql available on my local port 6666.
It is important to note, that this won't work unless you:
1) Configure PhpMyAdmin to connect to a different port
2) Open a tunnel so that you can connect to the remote MySql server via that different port.
Hope that helps
Hi Steve,
> I configured Phpmyadmin to connect to 6666 as in the instructions. Below ...
/* Advance to next server for rest of config */
$i++;
if (substr_count($_SERVER["REQUEST_URI"], "phpremoteadmin") > 0) {
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['port'] = '6666';
$cfg['Servers'][$i]['host'] = '127.0.0.1';
}
> Then after this i made SSH tunnel to server on which there is mysql running
ssh -L 6666:localhost:3306 mobshare@124.15.102.53
> Then i opened localhost/phpremoteadmin
> It shows the normal PhpMyadmin page with login box.
So.. did it get connected to the remote one or my local?
Should I be logging in that with remote server Mysql user and pass ?
Did i do everything fine?
Thanks
Yes, you should be logging in with the remote MySql server's username and password.
Did you try this? Only you can tell me if it worked or not :/
1> Added alias at this location as i couldn't find the file you specified.
sudo vim /etc/phpmyadmin/apache.conf
Alias /phpmyadmin /usr/share/phpmyadmin
Alias /phpremoteadmin /usr/share/phpmyadmin
Options FollowSymLinks
DirectoryIndex index.php
> And configuration for 6666 at
sudo vim /etc/phpmyadmin/config.inc.php
I tried logging in with the remote server MySQL username and pass in the phpmyadmin interface which i got when i gave
localhost/phpremoteadmin
But not working :( .I tried same user pass in the window in which SSH tunnel was done. Its working fine. Any clue where to look for error ?
It sounds like PhpMyAdmin is still connecting to your local database (using the original port 3306).
I'm afraid I can't help you anymore than this, but some ideas to help you find the cause of your problem could be:
1. Once you have an SSH tunnel established, you could check that the tunnel is actually going to your remote MySql with `telnet localhost 6666`. If that connects, your tunnel is ok.
2. If you tunnel is ok, you try to connect the normal mysql client to it through the tunnel to make sure it is working with something like 'mysql -uuser -ppass -P6666' (you'll have to check that syntax!)
3. Remove the 'if' condition in PhpMyAdmin's config and just hard code it to 6666 to make sure it is working.
Or perhaps a random port that is not is use (such as 9999), this should stop you from being able to login at all, if it doesn't cause an error, that means you are not modifying the right config file.
You might need to get another programmer to look at your setup in person, I don't think I can really offer any more help than than without seeing it in person and debugging/experimenting.
Good luck!
Post a Comment