Can't connect to mysql, have tried a lot of things - php

I've read a ton of threads and solutions, I'm not purposely trying to make yet another one of these posts.
Now that thats out of the way
if i run this in my local osx folder (with web sharing on), i get a could not connect error:
<?php
$db = mysql_connect("localhost", "root", "password");
if (!$db) {
die('Could not connect' . mysql_error());
}
echo 'Connected successfully';
yet when i go to terminal and run the following it works fine:
mysql -u root -h 'localhost' -p
password<enter>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1030
Server version: 5.5.25a MySQL Community Server (GPL)
i have updated php.ini to point to /tmp/mysql.sock as well

If you're just starting out and are using PHP5, you should not use mysql_connect, but instead mysqli_connect (use of the mysql extension is discouraged).
Check connect_error to get more information about why the connection is failing.
(Example taken from the above doc)
<?php
$mysqli = #new mysqli('localhost', 'root', 'password', 'THE DB NAME');
// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
}

Try to display your php configuration with a phpinfo();, you might learn a lot from that.
Also, check the port number used by MySQL, I've seen many example where it wasn't standard on OSX installation.

Related

php mysql connect fails even though shell client succeeds

I have a weird problem.
I'm trying to connect from a linux host to a remote MySQL server on a Windows host.
If I try to connect from shell using the mysql client it works perfectly.
#mysql -h 192.168.x.x -u MyUser -pMyPassword
I've also tried connecting from a simple python script and it works as well.
But if I try to connect from a php script it fails with a "Can't connect to MySQL server on '192.168.x.x' (13)" error.
mysql_connect ('192.168.x.x', 'MyUser', 'MyPassword');
the remote server is running 5.0.41-community-nt on Windows (no info about the OS version)
the client machine is running CentOS 6.2 and is itself equipped with a MySQL 5.1.61 server.
The PHP MySQL module is using the sampe API version 5.1.61
The problem MUST be inside PHP as the connection is successful from the shell client (and even from a python script) but I don't have a clue.
Any help will be higly appreciated.
Use the simple PHP way to connect MySQL:
<?php
$link = mysql_connect('192.168.x.x', 'MyUser', 'MyPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Please, show us the full code.

Default Port required on MySQL Connection

I installed MySQL on my OS X Mountain Lion box and got it to work. Now, I'm having problems getting a new WordPress site to access it and it has been suggested that my server is not configured correctly because I have to supply the port. I created this test code to verify the connection:
<?php
$mysqli = new mysqli('localhost:3306', 'root', 'somepassword', 'gazos');
if ($mysqli->connect_error)
die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
else
echo 'Good connection to gazos';
?>
It returns a good connection. It errors out if I remove the port. I have something similar in another non-WordPress website that works fine. I don't remember configuring MySQL. I just installed it. When I checked my my.cnf file, everything is commented out except:
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
Is it normal to supply the default port on a connection? If not, what do I need to fix.
Try new mysqli('127.0.0.1', 'root', 'somepassword', 'gazos');

PHP, MySQL, Lost connection to MySQL server during query, TurnKey Ubuntu/Linux,

Hi to all php and mysql experts,
I try to find some help or 'tutorial' or similar question/answer for my problem but it was unsuccessful.
I have install TurnKey Linux, php, mysql, successfuly width ip address: 172.##.##.## and I can connect to this server from another comp.
I was edit my my.cnf file, I was open port 3306 and I can connect via mysql -u root -p -h 172.##.##.## from another comp. I was add user:root that can connect from any host or IP address. Also I can make successful connections width mysqladmin or mysql workbench.
I can, also, make database and some tables on this server like db_test, tab_test etc. and if I use next script on this server its work very good:
<?php
$db_hostname="localhost";
$db_username="root";
$db_password="pxxxxx";
$db_connect=mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_connect) {
die('Could not connect: ' . mysql_error());
}
mysql_close($db_connect);
?>
But if I use next script it does not work and report to me error: Could not connect: Lost connection to MySQL server during query;
<?php
$db_hostname="172.##.##.##";
$db_username="root";
$db_password="pxxxxx";
$db_connect=mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_connect) {
die('Could not connect: ' . mysql_error());
}
mysql_close($db_connect);
?>
Also this script it does not work if I use another php server witch can see or ping this server.
Can somebody help me to resolve this problem! Please. I am desperate.
I have the same error, please go to xampp\phpMyAdmin\libraries\config.default.php
Modify the following code
existing one - $cfg['ExecTimeLimit'] = 300;
modify to - $cfg['ExecTimeLimit'] = 600;
This is getting a bit long for a comment....
It doesn't appear to be a connection error - that's a different error message.
Changing the configuration of PHPMyAdmin is not going to make any difference to other scripts.
It might help if you publish details of the mysql config from PHP. Also is your mysqld started with skip-name-resolve? How long does your example script take to fail?
One thing to watch for is that when the mysql client sees 'localhost' in the conection string, it tries to connect via a filesystem socket - not a network socket - do you get the same error when you supply the host as 172.##.##.## ?

php and mysql connect issues on windows server / IIS6

I installed php and mysql on a Windows 2003 server running IIS6. I uncommented the lines for the mysql and mysqli extensions. I am able to run phpinfo, and am seeing the mysql and mysqli sections in phpinfo.
I then run the following code to attempt a connection to the database -
<?php
$link = mysql_connect('localhost', 'root', 'mypassword');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
When I attempt to load this through a browser, I am getting a generic 500 server error. I don't know where else to look to troubleshoot this issue. Can someone point me in the right direction?
I am also able to access the mysql database using mysql workbench on the server.
Thanks for any thoughts.
I solved this by referring to this post - PHP has encountered an Access Violation at 77FCAFF8
Ultimately, I uninstalled MySql and then reinstalled at the root of my filesystem in order to eliminate any spaces in the path. I then recycled my application pools, and am now able to connect.
thanks.

mysql connect not working with PHP

Any idea why the following code is not working
mysql credentials are correct, have verified from command line.
The second connection fails i.e., $conn1 . I am clueless !!!
$conn = mysql_connect($hostname,
$username, $password)
or die("Connecting to MySQL failed" . mysql_error());
$conn1 = mysql_connect($hostname,
$username1, $password1)
or die("Connecting to MySQL failed" . mysql_error());
Test it with hardcoded values first to be sure...
//Connect to database from here
$link = mysql_connect("localhost", "dbaadmin", "sqlpassword");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
have verified from command line.
There's so many questions here that you have not answered.
The command line where the PHP script is running?
The same chroot environment where the PHP script is running?
The OS user (not database user) is the same as the webserver runs as?
The hostname you are connecting to is on a seperate machine from where the PHP script (and the CLI) is running? (localhost will use a unix domain socket rather than a network socket)
...and just what is the error message you get?
C.
It's a guess, but is the second connection supposed to be to $hostname, or perhaps $hostname1 instead.

Categories