How to connect to a remote DB from my computer with PHP - php

I am quite new in PHP and databases. I am trying to connect to a remote Postgres database from my computer. In the past I have done this by using a localhost and a MySQL database but it is the first time that I try to connect in another host.
This is the PHP code that I have:
<?php
$conn_string = "host=hostname.com port=xxxx dbname=dbname user=root password=root";
$dbconn = pg_connect($conn_string);
?>
I read several articles in internet saying that it is enough to change the hostname from localhost to the one provided but it doesn't seem to work. I also have deactivated my firewall in case there is some problem with the communication but with no success.
As I said I am quite new with this and in the past I had used only test servers (localhost). Back then I was using software like Wamp and Mamp and I was placing the script files in the localhost.
So, my questions are:
1) where should I save the test.php file which has the above lines of code?
2) Would this solve the problem or I am missing some important concept here?
I hope what I am trying to say is clear. Please let me know if I have to be more specific in something.
Thanks
Dimtiris

First, error messages would really help. Based on the comments I will say I suspect that you are connecting to the right server. However you are using a username of "root?" It would be highly unusual for such a username to be present on Postgres.
The first one is where to put the file. This should be put in your web server's document root or cgi bin depending on how you have things configured (probably the former).
For your second question, please double check usernames and passwords, and make sure you can use those usernames and passwords via pgAdmin III to connect to the same db. Making sure you have the credentials right to start will make your code a lot easier to troubleshoot if things don't go right.

Related

PHP Socket PDO SQLSTATE[HY000] [2002] No such file or directory

Introduction
For the past couple of days I’ve been messing about with PHP web sockets, in an attempt to retrieve some information in real-time for the user about the various different people connected.
I’m on Ubuntu, running the latest version of XAMPP (located under /opt/lampp/), the web socket I run using PHP which I’ve installed using apt-get install php. This opens a socket with no problem whatsoever.
The problem I’m facing has to do with establishing a connection to the database from the web socket with a little help from PDO. Whenever I attempt to establish the connection to the database located on localhost, I get the following error message:
SQLSTATE[HY000] [2002] No such file or directory
Since PHP and my MySQL server isn’t aware of one another, considering MySQL is run through the instance of XAMPP and the server is run using the version of PHP (which is the same as the one as for XAMPP) I’ve installed using apt-get, one might think that this is the cause of problems. But shouldn’t the server be reachable as long as you have the correct username and password to the server configuration?
Research
I’ve been doing quite some research on the subject, and have found multiple potential solutions for the problem, but neither one appears to do the job. For example, on this question, it is suggested that you can solve this error by doing some witchery to your server software, as well as PHP—I’ve tried this with to apparent success.
On other questions, it’s instead suggested that you change this DSN PDO setup:
$dsn = "mysql:host=$host;port=$port;dbname=$name;charset=$charset";
To instead of using "localhost" for the host, you stick with 127.0.0.1—as a matter of fact, I can’t even connect unto my server if I were to do this. Doing this, my XAMPP gets stuck in some form of endless loop. A minute or so after changing from 127.0.0.1, back to localhost, it starts working again.
I can’t remember where, but I also remember seeing somebody saying it’s not at all doable (connecting to MySQL from a PHP socket), which I’d find extremely strange.
Some people also seem to suggest switching to Node.js for handling real-time connections, but I’d very much like to keep it all PHP if possible.
Furthermore
I’m probably just being a complete moron (I was ;-)), but if anybody can point me in the right direction it would be highly appreciated!
you need to specify the path of the socket
example:
$dsn = 'mysql:dbname=testdb;unix_socket=/path/to/socket';
Try to search the file mysql.sock inside the /opt/lamp

Does a PHP to MySQL connection need to be local?

Okay, I know this may be a very noobish question, so forgive me, but right now I have XAMPP and I'm running a local Apache server on my personal computer to test PHP code. I have setup a test database through phpmyadmin on a webhost (Hostgator), but it's looking like if I want to connect to that database I need to have the PHP file that I am editing on the same server because any of the tutorials I read tell me to use locoalhost for the servername requirement when using MySQLi or PDO.
Is this because you 'can't' connect to a database on a separate server? Or because it's just not common because there is a better way to do things? (I've seen hints on being able to download MySQL and phpmyadmin onto my PC, and then importing and exporting tables, but what I've seen hasn't been clear on if that's what I need to do for this or not.)
Thank you!
No, it's very common practice to use different machines for web and mysql. There could be a few issues (not familiar with Hostgator but I've dealt with similiar). One is likely firewall. Anything external to the Hostgator network will not have access.
if I want to connect to that database I need to have the PHP file that
I am editing on the same server because any of the tutorials I read
tell me to use locoalhost for the servername requirement when using
MySQLi or PDO.
I'd find better tutorials. It's good practice to separate your MySQL Server away from your web server.
It is possible to connect to a database server running on another machine provided that the server machine has the appropriate ports open in its firewall and that there is a route between both machines.
Problem solved! I had to go into the Remote MySQL and allow the IP address. Again, maybe a noobish thing, but I had no idea that was something you needed to do. Learned from this though! Thank you everyone!
You can do it, and I see that you figured it out.
However exposing your database to the public internet is never a good idea, so for security purposes it is turned off by default.
If the reason is just for testing your code then it is better to setup your development environment with a local mysql server, this way you don't mess with production database.

PHP show all connection

Really can't figure out how to word what I need...
I work with a site through SCP. And sometimes it crashes due to the database server fault. But actually I don't connect to that database. So I need to see all possible connections that my site can use. I found two files at include path that contain connection to server that faults, but my files don't include them. If I can give some more info please ask me.
All connections are made through odbc_connect("DSN_NAME;host_address", $login, $pass)
How did you find out that your SCP crashes because of database server fault? It shouldn't. I think the problem is not in database. By the way, MySQL allows to configure many connections, so I believe the problem is in something else.
$con=mysqli_connect("example.com","peter","abc123","my_db");

The right way? Automated Synchronization between 2 mysql DB

I already read a few threads here and I also went through the MySQL Replication Documentation (incl. Cluster Replication), but I think it's not what I really want and propably too complicated, too.
I got a local and a remote DB that might get both accessed/manipulated by 2 different persons at the same time. What I'd like to do is to sync them as soon as possible (means instantly or as soon the local machine goes online). Both DB's only get manipulated from my own PHP Scripts.
My approach is the following:
If local machine is online:
Let my PHP Script on the loal machine always send the SQL Query to the remote DB too
Let my PHP Script on the remote machine always store its queries and...
...let the local machine ask the remote DB every x minutes for new queries and apply them locally.
If local machine is offline:
Do step 2. also for both machines and send the stored queries to the remote DB as soon as
local machine goes online again. Also pull the queries from the remote machine for sure.
My questions are:
Did I just misunderstand Replication or am I right that my way would be easier in my case? Or is there any other good solution for what I'm trying to accomplish?
Any idea how I could check whether my local machine is online/offline? I guess I'd have to use JavaScript, but I don't know how. The browser/my script would always be running on the local machine.
What you're describing is master-master or multi-master replication. There are plenty of tutorials on how to set this up across the web. Definitely do your research before putting a solution like this into production as replication in MySQL isn't exactly elegant -- you need to know how to recover if (when?) something goes wrong.

PHP database connection

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.

Categories