MySQL connection error on Mac OS X 10.8.5 - mysql

I have installed MySQL using Homebrew on my Mac running OS X 10.8.5. Whenever i start my mac, and try to connect to MySQL i get the following error
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)
I then run the following commands and then im able to connect
mysqld stop
mysql.server start
But on restarting my computer again i get the same error as above and have to run those commands again. Any way to fix it permanently ?
Thank You.

If you like to have the mysql server start on startup and to do, you would run the following commands:
('5.6.10' is mysql version number. if you use another version, change it)
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.6.10/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Related

MySQL 5.7 installed via brew now getting ERROR! The server quit without updating PID file (/usr/local/var/mysql/dev_box.local.pid)

I had mysql version 8 earlier, due to some dependency i had to downgrade to 5.7 so installed mysql#5.7 via brew.
Steps
brew uninstall mysql
brew install mysql#5.7
mysql.server start
brew link mysql#5.7 --force
Now once i have stopped the server and now i want to restart it but i am getting
ERROR! The server quit without updating PID file (/usr/local/var/mysql/dev_box.local.pid).
When i do
mysqld --initialize
I am getting
[ERROR] --initialize specified but the data directory has files in it. Aborting.
[ERROR] Aborting
But when i try to start the server using services it's starting but i am not getting anything in the grep but when i do "brew services list" it's showing that server is started.
but i am not able to login
mysqladmin -u root password 'password'
This gives the error
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
I have tried almost everything from
MySql server startup error 'The server quit without updating PID file '
After MySQL install via Brew, I get the error - The server quit without updating PID file
With no luck, hoping for some quick help.
I had the same situation. What helped me is:
First uninstall mysql using brew
brew uninstall mysql
brew uninstall mysql#5.7
Then manually go through all folders in /usr/local/ and delete everything related to mysql.
For example, I had /usr/local/etc/my.cnf and my.cnf.default, so had to run rm -f /usr/local/etc/my*; also /usr/local/mysql-5.7.21-macos10.13-x86_64 and mysql-8.0.16-macos10.14-x86_64, so rm -rf /usr/local/mysql* and so on.
After this is done install using brew
brew install mysql#5.7
brew services start mysql#5.7

MySQL installed with Brew is refusing to start

I've installed mysql using brew:
MacBook-Air-Pavel:mysql paus$ sudo brew install mysql
==> Installing mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.6.24.yosemite.bottle.tar.gz
######################################################################## 100,0%
==> Pouring mysql-5.6.24.yosemite.bottle.tar.gz
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.
To connect:
mysql -uroot
To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
mysql.server start
==> Summary
🍺 /usr/local/Cellar/mysql/5.6.24: 9719 files, 338M
Unfortunately, when I'm trying to staet mysql.server, it crashes with this message:
MacBook-Air-Pavel:webogram paus$ mysql.server start
Starting MySQL
. ERROR! The server quit without updating PID file (/usr/local/var/mysql/MacBook-Air-Pavel.local.pid).
MacBook-Air-Pavel:webogram paus$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
I've no idea why it happens. Could you help me to avoid this problem?
Thank you!

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

