"Can't connect to local MySQL server through socket" on linux server - php

I'm having a hard time getting my website connected to the mySQL database on host. However, when I was running my website on the PC by Apache it was connecting smoothly. The snippet I'm using to connect is:
<?php
$conn_error = 'Could not connect';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'firstdatabase';
if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass)|| !#mysql_select_db($mysql_db)){
die($conn_error);
}else{
//echo 'Connected';
}
?>
And it gives me the following error when I try to connect to mysql on the web host:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server
through socket 'MySQL' (2) in /home/umudo/public_html/connectserver.inc.php on line 10
I searched through the web for finding a solution but couldn't find any exact example of this.

Help: http://dev.mysql.com/doc/refman/5.0/en/connecting.html
Says:
On Unix, MySQL programs treat the host name localhost specially, in a
way that is likely different from what you expect compared to other
network-based programs. For connections to localhost, MySQL programs
attempt to connect to the local server by using a Unix socket file.
So you have to
use "127.0.0.1" instead of "localhost" to use tcpip instead of sockets, or
enable sockets in mysql server config (socket connections are probably disabled and that causes that error on linux)
Second method is better - using sockets for local connections is better than TCP/IP (better performance), but you may have no privileges to mysql configuration on that server.

Try installing phpmyadmin or php5-mysql as in apt-get install phpmyadmin.

Related

How to configure XAMPP to connect to MySQL remotely

I have a problem to connect my online MySQL database from my local system.
I installed XAMPP on my Windows 7 and created a PHP file with this code:
<?php
$db_path = mysqli_connect('printcity24.com', 'printci1_admin', 'xr10s20191', 'printci1_db', '3306');
if(!$db_path) {
echo mysqli_connect_error();
}else{
echo "Connected successfully";
}
?>
Then i created a database on my website : www.printcity24.com
My web host admin configured my host and opened firewall.
When i use XAMPP command line to connect to my database every thing is ok, i can connect to my database remotely with this code :
# mysql -u printci1_admin -p -h printcity24.com
but when i use php code to connect to my database i get this error :
Warning: mysqli_connect(): MySQL server has gone away in D:\Xampp Server\htdocs\st\index.php on line 2
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in D:\Xampp Server\htdocs\st\index.php on line 2
MySQL server has gone away
I upload my php code on to other websites and test for connection and everything is "ok" but on my local xampp can't connect.
In this link said i have to use this command :
setsebool -P httpd_can_network_connect=1
but I don't know where to put this code and how to configure my xampp.

php pdo in linux, not able to connect to remote mysql server but in wamp it's working fine [duplicate]

I am facing a weird problem here
we have a server A where the app files are stored
and B server with database
Tried to connect via command prompt from server A to B using the command
mysql -h xx.xx.xx.xx -u root -p password - and it worked
NOw i tried to create a php script in server A to connect to server B
the command is
$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password');
Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL
server on 'xx.xx.xx.xx' (13) Fatal error: Uncaught exception
'Exception' with message 'SQLSTATE[HY000] [2003] Can't connect to
MySQL server on 'xx.xx.xx.xx' (13)'
Unable to find a solution on this.
Can any help on this?
thank you
I got it working by running a command in the database server :)
setsebool httpd_can_network_connect_db=1
thanks for the replies yycdev
Try specifying the port in your connection string and ensure the database server is set to allow remote connections and the port is open on your firewall (both of these I suspect are already done as you are able to connect via the terminal but it never hurts to verify and check things).
Change your PDO connection and add the port=3306 or if you're using MAMP use port 8889
$this->db=new PDO('mysql:host=xx.xx.xx.xx;port=3306;dbname=databasename','root','password');
Another thing to check is if – SELinux is blocking network connections. Login as root and run
setsebool -P httpd_can_network_connect=1
I don't know much about that, but try to place the port in back of the script:
$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password',3306);

php 5.4 mssql_connect suddenly stopped working

