Mysql - connect to remote server using IP address - php

I am using PHP and MySQL to make a database connection, the only change is that I am trying to access my remote server from my local computer using my server IP address.
mysql_connect("x.xx.xx.x","username","password") or die(mysql_error());
and the error I am getting is Unable to connect to the given host. The user has all privileges and rights.

Most default installs of MySQL only listen to the local machine.
Look for the bind-address setting in my.cnf on the server, and make sure it's listening on the IP address you're targetting. You may also need to ensure skip-networking isn't switched on!
Alternatively (and less securely!) the following will set it up to listen on all addresses - local and remote:
bind-address = 0.0.0.0

Before you try using PHP todo this. Open up your terminal or console and type the following:
$ mysql -u username -h x.xx.xx.x -p
then enter your password
Or on windows then type:
\path\to\mysql.exe -u username -h x.xx.xx.x -p
then enter your password
This will give you a more detailed response as to the authentication issue that's coming up. And you can be 100% sure that your remote login from your IP address works. If it works fine then its something in your PHP code that you're missing.

Try to add a user (USER-NAME) allowed to connect from the IP:
mysql> GRANT ALL PRIVILEGES ON *.* TO USER-NAME#IP IDENTIFIED BY "PASSWORD";
USER-NAME is the username that you would like to create (like 'widor')
IP is the public IP address of your remote connection (like '195.x.y.z')
Of course, limit the privileges you want to grant (ALL PRIVILEGES is probably not your choice)

1) Check which interface MySQL listen for connections, local (localhost, 127.0.0.1) or remote (0.0.0.0 or IP address). It's in my.cnf. http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
2) Check does your user have corresponding privileges. User that logs in from localhost can have different priveleges from remote one. For example 'user'#'localhost' and 'user'#'%'

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

How do I set MySQL host to something other than 'localhost' on ubuntu?

I have two server bought and I have 2 real IP's. One for files and another for database. I setup mysql database to second server but I cannot connecting it from my first server because mysql hostname is localhost. How can I connect 2nd server database from first one?
Use the IP Address of the Database server as the host name
i.e
host = '192.168.1.10`;
you will of course also have to make sure you are using a MySQL user account that is allowed to login from your applcation servers ip address
Go to your first server.
give below command on first server-
mysql -h server_ip -u myuser -p
Here server_ip means 2nd db server ip, myuser is a db user for 2nd server.
Now it will ask password so give your db server password for root user.
Note: Make sure myuser should have privileges on 2nd db server either from 1st server or should have global privileges.

MySQL Can't Connect to External Server

I am trying to connect to an external server on a fresh MySQL install, but it just seems to timeout. Access has been granted by the other servers admin to the particular account and ip address. I try running the following:
mysql --host=hostname -u username -ppassword database
But just get the following:
ERROR 2003 (HY000): Can't connect to MySQL server on 'hostname' (110)
I have adjusted the following in my /etc/mysql/my.cnf file:
bind-address = 0.0.0.0
and restarted, but it didn't help. Any recommendations?
If you have checked that the firewall is letting MySQL traffic (usually port 3306) pass through and the service is running, the most probable cause is fine grained permission on the server. The user may not have been allowed to connect from any host, probably the permissions to connect are IP based or something similar.
I would try to use that code but not providing the password, like:
mysql -h hostname -u username -p database
and intert the password after that command (it will ask for it).
Also you could try to provide a port: --port=3306 for example.
You can also try to provide the protocol --protocol=TCP.
The only last idea that i have is that you dont provide the database and enter the database after you access the server...
sorry, can't do anything else from here with that data.
Try to grant the user to access database from the remote computer.
to do that:
mysql> GRANT all on database.* to 'username'#'%' IDENTIFIED BY 'user_password';
this is not recomended becouse this will allow connection to your mysql server from anywhere.
for allow connection to user in a certain ip address (we use 8.8.8.8 as example) use code below
mysql> GRANT all on database.* to 'username'#'8.8.8.8' IDENTIFIED BY 'user_password';
The problem lies in your permisions granted to your user. try changing permisions and hope it will help you .

cant connect database server using ip address