I have installed MySQL server and trying to connect to it, but getting the error:
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
I have checked my /tmp directory and there is no mysql.sock. I can't find mysql.sock anywhere. I read that it might be in
/var/lib/mysql/mysql.sock
But I checked there as well and there is even no mysql directory, only some postfix thing inside /lib. Could anyone help me with this problem?
Try to start the MySQL server:
mysql.server start
I got the same question after updating OS X Yosemite, well the solution is quite simple, check system preference -> mysql, the status was STOP. Just restart it and it works fine on my mac now.
For MAMP
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
From https://coderwall.com/p/w5kwzw/solved-can-t-connect-to-local-mysql-server-through-socket-tmp-mysql-sock
UPDATE:
Every time my computer restarts I have to enter this command, so I created a shortcut.
Do the following in terminal type:
~: vi ~/.profile
Add
alias ...='source ~/.profile'
alias sockit='sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock'
Save.
In terminal type:
~: ... to source the .profile config.
Now in terminal you can just type
~: sockit
Following command resolved my issue:
sudo chown -R _mysql:mysql /usr/local/var/mysql
sudo mysql.server start
After trying all solutions it worked only for me after specifying the host
mysql -u root -p -h127.0.0.1
when asking for password
Enter password:
press enter
and it will work , if everything is ok as above .
After struggling for hours the only thing which worked was
sudo mysql.server start
Then do a secure installation with
mysql_secure_installation
Then connect to the db via
mysql -uroot -p
Mysql is installed via homebrew and the version is
Server version: 5.7.21 Homebrew
Specifying the version might be helpful as the solution may be different based upon the version.
Try this it worked for me.
sudo /usr/local/mysql/support-files/mysql.server start
In your mysql config file, which is present in /etc/my.cnf make the below changes and then restart mysqld dameon process
[client]
socket=/var/lib/mysql/mysql.sock
As well check this related thread
Can't connect to local MySQL server through socket '/tmp/mysql.sock
Following resolved my issue:
Check where is your MySQL server is listning to: netstat -nlp
If it is listning to TCP then use 127.0.0.1 while connecting to DB instead of "localhost"
Check MySQL doc here
First Type this-:
brew services start mysql
Then this -:
mysql -uroot
Type in the terminal as follows:
mysql.server start
If you are using XAMPP in Mac OS X and have installed MySQL with Homebrew you may have this problem. In XAMPP manager window go to Manage Servers and select MySQL, then click configure and open the configuration file, there you have the socket file path, put the path in your MySQL host config and it should work.
It's something like this:
...
[client]
#password = your_password
port = 3306
socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
...
then, for instance in Django:
...
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "database_name",
"USER": "user",
"PASSWORD": "password",
"HOST": "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock",
"PORT": "",
}
}
...
Hope this helps.
First, knowing where the data directory was for me was the key. /usr/local/var/mysql
In here, there was at least one file with extension .err preceded with my local machine name. It had all info i needed to diagnose.
I think i screwed up by installing mysql 8 first. My app isn't compatible with it so i had to downgrade back to 5.7
My solution that worked for me was going to
/usr/local/etc/my.cnf
Find this line if its there. I think its mysql 8 related:
mysqlx-bind-address = 127.0.0.1
Remove it because in the mysql 5.7 says it doesnt like it in the error log
Also add this line in there if its not there under the bind-address.
socket=/tmp/mysql.sock
Go to the /tmp directory and delete any mysql.sock files in there. On server start, it will recreate the sock files
Trash out the data directory with mySQL in the stopped state. Mine was /usr/local/var/mysql . This is the same place where the logs are at
From there i ran
>mysqld --initialize
Then everything started working...this command will give you a random password at the end. Save that password for the next step
Running this to assign my own password.
>mysql_secure_installation
Both
>brew services stop mysql#5.7
and
>mysql.server start
are now working. Hope this helps. It's about 3 hours of trial and error.
Stoping and starting the mysql server from terminal resolved my issue.
Below are the cmds to stop and start the mysql server in MacOs.
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
Note:
Restarting the services from Mac System preference didn't resolve the issue in my mac. So try to restart from terminal.
Faced the same issue while taking mysql dump:
Can't connect to local MySQL server through socket '/tmp/mysql.sock
Try to give the path for mysql.sock explicitly.
type ps -ef|grep -mysql
Get the path for mysql.sock from this command, e.g /var/lib/mysql/mysql.sock
mysqldump --socket=/var/lib/mysql/mysql.dump -u username -pPassword
you can try this with any mysql command
For CentOS, the file to init mysql is located here:
/etc/init.d/mysqld start
I have spent lots of time doing this
I want to put my django app on my server and when I run python manage.py migrate I met this questions
And!! I set this
ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
It worked finally!
I have faced the same issue. Here is how I have fixed it.
Step 1: Remove mysql using command:
brew uninstall --force mysql
Step 2: Run command brew doctor which will give you some hint related to your brew packages.
Step 3: Cleanup brew packages using command:
brew cleanup
Step 4: Move/delete previously installed mysql data using command:
mv /usr/local/var/mysql/ /usr/local/var/old_mysql
Step 5: Finally install mysql again using command:
brew install mysql
I start mysql from settings,it works!
If you're running on a macOS it's just easier to first check go to 'System Preferences' and see if MySQL is running or not.

Lost or corrupt mysql table after computer is turned off or restarted

I'm using a mysql local database to store some data from two ruby scripts i created. The problem is that whenever I restart or turn off my computer my database seems to get corrupted as when i tried to access it it show the following error:
Can't connect to MySQL server on 'Andres-MacBook-Air-3.local' (61) (Mysql2::Error)
I then use the following commands (which I found while googling around) to restore the connection:
cd /usr/local/mysql
sudo ./bin/mysqld_safe
(Enter your password, if necessary)
(Press Control-Z)
bg
After I do that, I can connect to the database but when I try to access a table the following error comes up:
Error retrieving table information
An error occurred while retrieving the information for table 'fourCheckins'. Please try again.
MySQL said: Table 'trackpuntos.fourcheckins' doesn't exist
I think that turning off/restarting my computer is causing the problem as this same scenario has happen to be twice. Fortunately after the first time I am doing regular mysqldumps.
Does anyone have an idea on why is this happening/how to prevent it?
I'm running mysql 5.6.12, on a macbook air running OS Lion.
Thanks
I've been having the same problem for the past week. I'm finding the InnoDB databases are getting corrupted when I have open connections to the databases and I shut off/restart the computer.
I had installed mysql 5.6 unintentionally via homebrew upgrade on my Mac. I just rolled mysql back to 5.5.29 following the instructions below. After the downgrade back to 5.5, I set up my databases again, opened a bunch of connections, and then shutdown the computer. Upon reboot, I had no corruptions. Hopefully it'll stay that way!
Note: paths may be different for your system
Uninstall 5.6 mysql via homebrew
$ brew uninstall mysql
$ rm -rf /usr/local/var/mysql
Install 5.5.29 mysql via homebrew
$ cd /usr/local/Library/Formula
$ git checkout -b mysql-5.5.29 336c97637c5449018cde59f7d2a87a298692da08
$ brew install mysql
follow all the post-install steps for mysql...
$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
reset homebrew to master !important!
$ git checkout master
Lastly, don't brew upgrade mysql until 5.6 is more stable!
I had the same problem after a reboot... Just stopped and restarted MySQL and my DB was working fine after that.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' [duplicate]

