Fix configuration problems with MacPorts MySQL - mysql

I have mysql/mysqli installed on my local machine via MacPorts and can access mysql/mysqli commands from the command line ONLY when using locahost.
If I try running:
mysql -u root -h 127.0.0.1 -p
I get the following error:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
When running:
mysql -u root -h localhost -p
Everything is fine.
The reason this bothers me is that I am trying to use mysqli_connect in a PHP script running on localhost. When I enter the host as localhost, it gives me this error:
Warning: mysqli_connect(): (HY000/2002): No such file or directory in ... on line 2
bool(false) Failed to connect to MySQL: No such file or directory
And when I enter the host as 127.0.0.1, I get a different error:
Warning: mysqli_connect(): (HY000/2002): Connection refused in /Users/raph/coding/webroot/rugapp/qbo-palmer/docs/partner_platform/example_app_ipp_v3/test.php on line 2
bool(false) Failed to connect to MySQL: Connection refused
It seems as though I have some configuration problems. What is the best way to fix them?

The connection to localhost uses a unix socket. It seems the correct socket path is built into the mysql binary you use, which explains why it works on the command line. For the unix socket connection to work from PHP, you need to specify MacPorts MySQL's non-standard socket path as you would normally specify a port number.
For the failure to connect using 127.0.0.1 (which forces IPv4 and avoids the unix socket), see mysql running but cannot connect to 127.0.0.1.

Related

Nothing will connect to mysql on localhost

I've tried just about everything I can find online. I'm going to try to make a complete list of all of the things that I have tried in no particular order (as it's been about two weeks of hunting for solutions). Where it all starts is my formerly functioning local dev setup was working fine aside from some SQL settings that were interfering with my already established codebase. I found resources online that detailed editing my.cnf file; which I did. From that point forward, nothing will connect to mysql.
1. Uninstall and remove mysql files via terminal
2. Reinstalled mysql and attempted to connect via terminal, didn't work
Running via terminal: mysql -u root
Resulting socket error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
3. Connected to services via DBngin
All services start up and seem to run fine; (Homebrew svcs: php, mysql, httpd). All show a green light and appear to be functioning.
4. Attempt to connect to mySQL via TablePlus
These settings worked before I messed with my.cnf file as described initially (no password):
Host: 127.0.0.1
Port: 3306
User: root
Resulting error: Can't connect to MySQL server on '127.0.0.1' (61)
5. Installed MySQL Workbench / Server, won't connect
Resulting error: SQLSTATE[HY000] [2002] Connection refused
6. Any attempt to load localhost:3306 in Chrome/Safari/Firefox all result in the following
SQLSTATE[HY000] [2002] Connection refused
7. See if mySQL is running
Run in terminal: ps aux |grep mysql
Results in: natashya 74640 0.0 0.0 34132100 908 s002 S+ 1:40pm 0:00.00 grep mysql
Try to kill via: kill 74640
Results in: kill: kill 74640 failed: no such process
Run: brew services restart mysql
Results in: ==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql) ==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
Contents of my.cnf
# Default Homebrew MySQL server config
# Only allow connections from localhost
[mysqld]
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
port = 3306
mysqlx-port = 33060
socket = /var/lib/mysql/mysql.sock
datadir = /usr/local/mysql
tmpdir = /tmp
mysqlx=OFF
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
Thank you so much to Bill Karwin for help. I am however still unable to resolve the site I'm attempting to access (localhost:9000) and get this error: SQLSTATE[HY000] [2002] Connection refused
Redux of the comment thread above:
Check the MySQL Server error log for clues.
The log indicated the directory for the socket files, /var/lib/mysql, did not exist.
Once the directory was created with the right permissions, the MySQL Server process created the socket file.
Also:
ps aux | grep mysql matches the process running grep mysql because the word 'mysql' appears in that command.

How to connect to MySQL with sockets via terminal?

