I need to connect to a remote mysql database in PHP.
Specifically I have this as a constant:
define("DATABASE_SERVER", "localhost");
...
$db = #mysql_connect(DB_SERVER, DB_USER, DB_PASS);
I want to copy the site to my local machine but still connect to the main database.
Can I do this? if so, how? All I have is FTP access.
You need the IP/URL of the DB and put that instead of localhost.
You need to make sure the DB configuration allows remote connections/outside the local network.
Once you get the IP of the DB (should be given by the support team, if it is being supported), you will know.
If all you need to do is copy the data and build it locally on your machine, so you don't destroy live data, use the export tab in PHPmyadmin. I assume you don't have too much data.
PHPmyadmin->select the DB (from the left frame)->press the export TAB (top, right frame).
Should be clear from there.
You can export it as SQL and run this SQL on your local machine.
I would guess that your database server is refusing remote connections. If you have cPanel there's a section called something like "Remote Database Access" where you can enable them to certain IP ranges.
I suggest that you export the database in question, and import it locally. This way, you won't destroy live data if you mess up.
Many, if not most, hosting providers also provide a DB interface like phpMyAdmin. You can export the database from there.
+1 gnud. You shouldn't use the production database for development work, even if you could configure it to listen for external access on a public internet interface(*). One slip of the mouse and you're scribbling over important live data. Instead you should be working on a standalone development server of your own, from which you can push the code changes to the live server when you they're working and tested.
(*: Which is in itself a very bad idea: if the passwords are too weak or you're running an old version of MySQL with exploits available you are going to get hacked by automated probes very quickly.)
You'd normally export the database from the shell using the mysqldump dbname > dbdump.sql command, and import it on your local server by piping it into mysql dbname < dbdump.sql. There might also be a MySQL admin web interface installed on the server you could use. But if all you've got is FTP, you haven't really been given the tools you need to do development work. See if you can get an empty or dummy database to test the site with.
(What's more, FTP is a nasty business... really in this century you should be using SFTP.)
The simplest approach is to have DATABASE_SERVER be defined as database (or something equally meaningful) and put an entry in your hosts file. On your local machine this can point to the remote address, on the server this will point to 127.0.0.1.
define('DATABASE_SERVER', database);
On windows your sites file is:
c:\windows\system32\drivers\etc\hosts
with:
database <remote_ip>
Linux:
/etc/hosts
with:
database 127.0.0.1
Side note: is your remote DB a production DB? If so you shouldn't really be using the remote DB for development work, unless you're using a separate dev DB - even then I wouldn't run the risk of rogue dev queries eating up the remote DB resources.
Update: Doh, missed you're limited to FTP access.
Related
I have a software application that is designed to save data to a remote sever, by using the following URL:
http://130.228.263.2/projectName.php
The application itself creates tables, stores data, etc... so I don't have to worry about that.
I have set up MySQL Server on my Mac as well as MySQLworkbench to see the database visually.
My question is: Should I simply replace the above URL with the following?
127.0.0.1.3306
to have the application data written to my Mac's database instead of the remote one, or are there any other steps (or different format for local host, etc...) that I should be taking as well?
On the Mac Apache + PHP are installed by default, so it's not necessary to install anything. Just copy the php application to your local machine, adjust the db settings as MasterOdin wrote and you should be fine. Of course the php app must be accessible by your local Apache (you have to switch it on, via Internet Sharing in the system settings panel). In order to have a good starting point I recommend that you use MySQL Workbench to migrate your remote db to the local machine (see menu Database -> Migration Wizard).
Unless the server can connect/view your local system (which is unlikely off the bat and would require more configuration), you would need to locally host the php file locally using MAMP, then open projectName.php and edit the MYSQL connection details within that file to point to 127.0.0.1.3306 (which should probably be 127.0.0.1:3306 as that last number is the port information?).
This would then write to your local DB setup.
In our team, I used to be the only developer, coding directly on our remote dev server but we're hiring more developers so we have a need to move the codebase to our local machines so we don't encounter any file collisions.
However, even though I want to use a local codebase, I don't want to use a local database. Syncing the database seems like a hassle and is to be honest not really needed. So we're sticking to multiple codebases, but one database.
How can I run Wordpress on my local machine, while still using our remote server database? How do I set this up in MAMP?
I'm kind of new in setting up servers/ports/databases etc so it's not my strong suite. The problem which I don't know how to get around is the urls.
For instance, our WP sites have the url format:
site1.portal.dev
site2.portal.dev
When I type these URLs I want the codebase to be local but the database to be our remote dev server.
Update the config to point to the remote database. Job done.
As long as the configuration of that database allows you to connect to it, it's very simple.
An example of updating the wordpress config
I work in a multi-developer team. I've found that the best option is to have a full local environment.
We use wp-migrate-db pro for copying the dev database down locally. Once configured (per site) it takes a minute to grab the latest copy from dev.
Having this separation allows your team to make changes for their local development efforts (such as entering bad data, test pages, posts etc) without affecting other developer efforts.
This approach also works well for working with developers of various skill levels...and helps to reduce bigger issues that you may come across later on.
Using your site1.portal.dev should work, as the site_url and home_url. When you click on a link in your dev portal, it will continue to use site1.portal.dev, as WordPress thinks your site resides there. The database just holds the information, it doesn't matter where it resides.
You can connect to your remote mysql instance by providing the correct host, username, password. Usually it would be localhost, but in your case, you will want whatever the IP/DNS address for the machine that is hosting your mysql server is.
You can test the mysql connection with
mysql <database> -h <host> -u <username> -p
add -P <port> if your mysql server is listening on a different port.
I know this is an old question, I just post the answer I found that works for me in case anyone is looking for the solution of "How can I run Wordpress on my local machine, while still using our remote server database"
Install WordPress on your server (http://yourdomain.com)
Configure it with your database
Make sure whitelisting your local machine IP in "Remote MySQL" within your server's hosting panel
Pull down the entire WordPress directory onto your local machine
into a project folder
Add project folder to MAMP or your local web server app of choice
(make a note of your Server Name)
Open your LOCAL copy of wp-config.php Change 'DB_HOST' to
'yourdomain.com'
And add the following just above 'require_once(ABSPATH .
'wp-settings.php'); in wp-config.php file
define('WP_CACHE', true); $currenthost = $_SERVER['HTTP_HOST']; $mypos = strpos($currenthost, 'localhost'); if ($mypos === false) { define('WP_HOME','http://yourdomain.com'); define('WP_SITEURL','http://yourdomain.com'); } else { define('WP_HOME','http://localhost'); define('WP_SITEURL','http://localhost'); }
Replace each instance of 'localhost' with your 'Server Name' in MAMP. (localhost is default)
Source URL: https://coderwall.com/p/ck8v4a/remote-database-with-local-wordpress-instance
I have a simple MySQL database (one table with 12 rows of data and 5 columns) sitting on the web-server of my host provider.
On my home PC I create the data programmatically and store it in a free version of SQL Server (on my home PC). I would like to "upload" this data to the MySQL db in real time (or as close as I can get) over the internet (I'm assuming this is the only way to connect the pipes).
I know that opening up a MySQL database to a remote internet connection probably is not a "secure" thing to do, but the resulting data table will be publicly available anyway via an "app" so I'm not too worried about that (I suppose a hacker could "overwrite" my data with their own if they were both industrious and inclined) but I think the risk/reward is so small its not a major concern.
Anyway, what is the easiest way to do this with some semblance of security? I only know how to program in VB (I did a little HTML and ASP back in the day, but that was a long time ago). I could learn a few lines of code in another language if need be.
I do not have a static IP, and I've never actually interacted with a MySQL database before (only SQL server, so my MySQL knowledge/ familiarity is zero...but a db is a db, so how hard can it be?). Because of my home network firewall, I can't allow connections "in". I will have to make the connection to the MySQL db "out" from my home PC --> to the hosted database.
Ok this problem is not actually super simple.
What you will find is most shared hosting providers do not allow just any IP to access their databases.
Solution? set the IP for your computer of course! BUT.....you are probably on home internet connection so your IP address can CHANGE (if you have a static IP you are a lucky person!)
So the best way - create a mini-API!
Basically, you want to post your data to a script (with some security of course) that then inserts this data into the database.
It is a lot of work but having done all this before it seems to be the only way unless you have a dedicated server / advanced access privileges!
You could take a look at WAMP for your home pc. It's simple to use.
And then you should take a look at Mysql remote connections(some details here)
I would try this:
At your local computer install MySQL server, there's a free community
edition available for download, try the web installer since its more lightweight. Use the
custom installation and make sure MySql Workbench is selected too.
Workbench has a migration tool for the most common databases, Try this locally, so you can tell if all your data is correctly migrated from your local SQL Server to a MySQL db and there are no data losses in the process.
Then, You could probably connect through Workbench to your online MySQL db
and migrate your data to it directly from your just created local db. In case you cannot connect, make a backup of your local db and send the files to
your server by ftp or similar process. Then, simply restore DB from the backup file on your
online server.
Hope this helps!
I've been developing PHP pages for several weeks now and am getting much more confident in my code. I've obtained a hosting account through Bluehost, and am ready to begin making some live pages. Previously I have been using XAMPP on Windows and developing all of my pages on my local computer. I'm trying to determine the best practice for creating my page locally and then easily moving it to my hosting server. I use Filezilla to transfer my files. Here are my main questions:
1.) How can I ensure my local and live MySQL databases stay in sync? I have been manually creating the database locally, and then taking the same code and applying it to my live server. (I have phpmyadmin locally and on the live server, but dont know how to use it to streamline this process)
2.) I have to change the mysql password and connection credentials on all files before moving them from local server to live server. Is there a way around this?
For question 1 you could dump the database and import it at the production end each time you do an upload, or you could use mysql replication.
For question 2 it is quite common to have the database connection details in a separate php file and then include that file at the top of each page that needs to connect to the database
Then you can just change that one file, or only upload the one with the production database details.
db.php
<?php
$dbuser="";
$dbpass="";
$dbname="";
$dbserver="";
?>
myfile.php
<?php
include("db.php");
$conn = mysql_connect($dbserver, $dbuser, $dbpass);
mysql_select_db($dbname);
//....
?>
You could point your mysql_connect() host to your new server's IP address. Therefore, no matter where your code is, it will connect to the socket.
$link = mysql_connect('<your ip here>', 'mysql_user', 'mysql_password');
I always make my projects so that I have to change 1 line of code to fluctuate between dev and production.
I have a base file where i specify my environment like this:
$env = 'dev'
Then in my database setup variables i have something like this:
if($env == 'dev') {
specify dev variables
} else if($env == 'prod') {
specify prod variables
}
The beauty of this is that when i want to move between dev and prod, i only have to change 1 variable.
Use a version control system such as svn or git; look at phpmig for database management, look at deployment tools such as capistrano, make sure you test on a replica of the production server (even if you just use a virtual host)
I'm pretty much in the same position as you and here is how I plan to do it.
I don't plan to sync my local database with the live one but just move the local database once to the online one when I'm done. The way I do that is just, in PHPMyAdmin select the database I want and export it as a .sql file. Then on the online server I simply import that file.
The best way I know is to have a separate file where you have a function which looks something like this:
function connectToDB(){
$mysqli = new mysqli("localhost", "root", "123", "myDatabase");
return $mysqli;
}
Then when you want to use that you just import the file in the top of your other PHP files and use it like this:
$mysqli = connectToDB();
This is my way of doing it. If someone knows a better way, I would be happy know it.
Deploying from development to production is not a trivial task and can be very complicated. Companies invest a lot of resources in order to make deployment a logical and stable process. I recommend to break your question into pieces and ask more specific.
As response to your questions, what I'd do:
1) When deployment requires a change in the database structure, write those changes in sql scripts. When you deploy you'll need to run those scripts and update the code (as you can figure out, the time in between will make the application unstable or broken). Before anything, take a copy of the production database onto development and try the scripts first on development. You will get so databases in sync each time you deploy.
2) How you save passwords depends on whether you are using frameworks or not. Following frameworks, it's a good approach to define an environment variable and set passwords according to the environment (dev or production).
As regards to code deployment, even if you are a unique developer, I'd use svn or git to save your projects under versions control. It will make code updates much more simple and help you to roll back if desired provided you create tags for each deployment.
Best practices are to write database migration scripts, version them. Then each time you upload to production, you store the values of these versions in the database. Only run the versions that you have not run before.
Have different local config files outside of your repository. This allows you to maintain the necessary application config in the repo with a main config file, then the local config file overrides database and environment settings.
I have a fairly small MySQL database (a Textpattern install) on a server that I do not have SSH access to (I have FTP access only). I need to regularly download the live database to my local dev server on demand; i.e., I would like to either run a script and/or have a cron job running. What are some good ways of doing this?
Some points to note:
Live server is running Linux, Apache 2.2, PHP 5.2 and MySQL 4.1
Local server is running the same (so using PHP is an option), but the OS is Windows
Local server has Ruby on it (so using Ruby is a valid option)
The live MySQL db can accept remote connections from different IPs
I cannot enable replication on the remote server
Update: I've accepted BlaM's answer; it is beautifully simple. Can't believe I didn't think of that. There was one problem, though: I wanted to automate the process, but the proposed solution prompts the user for a password. Here is a slightly modified version of the mysqldump command that passes in the password:
mysqldump -u USER --password=MYPASSWORD DATABASE_TO_DUMP -h HOST > backup.sql
Since you can access your database remotely, you can use mysqldump from your windows machine to fetch the remote database. From commandline:
cd "into mysql directory"
mysqldump -u USERNAME -p -h YOUR_HOST_IP DATABASE_TO_MIRROR >c:\backup\database.sql
The program will ask you for the database password and then generate a file c:\backup\database.sql that you can run on your windows machine to insert the data.
With a small database that should be fairly fast.
Here's what I use. This dumps the database from the live server while uploads it to the local server.
mysqldump -hlive_server_addresss -ulive_server_user -plive_server_password --opt --compress live_server_db | mysql -ulocal_server_user -plocal_server_password local_server_db
You can run this from a bat file. You can ever use a scheduled task.
Is MySQL replication an option? You could even turn it on and off if you didn't want it constantly replicating.
This was a good article on replication.
I would create a (Ruby) script to do a SELECT * FROM ... on all the databases on the server and then do a DROP DATABASE ... followed by a series of new INSERTs on the local copy. You can do a SHOW DATABASES query to list the databases dynamically. Now, this assumes that the table structure doesn't change, but if you want to support table changes also you could add a SHOW CREATE TABLE ... query and a corresponding CREATE TABLE statement for each table in each database. To get a list of all the tables in a database you do a SHOW TABLES query.
Once you have the script you can set it up as a scheduled job to run as often as you need.
#Mark Biek
Is MySQL replication an option? You could even turn it on and off if you didn't want it constantly replicating.
Thanks for the suggestion, but I cannot enable replication on the server. It is a shared server with very little room for maneuver. I've updated the question to note this.
Depending on how often you need to copy down live data and how quickly you need to do it, installing phpMyAdmin on both machines might be an option. You can export and import DBs, but you'd have to do it manually. If it's a small DB (and it sounds like it is), and you don't need live data copied over too often, it might work well for what you need.