This question already has answers here:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
(43 answers)
Closed 21 days ago.
I installed MySQL on Mac OS X Mountain Lion with homebrew install mysql, but when I tried mysql -u root I got the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
What does this error mean? How can I fix it?
You'll need to start MySQL before you can use the mysql command on your terminal. To do this, run brew services start mysql. By default, brew installs the MySQL database without a root password. To secure it run: mysql_secure_installation.
To connect run: mysql -uroot. root is the username name here.
This happened after the homebrew install and occurs due to permission issues. The following commands fixed the issue.
sudo chown -R _mysql:mysql /usr/local/var/mysql
sudo mysql.server start
It's probably because MySQL is installed but not yet running. [...] To verify that it's running, open up Activity Monitor and under "All Processes", search and verify you see the process "mysqld".
You can start it by installing "MySQL.prefPane".
The above quote is from the blog Obscured Clarity, "Install MySQL on Mac OS X", where you can also find the complete tutorial that I found most helpful.
Run: brew info mysql
And follow the instructions. From the description in the formula:
Set up databases to run AS YOUR USER ACCOUNT with:
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
To set up base tables in another folder, or use a different user to run
mysqld, view the help for mysql_install_db:
mysql_install_db --help
and view the MySQL documentation:
* http://dev.mysql.com/doc/refman/5.5/en/mysql-install-db.html
* http://dev.mysql.com/doc/refman/5.5/en/default-privileges.html
As others have pointed out this is because MySQL is installed but the service isn't running. There are many ways to start the MySQL service and what worked for me is the below.
To start the service:
Go to "System Preference"
At the bottom pane there should be MySql icon.
Double click that to launch the 'MySQL Server Status' and press the button 'Start MySQL Server'
My env:
Mac Yosemite 10.10.3
Installed Package: /Volumes/mysql-advanced-5.6.24-osx10.8-x86_64
Solutions revolve around:
changing MySQL's permissions
sudo chown -R _mysql:mysql /usr/local/var/mysql
Starting a MySQL process
sudo mysql.server start
Just to add on a lot of great and useful answers that have been provided here and from many different posts, try specifying the host if the above commands did not resolve this issue for you, i.e
mysql -u root -p h127.0.0.1
The designated data directory /usr/local/var/mysql/ is unusable. You can remove all files that the server added to it.
MacOS:
$brew services stop mysql
$brew services list
$brew uninstall mysql
$brew install mysql
$brew postinstall mysql
If Any error found then run those cmd
! Warning: this will delete any databases on this server:
$sudo rm -rf /usr/local/var/mysql
$sudo rm /usr/local/etc/my.cnf
$brew postinstall mysql
$brew services start mysql
$mysql_secure_installation
Completed All process of secure installation then run
$mysql -u root -p
Congratulations you’ve just set up mysql!
Warning - this method will remove all of your databases in the /usr/local/var/mysql folder
I had MySQL installed with Homebrew, and the only thing that fixed this for me was re-installing MySQL.
On my company laptop, I didn't have permission to uninstall MySQL from my computer via Homebrew:
$ brew uninstall mysql --ignore-dependencies
Uninstalling /usr/local/Cellar/mysql/8.0.12... (255 files, 233.0MB)
Error: Permission denied # dir_s_rmdir - /usr/local/Cellar/mysql/8.0.12
So instead, I removed and reinstalled MySQL manually:
$ sudo rm -rf /usr/local/Cellar/mysql
$ brew cleanup
$ sudo rm -rf /usr/local/var/mysql
$ brew install mysql
And that worked!
Looks like your mysql server is not started. I usually run the stop command and then start it again:
mysqld stop
mysql.server start
Same error, and this works for me.
Below I'm including the latest instructions from brew install mysql so newer searches for this issue can benefit:
$ brew install mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.6.26.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.6.26.yosemite.bottle.1.tar.gz
To connect:
mysql -uroot
To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
mysql.server start
In my case I loaded mysql now via launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist and was then able to launch $ mysql and be on my way.
This problem related to /usr/local/var/mysql folder access, I remove this folder and reinstall mysql.
uninstall mysql with brew :
brew uninstall mysql
sudo rm -r /usr/local/var/mysql
brew install mysql#8.0
mysql -u root
This solution works fine for me!
BUT YOU LOST ALL YOUR DATABASES! WARNING!
This fixed my issue when I restarted the mysql service. Just run:
brew services start mysql
In my case it was simply a matter of deleting a lock file.
sudo rm -f /tmp/mysql.sock.lock
Ough, that took me a while to figure out. I saw it in a comment. After installing mysql using brew, and starting the service (perhaps using sudo brew services start mysql) then run:
$ mysqld
And MySQL should be running for your thereafter.
For me it was simple as running:
/usr/local/opt/mysql/bin/mysqld_safe
instead of mysqld
I went through this issue and I managed to run mysql server using below solution
Install mysql through .dmg(https://dev.mysql.com/downloads/mysql/5.7.html), you will get mysql service panel in system preferences then start mysql from the panel and try
mysql -u root -p
Images attached for reference
just hit "brew services start mysql" in terminal
I found the solution to my problem. It was indeed because my MySQL server was not running.
It was caused by MySQL not being correctly set up on my machine, thus not being able to run.
To remedy this, I used a script which installs MySQL on Mac OSX Mountain Lion, which must have installed missing files.
Here is the link: http://code.macminivault.com/
Important Note: This script sets the root password as a randomly generated string, which it saves on the Desktop, so take care not to delete this file and to note the password. It also installs MySQL manager in your system preferences. I'm also not sure if removes any existing databases, so be careful about that.
After working on this for several hours what worked for me was
go to /etc/mysql/ and edit the my.cnf file. Add the following
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
AFTER USING ALL THE ABOVE SOLUTIONS, NOTHING WORKED FOR ME BUT THIS WORKED.
I you have installed MySQL using HomeBrew then check System Preferences > MySQL in your mac that MySQL Server is stopped if it is running stop it by clicking on Stop MySQL Server and start MySQL from terminal writing the command mysqlserver.start.
If it doesn't work you can also try:-
If you have also installed MySQL workbench then just uninstall MySQL workbench and MySQL and after that install MySQL first and then MySQL workbench.
I would recommend you to run
mysql.server start
before going to
mysql -u root -p
so as to make sure that the mysql server is running before trying to login into it
This happens many times what you start/restart a machine where by no mysql server is running.
I keep coming back to this post, I've encountered this error several times. It might have to do with importing all my databases after doing a fresh install.
I'm using homebrew. The only thing that used to fix it for me:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
This morning, the issue returned after my machine decided to shut down overnight. The only thing that fixed it now was to upgrade mysql.
brew upgrade mysql
After trying many solutions, seems like the one that finally did the trick was to connect by IP. No longer file sockets getting deleted randomly.
Just update your MySQL client config (e.g. /usr/local/etc/my.cnf) with:
[client]
port = 3306
host=127.0.0.1
protocol=tcp
Probably you might have faced some issues during the homebrew mysql installation and mysql services might not be running. If that is the case, then it might be worth trying below steps to reinstall mysql properly and then try to connect.
sudo chown -R $(whoami) /usr/local/var
sudo chown -R $(whoami) /Library/Caches/Homebrew
brew uninstall mysql
brew install mysql
mysql.server start
mysql -uroot
Homebrew will absolutely never repair the permissions using a standard reinstall, and chasing down which folder/file is corrupted will take longer than it is worth.
In this case - blow away the mysql install manually and reinstall via homebrew. Takes about 2 minutes.
cd /usr/local/var
sudo rm -rf mysql
brew install mysql
Homebrew installs mysql insecure by default, so if you want a password:
mysql_secure_installation
Then fire it up.
mysql -uroot
After installing MySQL on my mac m1 macOS Monterey, using brew install mysql I got this info:
[System] [MY-013169] [Server] /opt/homebrew/Cellar/mysql/8.0.27_1/bin/mysqld (mysqld 8.0.27) initializing of server in progress as process 3624
[ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
[ERROR] [MY-013236] [Server] The designated data directory /opt/homebrew/var/mysql/ is unusable. You can remove all files that the server added to it.
[ERROR] [MY-010119] [Server] Aborting
[System] [MY-010910] [Server] /opt/homebrew/Cellar/mysql/8.0.27_1/bin/mysqld: Shutdown complete (mysqld 8.0.27) Homebrew.
and this warning:
Warning: The post-install step did not complete successfully You can
try again using: brew postinstall mysql
After that I try to start MySQL using brew services start mysql, and I got this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2)
I managed to fix it using:
> rm -rf ~/opt/homebrew/var/mysql/
> brew postinstall mysql
Now you can connect with mysql -uroot or mysql -uroot -p.
only this has made the trick for me
brew services start --all
(after trying all answers)
I managed to solve this issue by changing localhost to 127.0.0.1
For my case, just specify host as 127.0.0.1, instead of localhost:
$ bin/mysql -uroot -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
if you are using Kali linux you might want to specify the host explicitly
do that with
mysql -h 127.0.0.1 -u root
where 127.0.0.1 is your localhost