How does PHP communicate with MySQL on the same server - php

Does anyone know how php requests data from mysql?
If I have mysql in the same machine as php, does it open a tcp connection to the localhost on port 3306 or does it have some other way of getting the data?
Is it the same in linux and windows?
Thanks

if available it uses a unix socket, otherwise localhost.
Note that even if you specify localhost in the connection string it will try to use the faster "unix socket" if available

Usually PHP opens up a local pipe found at /tmp/mysql.sock to connect to a local version of the server, unless you use an IP address in your connection string.

PHP opens a connection to port 3306 is the server via TCP to allow data communication. Hence, you can specify which port to connect to in mysql(i)_connect etc, and why, you need to have firewall rules for mysql.
It is the same in Windows as Linux
So yes, TCP :)
EDIT: Revision, In linux, php looks to connect to mysql via /tmp/mysql.sock the tmp directory needs to have correct permissions.

Related

Connection Firebird DB via SSH

I am creating an application with Laravel and Firebird 2.5 and wants to run it on a DigitalOcean server structure. We have 2 servers for the application, one for the web services (lets call it www) and one for the database services. I did a successful setup with Mysql and it works well but we all know Firebird is a rough one. So this is what we did so far:
Install a LEMP stack in www and Firebird 2.5 SuperServer. As I said these are digital ocean servers, both runs Ubuntu 14.04.
We created an SSH tunnel between the two server with the following structure:
ssh -L 9500:127.0.0.1:3050 username#db_server_ip_address (Private address)
But when we tried to hook up the application on www with the database server got the following error:
unavailable database
In the Laravel Configuration file used the following set up:
DB_HOST=127.0.0.1
DB_DATABASE=/home/username/database.gdb
DB_USERNAME=username
DB_PASSWORD=password
The credentials are correct, we can use it on the remote (db) server.
What do you think what could be the problem? Is it the SSH tunneling?
Warning: I don't use SSH that much, and have never used SSH tunneling, my answer is based on looking at documentation.
The ssh -L 9500:127.0.0.1:3050 does not do what you think it does:
Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side. This works by allocating a socket to listen to either a TCP port on the local side, optionally bound to the specified bind_address, or to a Unix socket. Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel, and a connection is made to either host port hostport, or the Unix socket remote_socket, from the remote machine.
(from ssh(1))
In other words, as far as I can tell the proper command would be:
ssh -L 9500:<ip-address of the Firebird server>:3050
The second problem seems to be that your Laravel config does not specify a port, so it is likely still trying to connect to port 3050 (the Firebird default port), instead of port 9500 that you configure. I don't know Laravel, but a property DB_PORT=9500 seems logical (but maybe these properties are specific to your own deployment, in which case you may need to do some more work).

mysql_connect() failed - Error (4)

I developed a PHP application with MySQL and node.js implementation.
I am trying to connect to my production DB like the following:
mysql_connect('54.212.117.38', 'root', '********');
This is the Error I get :
Could not connect: Can't connect to MySQL server on '54.212.117.38' (4)
I must mention that I can reach the PHPmyAdmin directly through the browser.
Moreover, On local host the application is connecting to MySQL with no problem.
I also tried restarting the Apache server, shutting down the Node.js procedure...
BTW - My production server is running Ubuntu.
Does anyone has an idea on what's the reason that it doesn't work?
There is more than one possibility. Some advice:
Make sure the mysql server behind this IP address is running and accepting connects.
Make sure the user can access from your clients ip.
Check out if perhaps the connection process takes too long (in this case, you can try increasing the connect_timeout).
If nothing else helps, trace the traffic between client and server. Maybe a firewall is slightly too aggressive (I've had that once, user root was triggering an IDS rule...).
A sidenote, the mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
the problem is the firewall rules.
When you connect via Phpmyadmin, you are connecting through the web interface and internally Phpmyadmin is connecting to localhost.
Check your local ip or the ip of the server that you want to connect from.
Make sure you change the parameter bind-address to:
bind-address = 0.0.0.0 = 0.0.0.0

Phpcloud and Mysql Workbench connection

I have been struggling how to connect Mysql Workbench with Phpcloud.com Mysql instance since friday ? Below there is error which I get image
Anyone has did it working properly ?
The error message about the initial package usually indicates there was no server found at that address and port. Keep in mind that due to the SSH tunnel the remote server appears like it is a local one. The remote end of the tunnel sees the server locally too, since it is on the same machine as the MySQL server (I assume). So what you need is the localhost address for the MySQL server. The other thing to check are the ports: 1) Does the SSH connection really work with port 22? 2) Is your firewall blocking this port? 3) Is the remote MySQL server really listening on port 3306?
As a side node: on Windows you can usually just press Ctrl+C in error messages to copy the message to the clipboard.
Please look at the second section "Managing your Database Instance" at http://www.phpcloud.com/help/accessing-the-db
You need to establish a tunnel using SSH client (putty will be best in your case) and then access the server on port 13306 (local).
I don't use Workbench but I assume it tries to establish a regular SSH connection and not a tunnel and this will not work.

