181
sudo /usr/local/mysql/support-files/mysql.server start
This worked for me. However, if this doesnt work then make sure that mysqld is running and try connecting.
171
For me 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
- [Django]-How do I create a slug in Django?
- [Django]-Django admin file upload with current model id
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
161
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 set to
localhost
, then a socket or pipe is used. - If host is set to
127.0.0.1
, then the client is forced to use TCP/IP.
So, for example, you can check 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 check if you can connect to your database via sockets, use mysql -h localhost
.
If none of this helps, then you probably need to post more details about your MySQL config, exactly how youβre instantiating the connection, etc.
- [Django]-Homepage login form Django
- [Django]-How do I do a not equal in Django queryset filtering?
- [Django]-How can i test for an empty queryset in Django?
36
I just changed the HOST
from localhost
to 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': '',
},
...
- [Django]-How to go from django image field to PIL image and back?
- [Django]-Is it bad to have my virtualenv directory inside my git repository?
- [Django]-Django Rest Framework custom response message
31
Iβve seen this happen at my shop when my devs have a stack manager like MAMP installed that comes preconfigured with MySQL installed in a non standard place.
at your terminal run
mysql_config --socket
that will give you your path to the sock file. take that path and use it in your DATABASES HOST paramater.
What you need to do 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 which mysql_config
if you somehow have multiple instances of mysql server installed on the machine you may be connecting to the wrong one.
- [Django]-Django project models.py versus app models.py
- [Django]-Getting the SQL from a Django QuerySet
- [Django]-Using django-rest-interface
30
For me, 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.
- [Django]-OneToOneField() vs ForeignKey() in Django
- [Django]-Can I access constants in settings.py from templates in Django?
- [Django]-Add rich text format functionality to django TextField
15
When, if you lose your daemon mysql in mac OSx but is present in other path for exemple in private/var do the following command
1)
ln -s /private/var/mysql/mysql.sock /tmp/mysql.sock
2) restart your connexion to mysql with :
mysql -u username -p -h host databasename
works also for mariadb
- [Django]-What is the difference between cached_property in Django vs. Python's functools?
- [Django]-Embed YouTube video β Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
- [Django]-Django: Use of DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT in settings.py?
9
Run the below cmd in terminal
/usr/local/mysql/bin/mysqld_safe
Then restart the machine to take effect. It works!!
- [Django]-Django 1.5 custom User model error. "Manager isn't available; User has been swapped"
- [Django]-Does SQLAlchemy have an equivalent of Django's get_or_create?
- [Django]-Django annotation with nested filter
9
After attempting a few of these solutions and not having any success, this is what worked for me:
- Restart system
- mysql.server start
- Success!
- [Django]-Django REST framework post array of objects
- [Django]-How to test auto_now_add in django
- [Django]-Strings won't be translated in Django using format function available in Python 2.7
9
To those who upgraded from 5.7 to 8.0 via homebrew, this error is likely caused by the upgrade not being complete. In my case, mysql.server start
got me the following error:
ERROR! The server quit without updating PID file
I then checked the log file 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 mysql@5.7
via homebrew, stop the server, and then start the 8.0 system again.
brew install mysql@5.7
/usr/local/opt/mysql@5.7/bin/mysql.server start
/usr/local/opt/mysql@5.7/bin/mysql.server stop
Then,
mysql.server start
This would get your MySQL (8.0) working again.
- [Django]-Sending an SMS to a Cellphone using Django
- [Django]-Django β {% csrf_token %} was used in a template, but the context did not provide the value
- [Django]-Django-social-auth django-registration and django-profiles β together
8
I have two sneaky conjectures on this one
CONJECTURE #1
Look into the possibility of not being able to access the /tmp/mysql.sock
file. When I setup MySQL databases, I normally let the socket file site in /var/lib/mysql
. If you login to mysql as root@localhost
, your OS session needs access to the /tmp
folder. Make sure /tmp
has the correct access rights in the OS. Also, make sure the sudo user can always read file 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 MySQL with 127.0.0.1
, you may need to 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 MySQL, run this:
SELECT USER(),CURRENT_USER();
What does this give you?
- USER() reports how you attempted to authenticate in MySQL
- CURRENT_USER() reports how you were allowed to 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 create the corresponding user root@127.0.0.1
.
- [Django]-Convert Django Model object to dict with all of the fields intact
- [Django]-405 "Method POST is not allowed" in Django REST framework
- [Django]-How do you dynamically hide form fields in Django?
7
Check number of open files for the mysql process using lsof command.
Increase the open files limit and run again.
- [Django]-How to deal with "SubfieldBase has been deprecated. Use Field.from_db_value instead."
- [Django]-Choose test database?
- [Django]-Passing variable urlname to url tag in django template
7
This may be one of following problems.
- Incorrect mysql lock.
solution: You have to 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 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.
- [Django]-Django: "projects" vs "apps"
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
4
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 first database interaction required, for instance, by import of a module in settings.py or some __init__.py.
Iβll try to digg up some more info, but this might already ring a bell for your case.
- [Django]-How to define two fields "unique" as couple
- [Django]-Django β makemigrations β No changes detected
- [Django]-Custom django admin templates not working
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
- [Django]-Error: No module named staticfiles
- [Django]-Django composite unique on multiple model fields
4
if you get an error like below :
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Then just find your mysqld.sock file location and add it to βHOSTβ.
Like i am using xampp on linux so my mysqld.sock
file is in another location. so 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' : ''
}
}
- [Django]-Copy a database column into another in Django
- [Django]-Programmatically saving image to Django ImageField
- [Django]-Charts in django Web Applications
3
Had this same problem. Turned out mysqld
had stopped running (Iβm on Mac OSX). I restarted it and the error went away.
I figured out that mysqld
was not running largely because of this link:
http://dev.mysql.com/doc/refman/5.6/en/can-not-connect-to-server.html
Notice the first tip!
- [Django]-How to convert a Django QuerySet to a list?
- [Django]-How to resize an ImageField image before saving it in python Django model
- [Django]-Annotate with latest related object in Django
3
I had to 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 me.
- [Django]-Django index page best/most common practice
- [Django]-Suppress "?next=blah" behavior in django's login_required decorator
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
2
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 check if the PID is changing.
- [Django]-Django: How can I create a multiple select form?
- [Django]-Why there are two process when i run python manage.py runserver
- [Django]-Django REST framework post array of objects
2
Looked around online too long not to contribute. After trying to type in the mysql prompt from the command line, I was continuing to receive this message:
ERROR 2002 (HY000): Canβt connect to local MySQL server through socket β/tmp/mysql.sockβ (2)
This was due to the fact that my local mysql server was no longer running. In order to 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;
- [Django]-Images from ImageField in Django don't load in template
- [Django]-How to check Django version
- [Django]-IOS app with Django
1
The socket is located in /tmp. On Unix system, due to modes & 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 check should be to relocate mysql.sock in a more neutral directory.
The fact that the problem occurs βrandomlyβ (or not every time) let me 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
iotop
show you something wrong when you encounter the problem ?
- [Django]-Django REST Framework: adding additional field to ModelSerializer
- [Django]-Django get objects not referenced by foreign key
- [Django]-What's the best way to extend the User model in Django?
1
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.
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Django import error β No module named core.management
- [Django]-How do you dynamically hide form fields in Django?
1
# shell script ,ignore the first
$ $(dirname `which mysql`)\/mysql.server start
May be helpful.
- [Django]-Django Rest Framework Conditional Field on Serializer
- [Django]-No module named pkg_resources
- [Django]-Django β No module named _sqlite3
1
I had faced similar problem recently. Went through many answers. I got it working by following steps.
- change the socket path in /etc/my.cnf (as i was repeatedly getting error with /tmp/mysql.sock ) reference to change the socket path
- run mysqld_safe to restart the server as it is the recommended way to restart in case of errors. reference to mysqld_safe
- [Django]-Django index page best/most common practice
- [Django]-How to get getting base_url in django template
- [Django]-How do I filter ForeignKey choices in a Django ModelForm?
- [Django]-Extend base.html problem
- [Django]-Passing variable urlname to url tag in django template
- [Django]-Django: How to get related objects of a queryset?
- [Django]-How can I filter a Django query with a list of values?
- [Django]-ValueError: The field admin.LogEntry.user was declared with a lazy reference
- [Django]-Django test app error β Got an error creating the test database: permission denied to create database
0
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 this other page a socket file is used even if you specify localhost.
A Unix socket file is used if you do not specify a host name or if you
specify the special host name localhost.
It also shows how to check on your server by running these commands:
If a mysqld process is running, you can check it by trying the
following commands. The port number or Unix socket file 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
- [Django]-How do I create a slug in Django?
- [Django]-How to set a value of a variable inside a template code?
- [Django]-Django custom field validator vs. clean
0
in ubuntu14.04 you can do this to 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=
- [Django]-How to query as GROUP BY in Django?
- [Django]-How to server HTTP/2 Protocol with django
- [Django]-How to use "get_or_create()" in Django?
0
For me, Iβm sure mysqld is started, and command line mysql can work properly. But the httpd server show the issue(canβt connect to 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.
- [Django]-How to pass information using an HTTP redirect (in Django)
- [Django]-How to loop over form field choices and display associated model instance fields
- [Django]-Django model "doesn't declare an explicit app_label"
0
Simply try to run mysqld
.
This was what was not working for me on mac.
If it doesnβt work try go to /usr/local/var/mysql/<your_name>.err
to see detailed error logs.
- [Django]-Django select_for_update cannot be used outside of a transaction
- [Django]-Django render_to_string missing information
- [Django]-Migrating Django fixtures?
0
Using MacOS Mojave 10.14.6 for MySQL 8.0.19 installed via Homebrew
- Ran
sudo find / -name my.cnf
- File found at
/usr/local/etc/my.cnf
Worked for a time then eventually the error returned. Uninstalled the Homebrew version of MySQL and installed the .dmg file directly from here
Happily connecting since then.
- [Django]-How to get getting base_url in django template
- [Django]-Django models avoid duplicates
- [Django]-Switching to PostgreSQL fails loading datadump
0
In my case what helped was to edit the file /etc/mysql/mysql.conf.d/mysqld.cnf
and replace the line:
socket = /var/run/mysqld/mysqld.sock
with
socket = /tmp/mysql.sock
Then I restarted the server and it worked fine. The funny thing is that if I put back the line as it was before and restarted it still worked..
- [Django]-How to recursively query in django efficiently?
- [Django]-When saving, how can you check if a field has changed?
- [Django]-Django: remove a filter condition from a queryset
0
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-How to pull a random record using Django's ORM?
- [Django]-Backwards migration with Django South
0
Iβm using macOS Monterey, I fixed it by changing the file content which locate in /opt/homebrew/etc/my.cnf from "bind-address = 127.0.0.1" to "bind-address = localhost"
- [Django]-Django: how save bytes object to models.FileField?
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
- [Django]-How can I check the size of a collection within a Django template?
0
You can try
$ mysql.server start
then
$ mysql -h localhost -u root -p
in password just press enter and it will start,
then u can change password
- [Django]-How to get getting base_url in django template
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Django β {% csrf_token %} was used in a template, but the context did not provide the value
0
I installed MySQL 8.0 on macOS Monterey 12.5.1 using homebrew brew install mysql
When I ran mysql -uroot
it gave me that error.
I fixed it like this:
sudo /usr/local/opt/mysql/support-files/mysql.server start
It showed some error pointing to a given error file
so I just deleted the error file:
rm -rf /usr/local/var/mysql/*.err
and then reran the start command
sudo /usr/local/opt/mysql/support-files/mysql.server start
And then mysql -uroot
worked again
- [Django]-Serving Media files during deployment in django 1.8
- [Django]-ModuleNotFoundError: No module named 'grp' on windows
- [Django]-Factory-boy create a list of SubFactory for a Factory
-2
Mac user here running mac os mojave 10.14. In my case what helped me was to uninstall MySQL from within the system preferences pane and then reinstalling and selecting the legacy password MySQL system during the installation wizard.
I did this by going to apple menu > system preferences >> MySQL >> and then hitting uninstall.
Once I successfully uninstalled I went back to MySQLβs website and into the archive downloads and downloaded mysql Ver 8.0.18 for macos10.14 on x86_64 (MySQL Community Server β GPL).
Now, when going through the install wizard you will come to a point where the installer asks you to choose the password type that youβd like to use and here you MUST SELECT THE LEGACY SYSTEM that allows you to sign into MySQL using the root user and a password that you will set at that moment.
After I finished going through the install wizard I restarted my terminal (in my case I was using vs codeβs terminal) and then ran "mysql -u root -p" and was able to enter the password I created during installation and got into MySQL with no errors.
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-Why does DEBUG=False setting make my django Static Files Access fail?
- [Django]-Iterating over related objects in Django: loop over query set or use one-liner select_related (or prefetch_related)