My /etc/hosts file has 127.0.0.1 localhost line. When I try to connect to MySQL with mysql -u root -h localhost -p I get the ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) error. However, when I connect using 127.0.0.1, it works fine or using localhost with --protocol=TCP.
I read here that Unix treats host name localhost special and uses socket file instead of TCP/IP. Is it possible to connect to MySQL with socket file via terminal to not allow connections via the network?

Wordpress local raspbian installation : MySQL crashes once a minute

I've found many similar posts, however I wasn't able to find out a solution, so here is my question.
I've been running since more than one month a local wprdpress installation on Raspbian.
Since yesterday, I always get an error while attempting to tonnect to the website (WP debug mode is ON) :
Warning: mysqli_real_connect(): (HY000/2003): Can't connect to MySQL server on '127.0.0.1' (111) in /var/www/html/wp-includes/wp-db.php on line 1488
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/wp-includes/wp-db.php on line 1518
Warning: mysql_connect(): Can't connect to MySQL server on '127.0.0.1' (111) in /var/www/html/wp-includes/wp-db.php on line 1518
Trying to know more using the command : mysqladmin -u root -p status
gives me :
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
I chmoded the folders involved to 755. I don't use localhost to name my DB (in wp-config.php, in my.cnf) but 127.0.0.1.
If I restart the MySQL service, it works for more or less one minute, then stops again. Using safe-mode didn't work either.
I think this is a socket problem but how can I solve it ?

Wordpress localhost OSX installation: Error establishing a database connection

I have a problem with Wordpress config on my localhost nginx.
On wp-admin configuration i receive Error establishing a database connection.
When I create wp-config manually, in mysql logs I have only Aborted connection 12 to db: 'unconnected' user: 'dimmi' host: 'localhost' (Got an error reading communication packets)
This is my debug log:
Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /Users/patryk/Public/dimmi/dimmi_wordpress2/wp-includes/wp-db.php on line 1488
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /Users/patryk/Public/dimmi/dimmi_wordpress2/wp-includes/wp-db.php on line 1518
Warning: mysql_connect(): No such file or directory in /Users/patryk/Public/dimmi/dimmi_wordpress2/wp-includes/wp-db.php on line 1518`
Command line login and simple PHP script work perfectly, file permissions are 777.
I have OSX El Captain, mysql installed using homebrew. I ran out of ideas :/
The problem is in connection between php and mysql.pls check following;
1.Check user name , password is correct
2.check the user 'dimmi' have permission for host 'localhost'
3.is mysql running ? (since you can login from terminal,mysql is running )
4.try to telnet localhost 3306
(this will return
"Connected to localhost.
Escape character is '^]' " if mysql is running on your machine in 3306)

Concrete5 SQLSTATE[HY000] [2002] No such file or directory on localhost

I'm trying to install Concrete5 on a localhost using MAMP, but every time I put it the database information, I get this error.
SQLSTATE[HY000] [2002] No such file or directory
How can I solve this problem?
Error 2002 is typically displayed as
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/path/to/mysqld/mysqld.sock'
PHP can't connect to MySQL using a socket. On windows you probably don't want to use unix socket.
"But I'm not using unix sockets, I'm simply connecting to localhost" you might say.
For the PHP driver 'localhost' has a special meaning as described in the PHP manual.
The hostname localhost has a special meaning. It is bound to the use of Unix domain sockets. It is not possible to open a TCP/IP connection using the hostname localhost you must use 127.0.0.1 instead.
Solution: configure 127.0.0.1 as your MySQL host for Concrete5.
I finally solved this problem. I went into MAMP and found the MySQL port number and typed into the server box in this format: localhost:[MySQL Port].
Try 127.0.0.1:[MySQL port], that did the trick for me.
To find your port number, go to the MySQL section in MAMP and look at the top right corner of the screen. There will be a message saying: MySQL is configured to use port [port number].
I did it on MacOS 10.13 and MAMP 5.1, create a symlink to mysql.sock in /var/mysql (create the directory if it does not exist).
It's solution works global, without change any config, so PHP can connect to MySQL.
Type it in console/terminal:
sudo mkdir /var/mysql
cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock