How can I write data to a database table on another server? - php

I am using the mysqli_connect($host, $user, $pass, $db) line to connect to another server through php. I am trying to write to a MySQL database.
I have used the server's IP [A.B.C.D] on the $host variable. However I cannot write to that database, and the script does not run completely.
I am using the root user and password to connect, and the user list in phpmyadmin shows that anyhost(%), 127.0.0.1 and localhost can connect with the root username (I think that's what it means). Anyway, as I am using root, I don't think this is a user privilege problem.
I have tried putting a # before 'bind-address=127.0.0.1'.
I have tried editing my.cnf according to suggestions from a lot of forums.
I have not installed firewall in any of the servers that I'm using, so unless Debian 7 64-bit comes with pre-installed firewall that blocks certain ports, I don't think that is the problem either.
Isn't there an easy way to establish connection between multiple servers? :-/

Your remote mysql server must accept remote connection from your IP address.
After you make this setup you can access remote mysql server like this:
mysqli_connect($remote_host, $remote_user, $pemote_pass, $remote_db);

Related

Can't connect to remote SQL database in PHP, but remote access is set up and confirmed working

I'm trying to learn to use SQL stuff in PHP but I'm having an issue simply connecting to my database.
The database is almost certainly working with remote access to some extent. I disabled iptables temporarily (I know, bad bad bad!), so there's no firewall blocking access. The MySQL user I am using is configured to be able to connect from any IP. The SQL server is listening properly to all connections and is run on a Linux dedicated server.
[root#1742CC-XEON ~]# netstat -lpnut | grep mysql
tcp 0 0 0.0.0.0:3315 0.0.0.0:* LISTEN 3915/mysqld
I've also used the exact same credentials to successfully remotely connect to my database in Java, like so:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://__ip__:_port_/_dbname_", "username", "password");
System.out.println("Connection established.");
This functions just fine, and I can successfully run queries, etc. Since I have iptables disabled at the moment this connection functions just fine anywhere that has Java and the Java SQL connector.
Now, I'm trying to connect to the same database in PHP:
$db = mysqli_connect($sql_host, $sql_user, $sql_pass, $sql_db, $sql_port);
And this connection fails with the following error:
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'xx.xx.xx.ip' (111)
Note that $sql_user, $sql_pass, $sql_host, etc. are all the same as the credentials used in the Java connection, so there shouldn't be any issue with the user not being able to access the server from a new IP, since that user is granted access on % and I've made sure that it is indeed able to access the SQL server from multiple IPs not explicitly allowed in the users table. I've also tried different users, including root, and all give the same connection error.
I've spent a few hours looking around but I honestly can't find anything that seems to be the right answer, would really appreciate any help!
Is it possible that this is caused by my webhost? I've tried this script on HostGator webhost as well as the whois.com hosting and both give the same error. Could they possibly be preventing the PHP script from connecting to the remote database?
Have you tried telneting to the server on 3315 from your webhosts?

Access MySql Databases from same Network or Remotely access from Different Servers

I have an application in which I have to access all pc connected to same network and their MySql Databases and I also want want to connect to remotely a server.
Actually I have list of drop down services and each service holding a database name. when I select a service then I want to build connection to database either it lies on same network or any remote server.
Remember, I know the hostname, username, password and dbname. and I am using mysqli_connect function.
I have try multiple options given on web, but all in vain. e.g grant host and user access. But not found any solution can help to solve my problem.
I have try bind-address option in my.conf file but no solution.
Here is my code
For remote Server
$con = new mysqli_connect('xxx xxx xxx:3306', 'username', 'pass', 'dbname');
For local Network
$con = new mysqli_connect('xxxx xxx xxx:3306', 'localhost', '', 'talent');
Error
mysqli::mysqli();(HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. and also get this one earlier Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES).
I am using windows platform. WIndow 8.1
I guess you want to access mysql database installed on different computers on same network as your computer and also on some remote server.
What you need is IP Addresses of all the computers on which the mysql server is installed including the remote server. And on each such computer mysql server remote access should be enabled for your computer's IP Address and mysql user you are using in 'new mysqli_connect()'. See this tutorial.
Also as fvu said check php documentation for 'mysqli_connect' function.
UPDATED
What is SSH?
1. SSH is SecureSHell.
2. Its nothing but a remote login tool or program(like telnet but in secured way).
3. Remote login is logging in to some other computer(known as remote computer) as a user of that computer from your computer.
4. After you successfully logged in to the remote computer via ssh, you can type commands on that remote computer on behalf of the user you are loggen in with.
5. Consider this as if you are sitting in front of your computer and watching the command prompt(terminal in linux and mac) screen of the remote computer.
6. Whataver you will type here will reflect there.
For your knowledge to make you understand the problem:
1. When you install XAMPP or WAMP on your computer it also installs MySQL Server with it.
2. MySQL Server is a process running in the background to which we can request to do
database operations like SELECT, UPDATE, etc.
3. This thing we generally do using 'mysql_connect' or 'mysqli_connect' in php.
4. MySQL Server can have many users and different users have different previledges/permissions. So that MySQL admin user(i.e. root) can control what things are allowed and not allowed for a user.
5. Now while connecting to the mysql server process we need to specify the user credentials. That you specify in mysqli_connect function.
Now let me explain you why you are getting that error:
1. The user credentials you are using either does not exist or not correct or the user has no access to connect to MySQL Server process remotely. i.e. from other computer that the one has MySQL Server installed on. In your case from your computer to computer A or to computer B or remote server.
2. The other reason may be the firewall settings of remote computers.
Solution:
1. For computer A and computer B you don't need ssh you can directly go to the computer and open command prompt on it and type commands.
2. But for remote computer you need to use ssh.
3. Now another problem is you are using Windows so ssh program will not be available to you.
4. You will need to download putty program. Its ssh implementation for windows. Same as ssh only name is different.
5. Take a putty tutorial to connect to remote server.
6. Then you can use this tutorial for granting remote login access to mysql user you are using in mysqli_connect function.
If the server is a local hosted database and you can connect to it from local host and i you are not able to connect to it from a remote machine consider the following options.
There is a firewall in ubuntu that you need to open, you do this by granting access in IPtables.
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT And now we should be able to login to our server from our local machine:
mysql -h255.112.324.12 -uroot -pMyPASSWORD
You need
to grant access to mysql: https://askubuntu.com/questions/159053/mysql-server-not-accessible-from-remote-machine
as root, open your /etc/mysql/my.cnf with your favorite editor look for the [mysqld] section, and in there for the bind-address keyword. This usually is set to 127.0.0.1 -- change that to match your "normal" IP-address save the file, and reload the service (e.g. using service mysql restart)
Last but not least you need to give remote access in mysql:
GRANT ALL ON mydb.* TO root#'%' IDENTIFIED BY 'MyPASSWORD';
Find more information here: http://web.archive.org/web/20120930214828/http://chosencollective.com/technology/how-to-enable-remote-access-to-mysql
Right now you seem to be mixing object-oriented and procedural approaches. If you want to use object-orented approach, mysqli object should be instantiated as follows:
$con = new mysqli($host, $username, $password, $dbname, $socket); // all but first parameter are optional
If you want to use procedural approach, mysqli connection resource should be created as follows:
$con = mysqli_connect($host, $username, $password, $dbname, $socket); // all but first parameter are optional

Have two websites on different server but need one database using php and mysql

I want one website on one server and another website on another server, but only 1 database for both using php and mysql.
is it possible? if yes then how?
Yes. its possible. Normally you dont put your db on distant server. You should keep the db server on same data center so you avail high speed internal network link.
If your websites are in host web1 and web2 and database is in dbhost1, then in web1 and web2 connect to mysql with dbhost1 as host name.
mysqli
$mysqli = new mysqli("dbhost1", "user", "pass");
PDO
$dbh = new PDO('mysql:dbname=dbname;host=dbhost1', "user", "pass");
Legacy mysql extension
mysql_connect("dbhost1", "user", "pass");
Note: Make sure in the database user#web1 (on host web1) or user#web2 (on host web2) has access.
You can connect to a remote database given the correct settings.
Failing that, you could write a service layer that could be called from both servers.
while connecting to mysql database use same database connection credential on both servers.
Yes why not. But for security and performance issues i wouldn't do that.
The question is what do you understand under on another server is the server direct connected over LAN or is the server in another Data center. When the server is in another Data center you can get a lot of traffic and performance issues.
When the server is direkt connected you can change the my.cnf and change the line:
bind-address 127.0.0.1
then the server is reachable from outside. And you should give the user enough rights to connect from outside.
yes, it's possibile.
you have to use a mysql server which is reachable from both servers. and you simply connect with the same host/user/pass from both servers:
define("WEBDOMAIN", "94.145.22.15"); //some fake data
define("DEFDBNAME", "my_db");
define("DEFUSERNAME", "my_user");
define("DEFDBPWD", "my_pass");
mysql_connect( WEBDOMAIN, DEFUSERNAME, DEFDBPWD );
mysql_select_db( DEFDBNAME);
That should be enaugh. You coud have some problems if your hosting providers doesn't allow servers to connect to external servers (sometimes ports are closed).
For security reasons it is common, that You can't connect to a mysql server that has not authorized your hosting server.
So in mysql, You have to allow a user to connect not only from localhost, but also from other host (if You have enought priviliges, You can do it from phpMyAdmin by editing user priviliges afair).

public webserver trying to query sqlserver on internal network with private IP

I am using php to send a query to an existing database to use existing login credentials for a webbased program to allow access to a members section page. My web server is public and has a public ip as well as a private ip. However, my sql server is sitting on the local lan and does not have a public ip. I am very new to php, but I feel I have a done a good job writing it. the code works amazingly when I hosted the site for internal use with a dummy database on my pc. I set up Apache on my pc to run the php code and set up a sql database for testing purposes. I could access the site and login and out from any other computer in the office, by typing in my 192.168.x.x into the web browser.
Now that the site is moved to the web server and I am linking to an active database on another server it doesnt want to work. I am pretty sure i dont have any coding erors causing this its a configuration issue. I am wondering what ports should be open where? and will existing DB users be able to query the DB from a remote private ip? I realize this may be a beginners question, but I have looked everywhere for days now and my brain is fried. I need a basic checklist of the main things to look for or set when establishing this type of connection.
Website is running on 192.168.1.1 with public ip of 173.72.173.x
SQL DB is running on 192.168.1.2
I log into the sql engine on the sql server with 'user' and 'password' so my config file i use:
$server = "192.168.1.2:3306"; // server to connect to.
$database = "myusers"; // the name of the database.
$db_user = "user"; // mysql username to access the database with.
$db_pass = "password"; // mysql password to access the database with.
$table = "dbo.users"; // the table that this script will set up and use.
And I call it into every file that need to query anything.
So are theses setting right? Or do I need to create another user on my sql engine to access the db from a remote host? and what about ports? sql server has 3306 opened, but that's it?
If you are using MySQL (you mentioned port 3306), you will most likely want to check the my.cnf configuration file on the MySQL server, and have it allow remote connections.
Just do a fast search for bind-address = 127.0.0.1 and comment it by putting a hash# in front of it and disable interface binding.
Since you did not specify the OS you are using, I can't know for sure whether it may be bound to a specific NIC or not. Try it yourself.

How do I access another MySQL Database from another IP Address with PHP?

Ok, If you can answer this question, you deserve the nobel peace prize. Anyways, here's the situation.
I'm using Slicehost dot net, and I have 2 slices (two different IPs). One slice is my mail server and the other is my normal website. I'm running Ubuntu (8.04 or 8.10, something like that, it shouldn't matter). What I'm trying to do is access the MySQL database on the Mail server from the other slice. I'm trying to do that with PHP. I really appreciate any help.
mysql_connect()
$resource = mysql_connect('other-server-address.com', 'username', 'password');
The first parameter is the mysql server address.
Server Param
The MySQL server. It can also include
a port number. e.g. "hostname:port" or
a path to a local socket e.g.
":/path/to/socket" for the localhost.
If the PHP directive
mysql.default_host is undefined
(default), then the default value is
'localhost:3306'. In SQL safe mode,
this parameter is ignored and value
'localhost:3306' is always used.
Unless I'm misunderstanding... this setup is pretty common. Any trouble you're having might be related to the following:
Firewall settings
Grant access to the mysql user to connect from the other host
my.ini settings not allowing outside connections
Some other related SO questions:
Connecting to MySQL from other machines
How do I enable external access to MySQL Server?
php access to remote database
How to make mysql accept connections externally
Remote mysql connection
Assuming your mail server is at IP 192.168.1.20 and web server is 192.168.1.30
First of all you need to allow the web server to access the mysql database on your Mail server .
On 192.168.1.20 you run the mysql command and grant access on the database needed to your web server
mysql> grant all on mydb.* to 'someuser'#'192.168.1.30' identified by 'secretpass;
Your PHP code connects to that database with something like:
$conn = mysql_connect('192.168.1.20', 'someuser', 'secretpass');
mysql_connect() returns a link identifier if the connection is successful. Also you have to do is keep the references to both links.
When you want to use which ever link, simply include the link as an argument.
$link1 = mysql_connect($host1, $username1, $password1);
$link2 = mysql_connect($host2, $username2, $password2);
$r = mysql_query(QUERY, $link1);
Simple as that.

Categories