I'm beginning to migrate a software project from being a desktop application to a web application. Currently I am using a local PHP/MySQL connection that is associated with the desktop it is installed on.
I'm hoping to untangle this and am trying to create a MySQL database through my 1and1 account. I had no trouble creating a database and recorded my account information. I'd like to be able to edit this database using PHP scripts on my system. However, I haven't been able to get a working connection string going. When I run the following PHP script:
$hostname="db*********.db.1and1.com";
$database="db*********";
$username="dbo*********";
$password="*********";
$link = mysql_connect($hostname, $username, $password);
I get the following error when I run the script in my webbrowser:
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'db*********.db.1and1.com' (11001) in C:\Program Files\xampp\htdocs\Remote_MySQL\rightbid_database_initialize.php on line 74
Connection failed: Unknown MySQL server host 'db*********.db.1and1.com' (11001)
How do I get my local system to recognize the 1and1 server host? Am I structuring my connection string correctly? Is it possible to access a remote server from a locally hosted Windows PHP connection?
Thanks!
I assume what's happening if that 1&1 are blocking any connections coming from outside of their network.
Try connecting using
mysql -h HOST -u USERNAME -p DATABASENAME
Good luck
Make sure the MySQL server on 1and1 will accept connections from remote clients. Not just on localhost, and also make sure port 3306 is open to the server.
Related
I have a new drupal site running behind a load balancer. I've got one master slave set up with the database and another that syncs files from the web dir.
For the remote slave, I've got ENV vars setup for the database host, which is the INTERNAL IP of the master slave holding the database. This works fine as the master has this too with it's ENV var being 'localhost'
Now, when accessing the site, getting put on the master slave works fine. But when hitting the remote slave I get the database error from Drupal :
PDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL
server through socket '/var/lib/mysql/mysql.sock' (2) in
lock_may_be_available() (line 167 of /var/www/html/includes/lock.inc).
I've run the regular GRANT commands for the remote slave and I can connect over SSH from the slave machine using
mysql -u root -p -h master.slave.internal.ip
So from what I can tell it's not a firewall or mysql permissions issue, I'm stumped! :(
Big beer coming someones way, as I'm left scratching my head.
It looks like your Drupal config is wrong if you need to connect to a database on a different server. The error message says it can't connect to the "local MySQL server", not that it can't connect to "MySQL on server ". Look at settings.php and make sure the database ip address/hostname is correct.
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?
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
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);
I get this everytime I use mysql_connect() no matter what database I choose:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'IP' (111) in filename.php on line 17
A MySQL error has occurred: Can't connect to MySQL server on 'IP' (111)
The exact same file works on my personal website fine. I have tried multiple databases hosted on different servers and it always gives that output.
The database itself is hosted on the same server, but using its full IP in mysql_connect(). Using localhost:port doesn't work either as it says:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in filename.php on line 17
A MySQL error has occurred: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
But using the IP should work as it has worked calling it via the same file hosted on other servers.
This is the code:
$connect = mysql_connect($db_url,$db_user,$db_pass); // connects
if ($connect == false) exit("A MySQL error has occurred: " . mysql_error());
Now since the file works on other servers i am guessing it is something to do with the server it is on and might need something changed. I don't personally have root access to the server (just my part of the shared host). Is there anything I can do i php, editing the php.ini file or something I should pass on to someone with root access?
Edit: Ok it turns out that the server doesn't have access to outside databases, so thats why the IP didn't work. Thanks for all your answers but we have decided simply to change hosting provider. We need to be able to access an outside database.
This is on a hosting service? Check their documentation, there will be something that tells you where to find mysql. It isn't necessarily localhost.
For example, on startlogic.com, you use: yourdomain.startlogicmysql.com
Can you connect using mysqladmin using the same host, username and password?
mysqladmin -h $db_url -u $db_user -p $db_pass
Replace $db_xxxx with real values.
If that works from the same host as your php script, then sudo to the apache User and try the same test. One of those must be failing.
EDIT: nevermind on sudo part, I noticed that you don't have root access.
Something else to try: Use '127.0.0.1' instead of 'localhost'. I have had issues before where mysql stupidly assumed it could silently change 'localhost:' to '/var/run/mysqld/mysqld.sock'.
Your wording is not very clear, I hope you are not thinking you can connect to the same mysql server from any old web server just because you know the IP address and port number. If the web host is at all competent, they have probably firewalled mysql so it is only accessible through their own web servers.