Querying a MySQL database on a remote webserver through SSH

We have an internal MySQL database that our customer service department uses to do quotes, and an external MySQL database on our website that our customers use to do quotes. I have a PHP report that I've written that gives me the information from the internal database. Now I'd like to modify the report to include the data from the external database as well. I need to connect to the external database by using a SSH connection. I've been looking into using cURL or SSH2, but I'm not sure if either are the right way to do this. Has anyone out there queried a SSH MySQL database on a webserver before, or does anyone know what I'd need to do to make this work? Thanks for any help you can give!
I actually use a MySQL GUI client called "Sequel Pro", which does offer the option to connect to MySQL through a SSH connection. This is totally doable, though if you have the option to connect to the DB without it (e.g. you can connect to it from "outside" the host), you should prefer this.
the easiest way I conceive you could query your DB through SSH is to use SSH port-forwarding, so you'd spawn a process that would do ssh user#host-that-has-the-db -L3306:localhost:3306, and then you'd have your MySQL connector connect on localhost.
The -L argument to SSH instructs it to do local port forwarding, so SSH will listen on a local port and forward what it receives there directly to the specified remote host/port from the other end. obviously my example uses port 3306 on both ends, but that can be changed as appropriate (RTM to have more about this)
Yes you can query mysql from bash over SSH but this is not the best and scalable way to go. The best solution would be to create a REST Web Service on the production servers. Then your internal system will query that remote service with authentication.
You set up a tunnel from some available port on the local server to the mysql port on the remote server:
ssh -L 3307:localhost:3306 someuser#remoteserver
That creates a tunnel listening on port 3307 on the local system, which connects to port 3306 (MySQL) on the remoteserver.
Then point your PHP code to connect to port 3307 instead of 3306 , and it'll get tunneled to mysql on the remoteserver.

Connect to remote MySQL database with PHP using SSH

I have a remote database I'd like to connect to from PHP running locally. The database doesn't allow remote connections so ordinarily I SSH into the box and use it from the command line, but that's not really a long term solutions.
I have SSH access, I have MySQL access once I SSH in, but I don't know how to get PHP into that workflow. If I could make this work within MAMP, that would be great, too.
For developing or testing, you can use ssh command to setup tunnel first and then access the remote database as a local one. The steps are:
1) setup tunnel with ssh command. command format: ssh -L [local port]:127.0.0.1:[remote mysql port, by default, it is 3306] [user]#[remote mysql server ip]. sample:
ssh -L 3307:127.0.0.1:3306 ford#134.11.21.89
2) keep the command window alive
3) You can access the remote database by mysql string: mysqli://[user]:[password]#127.0.0.1:3307/[database name]
Connect to a MySQL server over SSH in PHP
You could set up a SSH tunnel and then point your php connection code to a local port which is forwarded through the tunnel. Under Windows you might use putty; for Mac there will be similar solutions.
If this is for development, the suggested solution by alex is the way to go; set up a ssh-tunnel.
The tunnel will redirect your 127.0.0.1:3306-requests to the remote machine. The remote machine will also belive the requests will come from 127.0.0.1 (locally).
However, you may encounter problems if your server (shared host? please specify) doesn't allow mysql-connections from 127.0.0.1 (quite commonly only localhost are allowed). There's a tiny difference in those, and it will inhibit your tunnel from reaching the remote mysqld.
Just google tunneling, set it up, and use 127.0.0.1 from your php-connection strings.
regards,
//t

Categories