Connect to external mysql server from a specific ip - php

I've been searching and couldn't find an answer to this.
I have a server that holds multiple ip addresses - lets say 1.1.1.1 (as the main ip) and 1.1.1.2
I need to connect to an external mysql server that allows only 1.1.1.2 to connect to it but it tries to connect from 1.1.1.1
is there any way to "choose" what ip the server will try to connect from?
thanks

1) try check my.cnf (for bind to multiple IPs)
bind-address = 0.0.0.0
#skip-networking
2) check server firewall rules (or try disable firewall to test connection).
More information:
http://www.cyberciti.biz/faq/unix-linux-mysqld-server-bind-to-more-than-one-ip-address/
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

Related

MySQL database IP address from rackspace or google cloud?

Ok here’s the deal. I setup MySQL database on Rackspace and I’m trying to connect to it. I am using the tutorial google maps to create store locator using MySQL. The line of the code in the tutorial asks for the host name and I’m giving the IP address of my server on Rackspace. I’m using what I think is the correct one but it’s not working. Any ideas?
Here's the google tutorial code:
// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
};
My code:
$connection=mysqli_connect ('THE IP ADDRESS OF MY RACKSPACE SERVER', 'MY USERNAME', 'MY PASSWORD');
Here's a link to the tutorial from google: https://developers.google.com/maps/solutions/store-locator/clothing-store-locator
I know I have the server IP correct and my username/password correct since I'm able to login via SSH from terminal with those credentials.
When I use mysql> \s to show the status it says Connection: Localhost via UNIX socket - does this mean it's local host? I need it hosted on IP to connect right?
I expect that your MySQL server is by default by your Linux distribution's packaging configured to listen either locally, or on the socket file only. You can update your DB config to listen on the public IP address of your server however obviously this can come with some security implications.
To do this edit the /etc/mysql/mysqld.conf file (this may be in a slightly different location depending on distribution being used) and the following line as such...
From
#bind-address = 127.0.0.1
To
bind-address = 0.0.0.0
Now restart your MySQL service using the systemctl or service command.
service mysql restart
Your MySQL server is now listening on ALL the host's IP addresses. If you want to limit it to just one you should enter that IP instead of 0.0.0.0. You should now be able to connect to your MySQL server remotely, however, you must have already configured your database user to be able to login from the webserver. If you haven't configured the user yet do something like this.
mysql
CREATE USER '<username>'#'<webserver ip address here>' INDENTIFIED BY '<password>';
GRANT ALL PRIVILEGES ON <database>.* TO '<username>'#'<webserver ip address>';
FLUSH PRIVILEGES;
You should now be able to login as this user and view / modify / insert etc... data to the database specified from the server IP address specified. You can test this from the web server using the MySQL client like this...
mysql -u <username> -h <db server ip> -p

Can't connect to mysql in PHP [duplicate]