Environment:
Apache 2.2.9
PHP 5.4.37
freetds 0.91
SQL Server 2008
After last night's round of updates applied from Microsoft my mssql_connect stopped working.
tsql -LH x.x.x.x still shows the server and port correctly
tsql -H x.x.x.x -U user connects fine
I get nothing in the apache log files or the freetds log
Here is the code segment that is failing (critical info changed)
error_reporting(E_ALL); ini_set('display_errors',1);
$con = mssql_connect('server', 'user', 'pass');
var_dump($con);
if (!$con) {
if ( function_exists('error_get_last') ) {
var_dump(error_get_last());
}
die('Could not connect to the server!!'.mssql_get_last_message());
}
And here is the result:
Warning: mssql_connect(): Unable to connect to server: server in /home/....../public_html/states/index.php on line 25
bool(false)array(4) { ["type"]=> int(2) ["message"]=> string(55) "mssql_connect(): Unable to connect to server: server"["file"]=> string(63) "/home/....../public_html/states/index.php"["line"]=> int(25) } Could not connect to the server!!
Please help!
I'm unable to post a comment to ask this, but here is my thoughts.
Obviously it's not connecting to the server outside of the command line, so you'll want to double check its configuration (check the server port, specify the port, etc..)
Is this running from a Win or Unix type OS?
It is Linux. I found a work around for the moment. The problem appears to be on the Windows side where, although a tsql -LH command shows the IP and port, it is not actually answering queries on the port. You get connected to the server via TCP and that is it.
Because the SQL server is multi-homed, I added a secondary connection to the web app server that connects to it over the primary IP.
I'll chase down the IP binding and service issue later. Or maybe just convert it all to mariaDB or Postgres.

Connect PHP to AS400 with ODBC or DB2

I'm trying to connect my WEB server to AS400. The web server has not DB2 library neither ODBC library.
I have installed XAMPP in my Windows computer, and one of my colleagues also did.
He has Client Access on his Computer.
We both installed:
- XAMPP
- ibm_data_server_driver_package_win32_v10.5.exe
We tried to install PECL DB2 extension (LINK) but unsuccessfully (got error ".\php.exe appears to have a suffix .exe, but config variable php": seems that nobody has solved this problem on Windows...).
Then we saw that XAMPP has the ODBC Module already on it, so we tried to estabilish a connection with obdc_connect. Referring to THIS question we are now able to connect to AS400 using his computer with Client Access Drivers using:
$user = 'USER';
$password = 'PASS';
$hostname = '192.168.1.30';
$server="Driver={Client Access ODBC Driver (32-bit)};
System=$hostname;
Uid=$user;
Pwd=$password;";
odbc_connect($server, $user, $password);
With my computer I tried to use the IBM data server driver already installed using:
$user = 'USER';
$password = 'PASS';
$hostname = '192.168.1.30';
$server="Driver={IBM DB2 ODBC DRIVER};
System=$hostname;
Uid=$user;
Pwd=$password;";
odbc_connect($server, $user, $password);
and I get always this error:
Warning: odbc_connect(): in C:\xampp\htdocs\test.php on line 11
When I've tried with IBM DB2 ODBC DRIVER on my colleague's computer, I've also got the same error.
What is this error? No information is specified.
We would like to use db2_connect instead of odbc_connect. What should we do to install this extension? I asked my ISP to install the db2 extension on the WEB server but I'm still waiting... maybe he also encountered some problems (the WEB server is a UNIX machine).
Any help is much apprecciated!

PHP: Unknown MySQL server host with mysqli

I have the following code:
$username = 'username';
$password = 'password';
$host = 'localhost';
$db = 'dbname';
$dbh = new mysqli('p:'.$host, $username, $password, $db)
or die('no connection to server');
But I get the following error
PHP Warning: mysqli::mysqli() [<a href='function.mysqli-mysqli'>function.mysqli-mysqli</a>]: (HY000/2005): Unknown MySQL server host 'p:localhost' (1) in /var/www/vhosts/politiker.lu/httpdocs/includes/sql.php on line 8
This intrigues me because the very same code worked on my development environment. Now that I want to set it up on my production server, it does not work. Any ideas?
Note: I am not very good with servers, so excuse me if the error should be something trivial.
EDIT 1
Here are the versions:
Development PHP (5.3.3-1ubuntu9.1) + MySQL(5.1.49-1ubuntu8.1)
Production PHP (5.2.4-2ubuntu5.12) + MySQL(5.0.51a)
take out the p: like this:
$dbh = new mysqli($host, $username, $password, $db)
or die('no connection to server');
Just adding this since I didn't think it was clear, but the problem is definitely your production version of PHP. Persistent connections for the mysqli extensions weren't added until 5.3 since they caused headaches before.
Some hosts might have different versions of PHP installed... I realized today that I can make HostGator use PHP 5.3! I just had to add to my .htaccess file:
Action application/x-hg-php53 /cgi-sys/php53
AddType application/x-hg-php53 .php
i think you missed the mysqli package in this server. Try :
sudo apt-get install php5-mysqli
if apt-get says it is already installed try to do a ping to localhost, if don't resolve edit /etc/hosts and check the line where localhost points to 127.0.0.1
should be there, if not, your server its horribly configured!
do a
ifconfig
and check if you get some output like this...
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
You need to specify the actual location of the MySQL server. For development, it is localhost (most likely), just how you have it configured. But for production servers, it is going to be something else. I doubt it should be localhost.

Categories