Can't connect to local MySQL server through socket '/tmp/mysql.sock

  • 3,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 3
  • Tình trạng: Còn hàng
sudo /usr/local/mysql/support-files/mysql.server start 

This worked for bủ. However, if this doesnt work then make sure that mysqld is running and try connecting.

answered Sep 17, 2013 at 6:36

6

For bủ the problem was I wasn't running MySQL Server. Run server first and then execute mysql.

$ mysql.server start
$ mysql -h localhost -u root -p

Lii

12.1k9 gold badges68 silver badges89 bronze badges

answered May trăng tròn, năm ngoái at 20:32

5

The relevant section of the MySQL manual is here. I'd start by going through the debugging steps listed there.

Also, remember that localhost and 127.0.0.1 are not the same thing in this context:

  • If host is mix to lớn localhost, then a socket or pipe is used.
  • If host is mix to lớn 127.0.0.1, then the client is forced to lớn use TCP/IP.

So, for example, you can kiểm tra if your database is listening for TCP connections vi netstat -nlp. It seems likely that it IS listening for TCP connections because you say that mysql -h 127.0.0.1 works just fine. To kiểm tra if you can connect to lớn your database via sockets, use mysql -h localhost.

If none of this helps, then you probably need to lớn post more details about your MySQL config, exactly how you're instantiating the connection, etc.

answered May 6, 2013 at 16:04

7

I just changed the HOST from localhost to lớn 127.0.0.1 and it works fine:

# settings.py of Django project
...

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '',
},
...

answered Mar 7, 2017 at 15:53

4

For bủ, the mysql server was not running. So, i started the mysql server through

mysql.server start

then

mysql_secure_installation

to secure the server and now I can visit the MySQL server through

mysql -u root -p

or

sudo mysql -u root -p

depending on your installation.

answered Jun 19, 2020 at 1:30

2

I've seen this happen at my cửa hàng when my devs have a stack manager lượt thích MAMP installed that comes preconfigured with MySQL installed in a non standard place.

at your terminal run rẩy

mysql_config --socket

that will give you your path to lớn the sock tệp tin. take that path and use it in your DATABASES HOST paramater.

What you need to lớn tự is point your

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'test',
        'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
        'PORT': '',
    },
}

NOTE

also run rẩy which mysql_config if you somehow have multiple instances of mysql server installed on the machine you may be connecting to lớn the wrong one.

answered May 9, 2013 at 16:35

4

When, if you lose your daemon mysql in mac OSx but is present in other path for exemple in private/var tự the following command

1)

ln -s /private/var/mysql/mysql.sock /tmp/mysql.sock

2) restart your connexion to lớn mysql with :

mysql -u username -p -h host databasename

works also for mariadb

answered Aug 24, 2013 at 11:55

Run the below cmd in terminal

/usr/local/mysql/bin/mysqld_safe

enter image mô tả tìm kiếm here

Then restart the machine to lớn take effect. It works!!

answered Jan 22, năm ngoái at 11:12

1

After attempting a few of these solutions and not having any success, this is what worked for me:

  1. Restart system
  2. mysql.server start
  3. Success!

answered Oct 29, năm ngoái at 18:31

1

To those who upgraded from 5.7 to lớn 8.0 via homebrew, this error is likely caused by the upgrade not being complete. In my case, mysql.server start got bủ the following error:

ERROR! The server quit without updating PID file

I then checked the log tệp tin via cat /usr/local/var/mysql/YOURS.err | tail -n 50, and found the following:

InnoDB: Upgrade after a crash is not supported.

If you are on the same boat, first install [email protected] via homebrew, stop the server, and then start the 8.0 system again.

brew install [email protected]

/usr/local/opt/[email protected]/bin/mysql.server start
/usr/local/opt/[email protected]/bin/mysql.server stop

Then,

mysql.server start

This would get your MySQL (8.0) working again.

answered Jul 13, 2018 at 0:43

2

I have two sneaky conjectures on this one

CONJECTURE #1

Look into the possibility of not being able to lớn access the /tmp/mysql.sock tệp tin. When I setup MySQL databases, I normally let the socket tệp tin site in /var/lib/mysql. If you login to lớn mysql as root@localhost, your OS session needs access to lớn the /tmp thư mục. Make sure /tmp has the correct access rights in the OS. Also, make sure the sudo user can always read tệp tin in /tmp.

CONJECTURE #2

Accessing mysql via 127.0.0.1 can cause some confusion if you are not paying attention. How?

From the command line, if you connect to lớn MySQL with 127.0.0.1, you may need to lớn specify the TCP/IP protocol.

mysql -uroot -p -h127.0.0.1 --protocol=tcp

or try the DNS name

mysql -uroot -p -hDNSNAME

This will bypass logging in as root@localhost, but make sure you have root@'127.0.0.1' defined.

Next time you connect to lớn MySQL, run rẩy this:

SELECT USER(),CURRENT_USER();

What does this give you?

  • USER() reports how you attempted to lớn authenticate in MySQL
  • CURRENT_USER() reports how you were allowed to lớn authenticate in MySQL

If these functions return with the same values, then you are connecting and authenticating as expected. If the values are different, you may need to lớn create the corresponding user [email protected].

answered May 10, 2013 at 21:23

Check number of open files for the mysql process using lsof command.

Increase the open files limit and run rẩy again.

answered May 6, 2013 at 13:06

1

This may be one of following problems.

  1. Incorrect mysql lock. solution: You have to lớn find out the correct mysql socket by,

mysqladmin -p variables | grep socket

and then put it in your db connection code:

pymysql.connect(db='db', user='user', passwd='pwd', unix_socket="/tmp/mysql.sock")

/tmp/mysql.sock is the returned from grep

2.Incorrect mysql port solution: You have to lớn find out the correct mysql port:

mysqladmin -p variables | grep port

and then in your code:

pymysql.connect(db='db', user='user', passwd='pwd', host='localhost', port=3306)

3306 is the port returned from the grep

I think first option will resolve your problem.

answered May 9, 2013 at 15:25

if you get an error lượt thích below :

django.db.utils.OperationalError: (2002, "Can't connect to lớn local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

Then just find your mysqld.sock tệp tin location and add it to lớn "HOST".

Like i am using xampp on linux so sánh my mysqld.sock tệp tin is in another location. so sánh it is not working for '/var/run/mysqld/mysqld.sock'

DATABASES = {

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'asd',
        'USER' : 'root',
        'PASSWORD' : '',
        'HOST' : '/opt/lampp/var/mysql/mysql.sock',
        'PORT' : ''
    }
}

answered Jun 7, 2018 at 9:59

I think i saw this same behavior some time ago, but can't remember the details.
In our case, the problem was the moment the testrunner initialises database connections relative to lớn first database interaction required, for instance, by import of a module in settings.py or some __init__.py. I'll try to lớn digg up some more info, but this might already ring a bell for your case.

answered May 1, 2013 at 20:51

Make sure your /etc/hosts has 127.0.0.1 localhost in it and it should work fine

answered May 4, 2013 at 21:33

2

I had to lớn kill off all instances of mysql by first finding all the process IDs:

ps aux | grep mysql

And then killing them off:

kill -9 {pid}

Then:

mysql.server start

Worked for bủ.

answered Feb 7, 2017 at 14:41

Check that your mysql has not reached maximum connections, or is not in some sort of booting loop as happens quite often if the settings are incorrect in my.cnf.

Use ps aux | grep mysql to lớn kiểm tra if the PID is changing.

answered May 6, 2013 at 19:14

Looked around online too long not to lớn contribute. After trying to lớn type in the mysql prompt from the command line, I was continuing to lớn receive this message:

ERROR 2002 (HY000): Can't connect to lớn local MySQL server through socket '/tmp/mysql.sock' (2)

This was due to lớn the fact that my local mysql server was no longer running. In order to lớn restart the server, I navigated to

shell> cd /user/local/bin

where my mysql.server was located. From here, simply type:

shell> mysql.server start

This will relaunch the local mysql server.

From there you can reset the root password if need be..

mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')
->                   WHERE User='root';
mysql> FLUSH PRIVILEGES;

answered Apr 4, năm trước at 1:32

This worked for bủ :

mysql.server start
mysql -u root -p

answered Nov 24, 2022 at 8:36

The socket is located in /tmp. On Unix system, due to lớn modes và ownerships on /tmp, this could cause some problem. But, as long as you tell us that you CAN use your mysql connexion normally, I guess it is not a problem on your system. A primal kiểm tra should be to lớn relocate mysql.sock in a more neutral directory.

The fact that the problem occurs "randomly" (or not every time) let bủ think that it could be a server problem.

  • Is your /tmp located on a standard disk, or on an exotic mount (like in the RAM) ?

  • Is your /tmp empty ?

  • Does iotopshow you something wrong when you encounter the problem ?

answered May 9, 2013 at 15:35

If it's socket related read this file

/etc/mysql/my.cnf

and see what is the standard socket location. It's a line like:

socket = /var/run/mysqld/mysqld.sock

now create an alias for your shell like:

alias mysql="mysql --socket=/var/run/mysqld/mysqld.sock"

This way you don't need root privileges.

answered Aug 10, 2017 at 10:13

# shell script ,ignore the first 
$ $(dirname `which mysql`)\/mysql.server start

May be helpful.

answered Dec 9, 2019 at 15:07

1

I had faced similar problem recently. Went through many answers. I got it working by following steps.

  1. change the socket path in /etc/my.cnf (as i was repeatedly getting error with /tmp/mysql.sock ) reference to lớn change the socket path
  2. run mysqld_safe to lớn restart the server as it is the recommended way to lớn restart in case of errors. reference to lớn mysqld_safe

answered Apr 10, 2020 at 12:24

If you installed through Homebrew, try to lớn run

brew services start mysql

answered Jul 12, 2020 at 21:03

Configure your DB connection in the 'Manage DB Connections dialog. Select 'Standard (TCP/IP)' as connection method.

See this page for more details http://dev.mysql.com/doc/workbench/en/wb-manage-db-connections.html

According to lớn this other page a socket tệp tin is used even if you specify localhost.

A Unix socket tệp tin is used if you tự not specify a host name or if you specify the special host name localhost.

It also shows how to lớn kiểm tra on your server by running these commands:

If a mysqld process is running, you can kiểm tra it by trying the following commands. The port number or Unix socket tệp tin name might be different in your setup. host_ip represents the IP address of the machine where the server is running.

shell> mysqladmin version 
shell> mysqladmin variables 
shell> mysqladmin -h `hostname` version variables 
shell> mysqladmin -h `hostname` --port=3306 version 
shell> mysqladmin -h host_ip version 
shell> mysqladmin --protocol=SOCKET --socket=/tmp/mysql.sock version

answered May 11, 2013 at 16:47

in ubuntu14.04 you can tự this to lớn slove this problem.

zack@zack:~/pycodes/python-scraping/chapter5$ **mysqladmin -p variables|grep socket**
Enter password: 
| socket                                            | ***/var/run/mysqld/mysqld.sock***                                                                                            |
zack@zack:~/pycodes/python-scraping/chapter5$***ln -s  /var/run/mysqld/mysqld.sock /tmp/mysql.sock***
zack@zack:~/pycodes/python-scraping/chapter5$ ll /tmp/mysql.sock 
lrwxrwxrwx 1 zack zack 27 11月 29 13:08 /tmp/mysql.sock -> /var/run/mysqld/mysqld.sock=

answered Nov 29, năm ngoái at 5:10

For bủ, I'm sure mysqld is started, and command line mysql can work properly. But the httpd server show the issue(can't connect to lớn mysql through socket).

I started the service with mysqld_safe&.

finally, I found when I start the mysqld service with service mysqld start, there are issues(selinux permission issue), and when I fix the selinux issue, and start the mysqld with "service mysqld start", the httpd connection issue disappear. But when I start the mysqld with mysqld_safe&, mysqld can be worked. (mysql client can work properly). But there are still issue when connect with httpd.

answered Jul 31, 2017 at 14:06

Simply try to lớn run rẩy mysqld.

This was what was not working for bủ on mac. If it doesn't work try go to lớn /usr/local/var/mysql/.err to lớn see detailed error logs.

answered Sep 2, 2017 at 21:10