I\'m trying to connect to my MySQL DB with the Terminal on my Apple (With PHP).
Yesterday it worked fine, and now I suddenly get the error in the title.
The
You can do it by simply aliasing the MAMP php on Apple terminal:
alias phpmamp='/Applications/MAMP/bin/php/php7.0.0/bin/php'
Example: > phpmamp - v
Now you can run something like: > phpmamp scriptname.php
Note: This will be applied only for the current terminal session.
When you install php53-mysql using port it returns the following message which is the solution to this problem:
To use mysqlnd with a local MySQL server, edit /opt/local/etc/php53/php.ini
and set mysql.default_socket, mysqli.default_socket and
pdo_mysql.default_socket to the path to your MySQL server's socket file.
For mysql5, use /opt/local/var/run/mysql5/mysqld.sock
For mysql51, use /opt/local/var/run/mysql51/mysqld.sock
For mysql55, use /opt/local/var/run/mysql55/mysqld.sock
For mariadb, use /opt/local/var/run/mariadb/mysqld.sock
For percona, use /opt/local/var/run/percona/mysqld.sock
Since your might use MAMP, either change your Port to the default 3306 or use 127.0.0.1 in the database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',// leave it for port 3306
'username' => 'yourUserhere',
'password' => 'yourPassword',
'database' => 'yourDatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Or with the default settings:
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1:8889',// leave it for port 8889
'username' => 'yourUserhere',
'password' => 'yourPassword',
'database' => 'yourDatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
When you face the following issue:
PHP throwing error "Warning: mysql_connect() http://function.mysql-connect: 2002 No such file or directory (trying to connect via unix:///tmp/mysql.sock)"
Set "mysql.default_socket" value in your /etc/php.ini
to
"mysql.default_socket = /var/mysql/mysql.sock".
Then restart web service in server admin
MySQL socket is located, in general, in /tmp/mysql.sock
or /var/mysql/mysql.sock
, but probably PHP looks in the wrong place.
Check where is your socket with:
sudo /usr/libexec/locate.updatedb
When the updatedb is terminated:
locate mysql.sock
Then locate your php.ini:
php -i | grep php.ini
this will output something like:
Configuration File (php.ini) Path => /opt/local/etc/php54
Loaded Configuration File => /opt/local/etc/php54/php.ini
Edit your php.ini
sudo vim /opt/local/etc/php54/php.ini
Change the lines:
pdo_mysql.default_socket=/tmp/mysql.sock
mysql.default_socket=/tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
where /tmp/mysql.sock is the path to your socket.
Save your modifications and exit ESC + SHIFT: x
Restart Apache
sudo apachectl stop
sudo apachectl start
I just had this problem, but it only appeared when loading certain pages (other pages worked fine). It turned out that I was making calls to MySQL after I closed the connection with mysql_close()
. So, as @brucenan said: make sure that MySQL is running when you call it.