Connecting to MySQL Database with Code Hosted Locally - php

When I try to run working php code on my localhost instead of the web server, I am getting a connection error.
Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] Connection refused
Any idea how to get the MySQL username, db, and password to work from my local machine? I am using OS X Mountain Lion and Apache.
Do I have to login to the database server and add my IP?
Thanks!

Unless you've changed the default password root is allowed to connect to localhost, so that would be something like this :
<?php
$mysqli = new mysqli("localhost", "root", "", "");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
You can replace localhost by an ip, depends on how your database is configured to allow connections, using localhost or an explicit ip.

in case you have downloaded the code from a server and are trying to make a local replica then you need to update the connection parameters to match your local configuration. You need to update username, password , database etc. as per your local settings.. Hostname you may keep as localhost
Respond back if things are not working and add more detail on how things started like how you set up the code locally..and what things you have tried yet..

Related

Can't connect to remote MySQL database with MySQLI - timeout

I have created two servers on Linux and installed LAMP on both, one I called web and the other is called app.
I would like to save a log from the web server on the app server - but when I try to connect to the app server, I get timeout.
My code is:
$mysqli = mysqli_connect("66:66:666:66", "username", "pass", "DB");
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
Where 66:66:666:66 is the ip to the app server.
I have in the app server under PhpMyAdmin added user privileges to a user with the name "username" and then the host is "%".
Have i forgotten something?
Edit: I have already tried to add port number 3306 to the connection.

How to access database?

I need to access the database. I've downloaded the file where database credential reserved.
The file is mysql.php
$conn = mysql_connect('localhost','user','password') or die("Connection error: " . mysql_error());
mysql_select_db('newadv',$conn) or die("Connection error: " . mysql_error());
IN the server how can I access the localhost and database.
The mysql.php file now is in server and the website is running.
I need to access the database from Mysql interface. How can I achieve this?
I finally got reply from webhosting service. They gave me the credentials to log in.
Thank you every one.
When you want to connect with server.If database present on same server then use host as localhost & database server username & password.If they are on different server then in place of localhost provide the server host details.
like:
$conn = mysql_connect('111.255.255.254','root','root') or die("Connection error: " . mysql_error());
mysql_select_db('newadv',$conn) or die("Connection error: " . mysql_error());

Connecting to mysql on another server.

The solutions I got are not working for me. Here is my code -
//Connect to database
$tempCon = new mysqli($domain, $username, $password, $database);
if ($tempCon->connect_errno) {
echo "Failed to connect to MySQL Database: (" . $tempCon->connect_errno . ") " . $tempCon->connect_error;
} else {
echo "connected";
}
The error I am getting is -
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on 'domain.info' (110) in /home/server/public_html/products/websites/EditWebsiteContent.php on line 8
Failed to connect to MySQL Database: (2003) Can't connect to MySQL server on 'doamin.info' (110)
Its because of the firewalls on the server which doesnot allow to connect.
If you are using shared hosting. You need to allow remote connecting to your database from your IP address.

PHP: getaddrinfo failed when trying access a 1&1 database from a local web app

I have an issue when I try to access my 1&1 database from the local web app I am coding.
Output: Failed to connect to MySQL: (2002) php_network_getaddresses: getaddrinfo failed: Unknown host.
I use mysqli (which works great with a local database) to connect my app with the DB and I already know the php5 issue with mysqli and 1&1 servers but this would occurs only if my app where on my server.
$mysqli = new mysqli("dbxxxxxxxxx.db.1and1.com", "dboxxxxxxxx", "xxxxxxx", "dbxxxxxxxx", 3306);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
else
{
$mysqli->set_charset("utf8");
}
Maybe the issue comes from the way I write the host, I tried to add "http://" before but the problem was the same.
Thanks for helping me! :)
Seems a name resolving problem. If you use Linux/MacOS, add the hostname and its IP address in hosts file:
# echo "123.123.123.123 dbxxxxxxxxx.db.1and1.com" >> /etc/hosts
You must replace this example IP for the real one.

Can't connect to Dreamhost's database

at first sorry for my english, i've a important question to resolve.
I'm trying to connect to my dreamhost's database but it's returninng the following error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I've contacted Dreamhost support and they told me that
it looks like the error was the result of using a newly created database
hostname.
I see that the hostname is now working properly
Now, I have a php class to connect to my database.
this is my command to connect:
$data = new MysqlClass('mysql.test.masterweblab.com','user','pass','data_name');
This works on my localhost. Anyone have a solution?
maybe they are using a different version of php or mysql than on your localhost?
try connecting to it like this on a new page and see if this works.
$link = mysql_connect("mysql.test.masterweblab.com", "user", "pass");
mysql_select_db("data_name") or die(mysql_error());
if ($link){
echo "it worked!";
}else{
echo "Failed " . mysql_error() ;
}
mysql_close();
If that doesn't work try pinging the host from a command prompt in windows. Copy the ip address and try.
$link = mysql_connect("THE_IP_YOU_COPIED", "USER", "PASS");
mysql_select_db("data_name")or die(mysql_error());
if ($link){
echo "it worked!"
}else{
echo "Failed " . mysql_error() ;
}
mysql_close();
Let us know what that produces.
if you cant get the ip from pinging the domain there's your problem! Log into cpanel and get your sql servers ip from phpmyadmin.

Categories