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.
Related
I've just made a database on mysql on my server. I want to connect to this via my website using php. This is the contents of my connections file:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to mysql');
$dbname = 'epub';
mysql_select_db($dbname);
I know what the username/passwords are, and I know the IP address of the server. What I'm just wondering is how do I know which port to use?
If your MySQL server runs on default settings, you don't need to specify that.
Default MySQL port is 3306.
[updated to show mysql_error() usage]
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to mysql: '.mysql_error());
For windows, If you want to know the port number of your local host on which Mysql is running you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'port';
mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
It will give you the port number on which MySQL is running.
check this out dude
<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
if you want to have your port as a variable, you can write php like this:
$username = user;
$password = pw;
$host = 127.0.0.1;
$database = dbname;
$port = 3308;
$conn = mysql_connect($host.':'.$port, $username, $password);
$db=mysql_select_db($database,$conn);
If you specify 'localhost' the client libs default to using the filesystem system socket on a Unix system - trying the mysql_default_socket value from php.ini (if set) then the my.cnf value.
If you connect using a different tool, try issuing the command "show variables like '%socket%'"
If you want to use a network port (which is a wee bit slower) then try specifying 127.0.0.1 or a physical interface asociated with the machine.
default port of mysql is 3306
default pot of sql server is 1433
This is a PDO-only visualization, as the mysql_* library is deprecated.
<?php
// Begin Vault (this is in a vault, not actually hard-coded)
$host="hostname";
$username="GuySmiley";
$password="thePassword";
$dbname="dbname";
$port="3306";
// End Vault
try {
$dbh = new PDO("mysql:host=$host;port=$port;dbname=$dbname;charset=utf8", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "I am connected.<br/>";
// ... continue with your code
// PDO closes connection at end of script
} catch (PDOException $e) {
echo 'PDO Exception: ' . $e->getMessage();
exit();
}
?>
Note that this OP Question appeared not to be about port numbers afterall. If you are using the default port of 3306 always, then consider removing it from the uri, that is, remove the port=$port; part.
If you often change ports, consider the above port usage for more maintainability having changes made to the $port variable.
Some likely errors returned from above:
PDO Exception: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
PDO Exception: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
In the below error, we are at least getting closer, after changing our connect information:
PDO Exception: SQLSTATE[HY000] [1045] Access denied for user 'GuySmiley'#'localhost' (using password: YES)
After further changes, we are really close now, but not quite:
PDO Exception: SQLSTATE[HY000] [1049] Unknown database 'mustard'
From the Manual on PDO Connections:
port number 3306 is used for MySQL and tomcat using 8080 port.more port numbers are available for run the servers or software whatever may be for our instant compilation..8080 is default for number so only we are getting port error in eclipse IDE. jvm and tomcat always prefer the 8080.3306 is default port number for MySQL.So only do not want to mention every time as "localhost:3306"
<?php
$dbhost = 'localhost:3306';
//3306 default port number $dbhost='localhost'; is enough to specify the port number
//when we are utilizing xammp default port number is 8080.
$dbuser = 'root';
$dbpass = '';
$db='users';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$db) or die ("could not connect to mysql");
// mysqli_select_db("users") or die ("no database");
if(! $conn ) {
die('Could not connect: ' . mysqli_error($conn));
}else{
echo 'Connected successfully';
}
?>
try
$conn = mysql_connect($host, $username, $password, $port);
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";
?>
I cant figure out why i am not connecting to my db. It seems like it is finding the host but having authentication problems or something. I am using xampp and the db provided with it. My connect code is as follows
<?php
$servername = 'localhost:1234';
$username = 'protech';
$password = '';
$dbname = 'protech';
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else{
echo "Connected successfully";
}
mysqli_close($conn);
exit;
?>
if you need any information I am by my computer and will respond quick.
I get these error messages after waiting a while.
Warning: mysqli::__construct(): MySQL server has gone away in C:\xampp\htdocs\protech_connect.php on line 9
Warning: mysqli::__construct(): Error while reading greeting packet. PID=7764 in C:\xampp\htdocs\protech_connect.php on line 9
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone away in C:\xampp\htdocs\protech_connect.php on line 9
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\protech_connect.php on line 9
The standard port for mySQL is 3306 - so if you are genuinely using 1234 then append that as a new parameter to the db connection constructor.
$dbhost = 'localhost';
$dbuser = 'protech';
$dbpwd = 'xxx';
$dbname = 'protech';
$dbport = 1234;
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname, $dbport );
if( $conn->connect_error ) exit( 'Bad foo: '.$conn->connect_error );
else { /* do exciting things */ }
$conn->close();
The mysqli_connect() function doesn't accept a port in the host parameter. You need to add the port number in another parameter like so:
$conn = mysqli_connect($servername, $username, $password, $dbname, $serverport);
Function documentation: mysqli::__construct()
If you get following error or problem then follow above solution.
Error:
Warning: mysqli_connect(): MySQL server has gone away
Warning: mysqli_connect(): Error while reading greeting packet. PID=3528
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away
Solution 1:
use 127.0.0.1 instead of localhost. Don't use any port number after 127.0.0.1.
Solution 2:
Check your host file located at
C:\Windows\System32\drivers\etc
and remove any localhost or 127.0.0.1 entry in file and save. You can use any HostEditor program to edit this file.
I am getting this error:
Server Response='12154 ORA-12154: TNS:could not resolve the connect identifier specified
I am on Ubuntu 14.04
My environment variables are:
ORACLE_HOME = /usr/lib/oracle/12.1/client64
LD_LIBRARY_PATH = /usr/lib/oracle/12.1/client64/lib
TNS_ADMIN = /usr/lib/oracle/12.1/client64/network/admin
tnsnames.ora and sqlnet.ora are within /usr/lib/oracle/12.1/client64/network/admin
PS: I can connect through sqlplus with:
sqlplus64 user/pass#dbname
This is the code:
<?php
$conn = oci_connect('user', 'pass', 'dbname');
?>
It never worked on that way, what I did was to use Easy Connect string:
$conn = oci_connect('user', 'pass', 'host/servicename'); however the first way should have worked because in another environments it works.
tnsnames.ora file should look as follows
DBNAME=
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=YOUR IP ADDRESS)
(PORT=YOUR PORT NUMBER)
)
(CONNECT_DATA=
(SERVICE_NAME=YOUR DBNAMEPROD)
)
)
conn.php file should look as follows
$USERNAME = "hr"; // Login Username
$PASSWORD = "hr"; // Login Passowrd
$DATABASE = "DBNAME"; // Connect string to connect to your database found in tnsnames.ora
$conn = oci_connect($USERNAME, $PASSWORD, $DATABASE);
if(!$conn){
echo "Your Connection Has an error";
}
else{
echo "Your Connection is Successful"
}
I've just made a database on mysql on my server. I want to connect to this via my website using php. This is the contents of my connections file:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to mysql');
$dbname = 'epub';
mysql_select_db($dbname);
I know what the username/passwords are, and I know the IP address of the server. What I'm just wondering is how do I know which port to use?
If your MySQL server runs on default settings, you don't need to specify that.
Default MySQL port is 3306.
[updated to show mysql_error() usage]
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to mysql: '.mysql_error());
For windows, If you want to know the port number of your local host on which Mysql is running you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'port';
mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
It will give you the port number on which MySQL is running.
check this out dude
<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
if you want to have your port as a variable, you can write php like this:
$username = user;
$password = pw;
$host = 127.0.0.1;
$database = dbname;
$port = 3308;
$conn = mysql_connect($host.':'.$port, $username, $password);
$db=mysql_select_db($database,$conn);
If you specify 'localhost' the client libs default to using the filesystem system socket on a Unix system - trying the mysql_default_socket value from php.ini (if set) then the my.cnf value.
If you connect using a different tool, try issuing the command "show variables like '%socket%'"
If you want to use a network port (which is a wee bit slower) then try specifying 127.0.0.1 or a physical interface asociated with the machine.
default port of mysql is 3306
default pot of sql server is 1433
This is a PDO-only visualization, as the mysql_* library is deprecated.
<?php
// Begin Vault (this is in a vault, not actually hard-coded)
$host="hostname";
$username="GuySmiley";
$password="thePassword";
$dbname="dbname";
$port="3306";
// End Vault
try {
$dbh = new PDO("mysql:host=$host;port=$port;dbname=$dbname;charset=utf8", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "I am connected.<br/>";
// ... continue with your code
// PDO closes connection at end of script
} catch (PDOException $e) {
echo 'PDO Exception: ' . $e->getMessage();
exit();
}
?>
Note that this OP Question appeared not to be about port numbers afterall. If you are using the default port of 3306 always, then consider removing it from the uri, that is, remove the port=$port; part.
If you often change ports, consider the above port usage for more maintainability having changes made to the $port variable.
Some likely errors returned from above:
PDO Exception: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
PDO Exception: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
In the below error, we are at least getting closer, after changing our connect information:
PDO Exception: SQLSTATE[HY000] [1045] Access denied for user 'GuySmiley'#'localhost' (using password: YES)
After further changes, we are really close now, but not quite:
PDO Exception: SQLSTATE[HY000] [1049] Unknown database 'mustard'
From the Manual on PDO Connections:
port number 3306 is used for MySQL and tomcat using 8080 port.more port numbers are available for run the servers or software whatever may be for our instant compilation..8080 is default for number so only we are getting port error in eclipse IDE. jvm and tomcat always prefer the 8080.3306 is default port number for MySQL.So only do not want to mention every time as "localhost:3306"
<?php
$dbhost = 'localhost:3306';
//3306 default port number $dbhost='localhost'; is enough to specify the port number
//when we are utilizing xammp default port number is 8080.
$dbuser = 'root';
$dbpass = '';
$db='users';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$db) or die ("could not connect to mysql");
// mysqli_select_db("users") or die ("no database");
if(! $conn ) {
die('Could not connect: ' . mysqli_error($conn));
}else{
echo 'Connected successfully';
}
?>
try
$conn = mysql_connect($host, $username, $password, $port);