I am using PHP and MySQL to make a database connection , problem is :
the web application project in my local machine when am trying connect to database remote server every thing is work fine
mysql_connect("x.xx.xx.x","username","password") or die(mysql_error());
when i deploy the web application in the remote server that have the mysql database and try to use that web application am getting no database error connection !! if i change database connection to :
mysql_connect("localhost","username","password") or die(mysql_error());
using localhost (127.0.0.1) instead of using ip address ever thing work fine , i have tried check /etc/my.conf file and add bind-address=192.168.15.15 and restart mysql server
still cant connect to database
any idea !!!
You need to check two things. First, make sure the port 3306 is open for connections. Second, if you're connecting remotely, you'll need to create a wildcard user (The host will be a percentage sign %). Take a look at the mysql.user table. If the user you're trying to connect to has a Host of localhost, you won't be able to connect remotely.
To create a new user:
CREATE USER 'newuser'#'%' IDENTIFIED BY 'password';
Give that user permissions, I suggest to only the database in question:
GRANT ALL PRIVILEGES ON your_database.* TO 'newuser'#'%';
Finally, reload the privileges:
FLUSH PRIVILEGES;
problem was i was enabling the SElinux , so i enabled httpd network with commad :
setsebool -P httpd_can_network_connect=1

mysql_connect not working for a site

so I finished a site locally, and I have been trying to start to transfer it to an online server. And this is probably a really stupid question but I've spent the last hour trying to find the answer and I can't. So this is a line of PHP code being run on the server (with the username and password changed, but the hostname the same):
if(!mysql_connect("www.bluestreakaquatic.com","username","password"))
die(mysql_error());
It produces this error:
Can't connect to MySQL server on 'www.bluestreakaquatic.com' (10061)
Can anyone possibly give me any sort of advice?
Your hosting provider probably doesn't allow external mySQL connections for security reasons.
If mySQL runs on the same server as your site, try replacing the domain name with localhost.
If that doesn't work, you need to ask your hosting provider for the correct database server address.
I had recently experienced the same problem.
If you have full access to your server (root privileges required):
Step 1: edit my.cnf (usually located in /etc)
Find the following line: [mysqld] and make sure line skip-networking is commented (or remove line) and add following line:
bind-address=YOUR-SERVER-IP
For example, if your MySQL server IP is 66.166.170.28 then entire block should be look like as follows:
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
language = /usr/share/mysql/English
bind-address = 66.166.170.28
# skip-networking
.......
Where
bind-address : IP address to bind to.
skip-networking : Don’t listen for TCP/IP connections at all. All interaction with mysqld must be made via Unix sockets. This option is highly recommended for systems where only local requests are allowed. Since you need to allow remote connection this line should be removed from my.cnf or put it in comment state.
Step 2: Grant access to all hosts
Start the MySQL monitor with this command: mysql or /usr/local/mysql/bin/mysql. Your shell prompt should now look like this: mysql>. Run this command:
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'%' IDENTIFIED BY "PASSWORD";
Where:
USERNAME is the username that you use when connecting using your php script.
PASSWORD is the password you use when connecting.
You now must flush MySQL's privileges. Run this command:
FLUSH PRIVILEGES;
Run this command to exit MySQL:
exit;
Step 3: restart mysql deamon
/etc/init.d/mysqld restart
or
/etc/init.d/mysql restart
depending on what linux distro is your server currently running.
If you are on a shared hosting environment:
It is unlikely that any hosting provider will allow remote connections to mysql server because of security risks. But you may be lucky if your hosting server uses DirectAdmin control panel or cPanel. If so, follow these steps:
For DirectAdmin:
Goto MySQL Management. Select your database and click on it. You will find a section named Access Hosts. Add a new host: (%)
For cPanel:
Goto Remote MySQL under Databases section and add % instead of myhost.com:
Also see the reference: http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
If you're unable to connect to a remote mysql server from the web server, but could connect to the remote mysql server from your localhost, the server may not have permissions granted for your username originating from that host.
GRANT ALL PRIVILEGES ON database.* TO username#serverhostname IDENTIFIED BY 'password'

Categories