I get this error when I try to connect to the mysql database using php mysqli class. Using following code:
$db = new MySQLi("localhost","kamil","*****");
password is * for security.
I have created user kamil with all privileges on external ip address and localhost. When I run: select user,host from mysql.user it properly displays those two users.
I did some research and used this benchmark: https://stackoverflow.com/a/2183134/1839439 to see what it connects to. As it turns out it is only able to connect to 127.0.0.1 and 127.0.0.1:3306 which is localhost, however when I supply localhost it throws out this error.
My question is why does it only allow me to connect to DB using localhost ip address and not the name or external ip. Do I need a different host if I want to be able to use mysql on website or if I can use 127.0.0.1?
hosts file
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 raspberrypi
Mysql user table results for this user:
| kamil | 109.255.177.28 |
| kamil | localhost |
When you use just "localhost" the MySQL client library tries to use a Unix domain socket for the connection instead of a TCP/IP connection. The error is telling you that the socket, called MySQL, cannot be used to make the connection, probably because it does not exist (error number 2).
From the MySQL Documentation:
On Unix, MySQL programs treat the host name localhost specially, in a
way that is likely different from what you expect compared to other
network-based programs. For connections to localhost, MySQL programs
attempt to connect to the local server by using a Unix socket file.
This occurs even if a --port or -P option is given to specify a port
number. To ensure that the client makes a TCP/IP connection to the
local server, use --host or -h to specify a host name value of
127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by
using the --protocol=TCP option.
There are a few ways to solve this problem.
You can just use TCP/IP instead of the Unix socket. You would do this by using 127.0.0.1 instead of localhost when you connect. The Unix socket might by faster and safer to use, though.
You can change the socket in php.ini: open the MySQL configuration file my.cnf to find where MySQL creates the socket, and set PHP's mysqli.default_socket to that path. On my system it's /var/run/mysqld/mysqld.sock.
Configure the socket directly in the PHP script when opening the connection. For example:
$db = new MySQLi('localhost', 'kamil', '***', '', 0,
'/var/run/mysqld/mysqld.sock')
If it's a PHP issue, you could simply alter the configuration file php.ini wherever it's located and update the settings for PORT/SOCKET-PATH etc to make it connect to the server.
In my case, I opened the file php.ini and did
mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
And it worked straight away. I have to admit, I took hint from the accepted answer by #Joni
If 'localhost' doesn't work but 127.0.0.1 does. Make sure your local hosts file points to the correct location. (/etc/hosts for linux/mac, C:\Windows\System32\drivers\etc\hosts for windows).
Also, make sure your user is allowed to connect to whatever database you're trying to select.
My question is why does it only allow me to connect to DB using localhost ip address and not the name or external ip.
As per docs for mysqli_connect():
Parameters: hostname: Can be either a host name or an IP address. The local host is assumed when passing the null value or the string "localhost" to this parameter. When possible, pipes will be used instead of the TCP/IP protocol. The TCP/IP protocol is used if a host name and port number are provided together e.g. localhost:3308.
So basically if you specify a localhost or null value, it enforces named pipes/socket connections (see: mysqli.default_socket), otherwise (e.g. when you specify a port or non-localhost host) it's going to use the TCP/IP protocol (e.g. localhost:3308).
Please check the following file
%SystemRoot%\system32\drivers\etc\host
The line which bind the host name with ip is probably missing a line which bind them togather
127.0.0.1 localhost
If the given line is missing. Add the line in the file
Could you also check your MySQL database's user table and tell us the host column value for the user which you are using. You should have user privilege for both the host "127.0.0.1" and "localhost" and use % as it is a wild char for generic host name.

PHP - Can't connect to a remote MySQL database

I have a MySQL database, hosted by me on a Windows server, and I would like to query it from a remote webserver. When I try to connect to the MySQL database using PHP (I know, I should be using mysqli):
$connection = #mysql_connect("203.0.113.0:3306", "username", "password");
With or without specifying the port, after a long time of loading, I get this error with an errorno of 2003:
Can't connect to MySQL server on '203.0.113.0' (4)
Here is what I get when I run netstat -n in command prompt on the server that is hosting the MySQL server: http://pastebin.com/pbRJNqCZ. It filled up the screen so I couldn't copy everything, but I know that everything else was (I saw a couple ports with a value of 3306, which is the MySQL port):
TCP 127.0.0.1:port 127.0.0.1:port ESTABLISHED
When I run netstat -a | find "LISTENING" I get: http://pastebin.com/Lqj2BrQK
Here's what I know so far:
It isn't an error with the MySQL server not being up, because I can connect to it locally.
It isn't an error with the webserver not being able to connect to MySQL databases, because I can connect to other databases
It isn't an authentication error (The username and password are correct, and the remote server has permission)
It isn't a port forwarding error, because the port 3306 is fowarded on both TCP & UDP.
It isn't an error with being able to connect to the machine the server is hosted on, because I can ping it fine.
The server isn't only listening on the localhost interface. skip-networking and bind-address are commented out in my my.cnf file.
How could I edit my connection code, or edit my MySQL server to fix this error?
Summarizing our discussion/chat:
Bind the network address 0.0.0.0 in my.cnf: bind-address = 0.0.0.0 and ensure skip-networking is commented out or not present.
Check netstat -a | find "LISTENING"
According to your pastebin there is a service listening on 3306. Test if the port is reachable on the NIC address from the server itself. This way an external firewall does not take effect. A simple test is to try a telnet connection to that port. More detailed information can be catched by the tool nmap. Nmap has displayed the mysql port as filtered. This adverts to a problem with a local packet filter, in this case the Windows firewall.
Ensure the Windows firewall is configured to allow public access to TCP port 3306 from all or a specific machine. Setup a rule in public profile or, if both servers are controled by the same domain controller, in domain profile. When the access from local machine is successful try the same from the remote web server.
If you fail to properly configure remote access to MySql port, consider to establish a SSH tunnel between the two machines. Once established you can access to MySql as if it were on the local machine. The port is then forwarded via the tunnel and on the database server side you can access the service on localhost loopback IP.

