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
Related
I'm trying to get a local development environment set up. I can't get wordpress to connect to mysql. I can duplicate the error with the following command:
wp core install --url=http://uganda.localhost/ --title="Uganda Aid" --admin_user="Jack" --admin_password="thepassword" --admin_email="JackWinterstein#msf.org"
Result:
Error: Error establishing a database connection. This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `localhost`. This could mean your host’s database server is down.
Things I tried:
Looked in wp-config and made sure the database name and credentials were correct. I can connect via cli
Updated homebrew.mxcl.mysql.plist to include --bind-address=*
Looked at the mysql socket location: mysql_config --socket producef /tmp/mysql.sock
Updated php.ini to reflect the attached below
Updated my.cnf to reflect bind-address = *
I am using dnsmasq (as shown in https://medium.com/#charlesthk/wordpress-on-os-x-with-nginx-php-mysql-62767a62efc4)
Environment
Darwin osx10.14, mysql Ver 8.0.17, nginx version: nginx/1.17.3, Wordpress 5.2.2, PHP 7.3.9
I figured it out. The problem was that caching_sha2_password authentication method unknown to the client. To fix this I ran:
ALTER USER jack#localhost IDENTIFIED WITH mysql_native_password BY 'thepassword';
I found this by: creating a basic test script:
<?php
$link = mysqli_connect('localhost', 'jack', 'thepassword');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
And the resulting error mentioned the caching_sha2_password authentication method.
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.
I have a simple php file I want to execute in the terminal:
script.php
echo "script is running";
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Connection Established\n";
}
mysql_close($con);
I'm running the script in the terminal on MacOs with php script.php, and I'm getting the following error:
script is running
Warning: mysql_connect(): No such file or directory in
/Applications/MAMP/htdocs/issueManager3/notifications.php on line 5
Could not connect: No such file or directory
However, when I run the script through a web browser, it connects to the database fine.
Am I missing something? Or is there something else I need to set up to achieve database connections through the terminal?
Any help appreciated.
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());
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".)