I get a "Connection Refused" error when I run the following batch file:
#!/usr/bin/php
<?php
mysql_connect('127.0.0.1', 'root', '*******');
?>
where * is a working password.
I've tried localhost too, but I get the same problem.
I know the password is good because I am able to connect just fine using:
> /Applications/MAMP/Library/bin/mysql -u root -p
I can also connect when I run the script from a browser (but I have to change 127.0.0.1 to localhost).
I'm using MAMP v1.8 on a Mac OS Lion.
Try using this line instead
$sqlcon = mysql_connect("localhost", "root", "password") OR die("Could not connect to database: " . mysql_error());
It should show some more info about the error and if not check to make sure that in phpmyadmin(or SQLbuddy) the user has access to databases(it should because the default 'root' should. Also be sure that MAMP is set to online mode.
Also it appears that you missed a ";" at the end of line 2(the line with the connect). Also, you have )) at the end where it should be ); Add that to the end of the line and it should work.
mysql_connect('127.0.0.1', 'root', 'password') OR DIE(mysql_error());
Related
This question already has answers here:
Warning: mysqli_connect(): (HY000/2002): No such file or directory
(6 answers)
Closed 2 years ago.
I am trying to connect to my MySQL server on a mac using PHP after starting the server using brew services start mysql After this I can see that the MySQL server has successfully started. However, when I try to access my webpage using:
<?php
/* Attempt to connect to MySQL database */
$link = mysqli_connect('localhost', 'root', 'password', 'databasename');
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
The webpage start spinning continuously and doesn't load. I was wondering what's the issue as I can log into the root from the command line as well as see the tables. And the bre shows MySQL server running in the background.
EDIT I managed to print my error however, now I get an error:
Warning: mysqli_connect(): (HY000/2002): No such file or directory However, homebrew shows my MySQL server running.
Try to change your host from localhost to 127.0.0.1
$link = mysqli_connect('127.0.0.1', 'root', 'password', 'databasename');
You better make sure if mysqli extension is available in php.ini
(If it is not enabled, go into the php.ini file and remove ; from the mysqli line.)
If mysqlit is enabled, try running the code below and restart the server/terminal php task if anything changes
cd /var mkdir mysql cd mysql ln -s /tmp/mysql.sock mysql.sock
Check your credentials you give into the mysqli_connect
If everything is okay, and you cant still connect try installing helpful programs like MAMP. They will create an Apache Server with Php and MySql server on local computer without a problem with full configuration
I have this problem, i have created a php file which inserts data into a mysqldatabase on Mamp.
I can use the command mysql anywhere and it starts the mysql client.
But when i start my php script i get this error:
Warning: Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running? in
For testing i have used the default username and password "root". Anyone know what causes this?
I have also tried this command:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
For me it is working fine. Which hostname you are using in your php code?
sgeorge-mn:~ sgeorge$ cat con_mysql.php
<?php
$con = mysql_connect("127.0.0.1:3306","root","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Connection Established\n";
}
mysql_close($con);
?>
sgeorge-mn:~ sgeorge$ php con_mysql.php
Connection Established
I am having this error:
Warning: mysql_connect(): [2002] Connection refused (trying to connect via tcp://127.0.0.1:3306)
I saw a similar question and I tried the solution by changing localhost to 127.0.0.1, but it is still not working. I am using Mac OS X Lion. Please any help would be appreciated
This is the PHP code I use to connect:
<?php
require("constants.php");
// open database
$connect = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die('error connecting: ' . mysql_error());
// select database
mysql_select_db(DB_NAME) or die('error selecting db: ' . mysql_error());
?>
Try making sure your firewall if open to connections on port 3306.
The command should be ipfw set enable 3306, but if you want you can read more about actually setting the firewall permissions.
Another thing could be to make sure MySQL is listening on port 3306, edit the my.ini file and set
[client]
port=3306
And then restart MySQL.
I've had that problem before. You can fix it by commenting out "skip-networking" in the configuration. (my.ini or my.cnf, it's /etc/mysql/my.cnf on Linux, not sure about Mac)
-- edit
Also, you can try typing "netstat" in the Terminal, to make sure that mysql is listed there (should be something like 127.0.0.1:mysql or 127.0.0.1:3306). If it is not listed, MySQL either hasn't started, or hasn't been able to bind.
I am new to PHP and MySQL and have recently installed both PHP v5.3.10 and MySQL v 5.5.21 on a Windows Server 2003 server already running IIS v6.
PHP runs and I have created a database on MySQL from the MySQL 5.5 Command Line Client.
However, when I try to access the database from PHP with the following commands:
echo "Open database";
$link = mysql_connect($host, $username, $password);
echo " link: $link";
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $link)or die("cannot select DB");
echo " database open";
I get the following returned to the web page:
Open database
No error messages are produced and nothing after the mysql_connect command is returned from
the PHP to the screen.
Also tried line below which also did not return any error messages:
mysql_connect($host, $username, $password) or die('Cannot connect:' . mysql_error());
Anybody have any ideas why I can't make a connection and can't get an error message back from mysql_connect command?
I have checked MySQL and I have tried defining the host as %, localhost, the local host IP and the IP:port number (from the port number 3306 listed in the my.ini) to no effect.
I only have one username of 'root' created in mySQL with a single password (which I used when I opened MySQL to create the database)
The 'php.ini' I have placed in both the 'C:\Program Files\PHP' and 'C:\WINDOWS'.
This file contains 'extension_dir = "C:\Program Files\PHP\ext"' to specify the extension directory and includes the following at the end of the file:
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll
[PHP_MYSQL]
extension=php_mysql.dll
[PHP_MYSQLI]
extension=php_mysqli.dll
Also I tried running phpinfo() and it returned the following table for mysqlnd:
mysqlnd enabled
Version mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $
Compression supported
SSL supported
Command buffer size 4096
Read buffer size 32768
Read timeout 31536000
Collecting statistics Yes
Collecting memory statistics No
Tracing n/a
So Is I assume the php should be able to connect.
Try this:
ini_set('display_errors',1);
error_reporting(E_ALL);
To mirror #Jrod:
is error_reporting on?
have you checked the error_log for the instance?
It sounds like something's failing and your error reporting isn't set high enough to show it. You might try addin this to the top of the script:
error_reporting(E_ALL);
I'm using PHP in order to connect to MySQL with the following way:
$link = mysql_connect('...host...', '...username...', '...password...');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
and it connects just fine.
But when trying from my terminal this
mysql -h ...host... -u ....username... -p ...password...
I give my password and i take as a result this:
ERROR 2003 (HY000): Can't connect to MySQL server on '...host...' (111)
Any ideas how this can be solved?
Try sudo mysql -h ....etc.
I'm running linuxMint and had to use either su or sudo. I think by default you have to be root to enter the terminal. But with Ubuntu root is inaccessible, the only feature I don't like about Ubuntu.
The php code is running on the web server, your local command is running on your machine.
MySQL accounts are not just usernames - they are username + hostname combos. So both have to match for MySQL to find the account.
Since your hostname is different from the one the web server uses MySQL does not consider it a valid user. (It's possible to set the hostname to % which means "Any".)