Cannot connect to mysql with 127.0.0.1

With the following code I can connect to mysql:
mysql_connect("localhost","username","");
But if I change localhost to 127.0.0.1 I get the following error:
Can't connect to MySQL server on '127.0.0.1' (13)
Why doesn't it work with 127.0.0.1?
localhost is special cased and uses UNIX sockets instead of TCP/IP. 127.0.0.1 doesn't get that special handling.
See the documentation:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.
If it doesn't work when you use TCP/IP then the database probably isn't listening on the network. This is generally a good thing as it enhances security (not that listening on 127.0.0.1 exposes any problems, but listening on all interfaces gives more opportunity for attacks).
If you really want to allow connections via the network, then see skip-networking.
have you got an entry in your hosts file mapping 127.0.0.7 to localhost?
Do you have more than 1 mysql servers installed/running on your system? If so, please specify the port number of the mysql server you are trying to access like 127.0.0.1:3306,127.0.0.1:8889 etc.
If you do not know whether there are any other mysql server instances running on your system also, please specify the port.
You will be able to access it when you add the privileges for 'root'#'127.0.0.1' in the "USER_PRIVILEGES" table in the "information_schema" database
You might also try disabling SELINUX

Website Connection to Local MySQL Server

I have recently configured a MySQL server on my Windows home computer. In addition, I have also created a website using 0fees.net-- a free hosting provider which comes with a vista panel that has various services including PHP support, FTP file-hosting, its own MySQL server etc.
For that website, I have created a "login" PHP script in order for people to login to my webpage. However, instead of reading from the MySQL database given to me on the cPanel on 0fees.net, I want the PHP logon script to read directly from the MySQL server that I have configured on my home computer. To do this, I have taken several steps:
1) Configured MySQL using the MySQL Server Instance Configuration Wizard
In that configuration, I have enabled TCP/IP Networking on port 3306 (and enabled a firewall exception for that port) and have enabled root access from remote machines.
2) On the MySQL command-line-client, I have granted remote access capabilities to other units by using:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password';
3) I have forwarded port 3306 to my router (and have used various port checkers to confirm that it is open)
4) I have created the login script called "login.php" which lies in my site's .htdocs directory and that process an HTML login form on the homepage of my website. It attempts (and fails) to connect to my local MySQL server. The segment of interest of that php script is:
$host="my_external_ip:3306"; // External IP of my computer and port to listen on
$username="root"; // Username specified in the GRANT command-line-client command
$password="password"; // Password specified in the GRANT command-line-client command
$db_name="logon"; // MySQL database name
$tbl_name="accounts"; // Table name within the database
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
When attempting to connect to the MySQL sever, I get an error message that states:
Can't connect to MySQL server on 'my_external_ip' (4).
It should also be noted that I am successfully able to connect to my local MySQL server remotely from other machines which are on other internet networks (tested with Navicat) using this same external IP address on port 3306. Any ideas as to why my website cannot also connect?
Thanks in advance for any insights.
Since I can tell that your server is definitely running (and visible from the internets in general), there are a few possibilities:
You forgot to run FLUSH PRIVILEGES
Your username or password are wrong
Your free host provider is blocking the connection
My money is on #3, there are a lot of incentives to do that.
#Chris mentioned that your provider might be blocking outbound requests on port 3306. Here's a quick way to check if that's the case and get around it:
Configure your router to port forward from port 80 on the router to port 3306 on your box. This will allow you to connect to MySQL from outside your network on port 80 instead of port 3306.
Change your PHP code to connect to your IP on port 80 instead of 3306.
Your hosting provider might block outbound port 3306, but the almost certainly wouldn't block outbound port 80 requests (lots of people using CURL or web services and such).
Well I will let you in on a little trick I learned, all you got to do is go into wamp>mysql>my.ini and replace all 3306 with 3305 then save and then apply this to your firewall and port forward 3305. Then when you are connecting remotely, simply do
mysql_connect("ip adress:3305", "username", "password");
I've tested this and it works because the web hosting sites block incoming traffic from port 3306.

Categories