Accessing a database from CakePHP when behind a proxy server-Collection of common programming errors

I’m working on a web application that’s using CakePHP and MySQL. Depending on the circumstances, I may be using one of two computers and I may be behind a proxy server that won’t allow me to directly modify any code on my web server. Because of this, I’ve installed XAMPP on my machines to act as a local web server, and I keep my code in an SVN respository I can still access from behind the proxy.

With the web server, I have three places this code might be executed from (although it will ultimately only be on the web server), and since I’m in development, I’m constantly modifying the database. To make it easier on me, I want to set all three copies of this application to use the web server database so I don’t have to keep exporting and importing each time I modify the database.

When I try to access the database, the first error I get is “Unknown MySQL server host ‘mysql.mywebsite.com'”. I’m assuming this is because I have to use an http proxy for all internet access, and without it, it can’t even resolve an address. Can I configure CakePHP or PHP or my local Apache or whatever to use the proxy server in order to access my database?

  1. Neither Cake nor PHP will talk directly to MySQL. That’s handled by the low level mysql drivers. MySQL does provide their own proxy that you could try out.

  2. Right off the jump, you need to get your database under source control. I would recommend the CakeDC Migrations plugin.

    Getting this done will allow you to break the nightmarish cycle of phpMyAdmin importing/exporting (or, worse yet, MySQL CLI commands). If you take the time to do this now, it will save you a lot of time and grey hair in the future. Moreover, your site’s database calls won’t be subject to the latency of leaving your local machine, much less your local network.

    If you want to keep a single, simple app/config/database.php configuration, you can use a fully-qualified URL for your database’s hostname, and alter your development boxes’ hosts file (full path in Windows: C:\Windows\System32\drivers\etc\hosts) to resolve the fully-qualified URL to 127.0.0.1. Thus, when you’re on one of those boxes, Cake will end up connecting to your local database server, but will access the proper database server when you’ve deployed to your production web server.

  3. I tend to work either locally or on a hosted server. I find that using Netbeans I can work on the remote code without having to constantly upload and download.

    Whatever system you use, the fewer instances you have the better. Keeping things in sync can very rapidly become a major headache.

Originally posted 2013-11-09 23:37:42.