Connect to MySQL using PHP on Ubuntu 14 - php

I am trying to connect to a MySQL database using PHP using the following code:
<?php
$servername = "localhost";
$username = "root";
$password = "pass";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
When using localhost, I get a message
Connection failed: No such file or directory
When using 127.0.0.1 for the host, I get
Connection failed: Connection refused
I have confirmed that I can connect using the command line, and the output is below
mysql -u root -p pass -h localhost
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7282
Server version: 5.5.47-0ubuntu0.14.04.1-log (Ubuntu)
Connecting using TCP
mysql -u root -p pass -h 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7318
Server version: 5.5.47-0ubuntu0.14.04.1-log (Ubuntu)
The following is the relevant mysqli section of the php.ini file in /etc/php5/apache2
mysqli.default_port = 3306
mysqli.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_host = 127.0.0.1
And the settings from /etc/mysql/my.cnf
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
.
.
.
bind-address = 127.0.0.1
I have tried using PDO to connect to the database with the same results. Can anyone see a configuration setting that is not correct, or have another idea of what could be wrong?
Edit: I installed phpmyadmin and it connects to MySQL with the same settings.

Try to do this?
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Related

How can I bypass error 111 MySQL in PHP connection?

I have bought a database MySQL to use on my domain/host.
I can connect to the database via wampserver (localhost) without problems.
But when I am trying to connect to the host website it gives me an error.
Connection failed: Can't connect to MySQL server on 'mysql-******.net' (111 "Connection refused")
My code:
<?php
$servername = "mysql-*********.net";
$username = "username8954";
$password = "dolphyn1235";
$dbname = "animals";
$dbServerPort = "26313";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $dbServerPort);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Any ideas how to solve this?
Check in your MySQL Server configuration files (my.cnf), find bind-address = 127.0.0.1 then comment them with # at the begining of the line. and then restar your MySQL.

Specifying socket option on mysqli_connect

I have two mysql.sock file.
One is at /opt/lampp/var/mysql/mysql.sock.
Another one is at /var/run/mysqld/mysqld.sock.
I want to create a connection using first mysql.sock file.
I also set the default_socket in php.ini.
But it always connect to second one.
The program won't be error, even if I use wrong path of mysql.sock.
This is my code:
<?php
$servername = "127.0.0.1";
$username = "my_username";
$password = "my_password";
$database = "";
$port = "3306";
$socket = "/opt/lampp/var/mysql/mysql.sock";
// Create connection
$conn = new mysqli($servername, $username, $password, $database, $port, $socket);
//Check the host info
echo $conn->host_info."\n";
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$conn->query("set character set utf8");
//Which socket did I connect?
$result = $conn->query("SHOW VARIABLES WHERE Variable_name = 'socket';");
$row = mysqli_fetch_assoc($result);
echo $row['Value']."\n";
//Check the default_socket in php.ini file
echo ini_get("mysqli.default_socket");
?>
The output is:
127.0.0.1 via TCP/IP
/var/run/mysqld/mysqld.sock
/opt/lampp/var/mysql/mysql.sock
How to solve this problem?
Update:
ls -l /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock
Output is
srwxrwxrwx 1 mysql mysql 0 7月 13 18:10 /opt/lampp/var/mysql/mysql.sock
srwxrwxrwx 1 mysql mysql 0 7月 6 21:06 /var/run/mysqld/mysqld.sock
By passing "127.0.0.1" as the first argument to the mysqli constructor, you force mysqli to connect to the MySQL server over TCP. This causes the socket argument to be ignored.
To force the connection to be made over a UNIX socket, pass NULL or "localhost" as the first argument.

connection error while using php and sql server 2008

Connection failed: 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.
as i establish connection between my computer and remote machine using "php" and sql server 2008 respectively. i got this error.
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "complaint";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
You must have a defined servername, username and password. If the server is local than use "localhost" if it is a remote server use IP "X.X.X.X"

c9 access a mysql database from another project

I have one project running a database on a Cloud9 project. I have created a mobile app which will need to connect to the database by POST to a PHP file which will then run the following code:
$servername = "myusername-projectname-somenumber";
$port = 3306;
$username = "form";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, 3306);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
I got the servername by using this in the terminal:
SHOW VARIABLES WHERE Variable_name = 'hostname';
I am getting an error from the other project:
Connection failed: Unknown MySQL server host 'myusername-projectname-somenumber' (0)
***Note: I have replaced myusername-projectname-somenumber and dbname for posting this. I did not forget to fill them out.
Thanks for the help!

connect remotly to mysql server with php

I can connect to mysql server with ssh .
# mysql -u username -h 185.2.3.80 -ppasword
output is :
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 280
Server version: 5.1.61 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
I want to connect to mysql server with php :
<?php
$servername = "185.2.3.80";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
this is output :
Could not connect: Can't connect to MySQL server on '185.2.3.80' (13)
I turn off firewall with iptables and remove bind-address and skip-networking from my.cnf .but can not connect with php .
try it
<?php
$servername = "185.2.3.80:<server Port>";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
write your server port in my case it is 3306.
Do one thing more go to your Cpanel -> remote mysql then add a percentage wild card to allow online access to your database.
You forgot the database name.
First set your database name in a variable
$databaseName = '/* The name of your database */';
Try this :
$conn = new mysqli($servername, $username, $password, $databaseName);
Refer to the documentation
I found solution.ssh can connect to mysql server but httpd can not connect mysql over network.I make httpd to can connect to mysql server over the network with :
sudo setsebool -P httpd_can_network_connect=1

Categories