I have this error when i try to access to some web pages of my project:SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql1.alwaysdata.com:3306' (2)
alwaysdata is the phpmyadmin website I use for my database.
I noticed that it's when I try to access in some pages in relation with the database (create user for example... etc) and there's no problems with other pages like 'contact'.
I'm on mac OSX and I use MAMP server, always data, laravel and netbeans IDE. all configurations required to make the connection between my project and the database is correct.
Here it is:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql1.alwaysdata.com:3306' (2)
open:
/Applications/MAMP/htdocs/lesenfantsdurhone/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
*/
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
If someone have an idea...
Thank you.
The port has it's own parameter in the DSN:
'mysql:host=mysql1.alwaysdata.com;port=3306;dbname=xxx'
Alternatively just omit the port, 3306 is the default port.
I had this issue while using Laravel with MAMP and Sequal Pro.
Inside the .env file, I set the
DB_HOST = "localhost"
DB_PORT = "3306"
and this worked.
Before, I had
DB_HOST = "localhost:8889"
I kept getting this error because of the failing DNS service due to the time on my Virtualbox being off - it does that after you put your host machine to sleep for example.
Hope this helps someone in a similar situation.
In my case I had double definition of DB_HOST variable in '.env' file
First one is correct:DB_HOST=127.0.0.1
And last one was
DB_HOST=127.0.0.1:3306
with unwanted 3306
That was the cause.
Related
I'm trying to test a PHP website locally on an Ubuntu 16.04 machine, but I'm having trouble connecting to MySQL database using mysqli_connect().
I have Apache 2.4 running with mod_php7.0.
I installed the Ubuntu package php7.0-mysql.
Accessing a php.info() file at a localhost address in the same directory as the website indicates that my php.ini file is /etc/php/7.0/apache2/php.ini.
In that file, I removed the comment from the line ;extension=php_mysqli.dll.
The file I'm trying to access contains the line
$con=mysqli_connect("localhost","db_user","db_password","db_name");
When I try to access the site, I get the error
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known"
with reference to the line above.
I have MySQL running locally with the given user, password, and database defined. I've tried using 127.0.0.1 instead of localhost, but the error is the same.
The error appears to be that PHP can't make a local connection to MySQL. How can I figure out what's wrong?
Edit To give more context, I'm trying to create a localhost version of a website that works on its production/dev servers. As far as I can tell, I've recreated all relevant aspects of the server setup, but obviously I've missed something.
The biggest difference between the local setup and production/dev version is that I have PHP7 locally but PHP5.5.9 on production/dev. My best guess at this point is that this difference is what's responsible for the localhost failure, but I don't know enough about PHP to narrow down what it might be.
Use the following to connect to MySQL database on localhost.
<?php
$servername = "localhost";
$username = "root";
$password = "mysql-password";
$dbname = "database-name";
$conn = mysqli_connect($servername, $username, $password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
I'm kind of new to programming and I'm only trying to connect to database using xampp but it gives me this error.
Warning: mysqli_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it.
$conn = mysqli_connect('localhost', 'root', '', 'db');
I tried running the same code on a different PC and it works fine.
so i guess the problem lies within PC that I'm working on.
Any suggestions what to configure?
If you are sure that login and password are all correct, then check port number.
Default MySQL port is 3306. Maybe you've got different one on this machine.
Or, it is also possible, default port in PHP may be changed.
See XAMPP configuration to see MySQL port and add it to your server address.
PS: you may also see privileges for "localhost" connection. See 'user' table in 'mysql' database. As you have XAMPP installed, you should have phpmyadmin as well.
I want to use two database instances in my CodeIgniter application. I have set up db1 and db2 in the application but it always uses the socket of the first database.
$this->db2 = $this->load->database('db2', TRUE);
Message: mysqli::real_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
but the second server runs on '/var/run/mysqld/mysqld2.sock'
I can not see where to tell it to use the second socket.
Please note that I still want to connect to the first database too, I need to merge two replications, so setting defalut socket in php.ini is not an option.
You'll also need to edit your database config file to add the additional settings. Example:
$db['default']['hostname'] = 'localhost';
..
..
..
$db['second_db']['hostname'] = 'localhost';
It seems it was something CodeIgniter was missing, at least for mysqli driver: The socket parameter was always NULL in the driver code. With a modification of the code and the configuration array, it is possible to connect to any given socket for the server(s).
Add this to your config
'socket' => '/tmp/mysql.sock', // if no socked uses set it to NULL
And change this in your /system/database/mysqli/mysqli_driver.php
return $mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $this->socket, $client_flags)
But keep in mind that editing core files could conflict with framework updates, causing errors in your database implementation.
http://forum.codeigniter.com/thread-486.html
I'm setting up a PDO connection in a test script:
use app\SomeDAO;
class SomeTest extends \PHPUnit_Framework_TestCase
{
protected $db;
public function setUp()
{
$dsn = "mysql:host=localhost;dbname=baseball;user=root;password=root";
$this->db = new PDO($dsn);
}
I'm getting an error:
PDOException: SQLSTATE[HY000] [2002] No such file or directory.
How can I use PDO here?
In Unix based (like linux, bsd, or OS X) systems, with MySQL localhost is secret code for try-to-use-a-socket, unless a you force it via a protocol flag to not do this (and no one ever does this). Just remember localhost usually equals socket file.
If Mysql in your MAMP is running in non-socket mode, you can try replacing the host value with 127.0.0.1 which connects via TCP on via port to the local machine--you'll need to figure out which port it's running on if it's not the default port.
If you look at the MAMP start script
less /Applications/MAMP/bin/startMysql.sh
You can see if it's starting in socket mode, and what file it's using if it has a config param like:
--socket=/Applications/MAMP/tmp/mysql/mysql.sock
You can also investigate what open socket mysql might be using by following the answer in this question: error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
If you're running in socket mode, you need to make sure PDO knows the path to the socket file. One way to do this is specify the unix_socket instead of host in the dsn:
$dsn = "mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=baseball;user=root;password=root";
I had problem like that at MAMP. My decided it, when I connected with PDO I used next line code:
$this->pdo = new \PDO("mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;port=8889;dbname=mydatabase;charset=utf8", 'root', 'root');
I have a connection.php file that is suppose to connect to a remote database.
Here is the code:
<?php
try {
$conn = new PDO('mysql:host=IP;port=PORT;dbname=DBNAME', 'USERNAME', 'PASSWORD');
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
Now all my variables I believe are correct since I can connect to the database through Toad. I used this same PDO format for my own database connection through localhost and it works fine. I am not sure what the problem is. Since i can use Toad i believe that the server already allows remote access to it, but i am not sure on that. Any input would be nice.
Also this is the Error that PDOException is coming back with:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'IP' (111)
111 means connection refused,
It probably means that your MySQL server is only listening the localhost interface.
If you have lines like this :
skip-networking
bind-address = 127.0.0.1
You should comment them In your my.cnf configuration file (add a # at the beginning of the lines), and restart MySQL.
well, in you /etc/mysql/my.cnf :
you should find out this:
skip-networking
bind-address = 127.0.0.1
You gotta comment the first one, and change the ip to 0.0.0.0 or to the ip of your remote host that you want to allow (recommended)
Also, you may don't have a user to connect to that host, i mean, mysql users are like 'root'#'192.168.1.1', so you may try to access with a user that doesn't exists. Try to create it and then grant to it all privilegies.
Just execute this couple commands on mysql:
CREATE USER 'root'#'192.168.1.100' IDENTIFIED BY '***';
GRANT ALL PRIVILEGES ON * . * TO 'root'#'192.168.1.100' IDENTIFIED BY '***';
Also, make sure that mysql is listening to in the right port, if using linux:
netstat -tlpn
should do the work, at least that's how i fixed similar problems.
I was facing the same error for a few hours now.
Turns out I had given the wrong port in my code.
My port to connect was the default port 3306 I was using 3327.
Hope it helps someone maybe.