Can't connect to local MySQL server through socket homebrew - mysql
I recently tried installing MySQL with homebrew (brew install mysql) and when I try to run it I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
There is no /tmp/mysql.sock nor a /var/lib/mysql.sock.
I've searched and haven't found any mysql.sock file.
How can I fix this?
When you got the server running via
mysql.server start
you should see the socket in /tmp/mysql.sock. However, the system seems to expect it in /var/mysql/mysql.sock. To fix this, you have to create a symlink in /var/mysql:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
This solved it for me. Now my phpMyAdmin works happily with localhost and 127.0.0.1.
Credit goes to Henry
Warning, this will wipe your databases, take a backup if you wish to keep them
I had some directories left from another mysql(8.0) installation, that were not removed.
I solved this by doing the following:
First uninstall mysql
brew uninstall mysql#5.6
Delete the folders/files that were not removed
rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf
Reinstall mysql and link it
brew install mysql#5.6
brew link --force mysql#5.6
Enable and start the service
brew services start mysql#5.6
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.
Try to connect using "127.0.0.1" instead "localhost".
If you are able to see "mysql stopped" when you run below command;
brew services list
and if you are able to start mysql with below command;
mysql server start
this means; mysql is able to start manually, but it doesn't start automatically when the operating system is started. Adding mysql to services will fix this problem. To do so, you can run below command;
brew services start mysql
After that, you may restart your operating system and try connecting to mysql to see if it started automatically. I did the same and stop receiving below error;
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2)
I hope this helps.
The file /tmp/mysql.sock is probably a Named-Pipe, since it's in a temporary folder. A named pipe is a Special-File that never gets permanently stored.
If we make two programs, and we want one program to send a message to another program, we could create a text file. We have one program write something in the text file and the other program read what our other program wrote. That's what a pipe is, except it doesn't write the file to our computer hard disk, IE doesn't permanently store the file (like we do when we create a file and save it.)
A Socket is the exact same as a Pipe. The difference is that Sockets are usually used over a network -- between computers. A Socket sends information to another computer, or receives information from another computer. Both Pipes and Sockets use a temporary file to share so that they can 'communicate'.
It's difficult to discern which one MySql is using in this case. Doesn't matter though.
The command mysql.server start should get the 'server' (program) running its infinite loop that will create that special-file and wait for changes (listen for writes).
After that, a common issue might be that the MySql program doesn't have permission to create a file on your machine, so you might have to give it root privileges
sudo mysql.server start
Since I spent quite some time trying to solve this and always came back to this page when looking for this error, I'll leave my solution here hoping that somebody saves the time I've lost. Although in my case I am using mariadb rather than MySql, you might still be able to adapt this solution to your needs.
My problem
is the same, but my setup is a bit different (mariadb instead of mysql):
Installed mariadb with homebrew
$ brew install mariadb
Started the daemon
$ brew services start mariadb
Tried to connect and got the above mentioned error
$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
My solution
find out which my.cnf files are used by mysql (as suggested in this comment):
$ mysql --verbose --help | grep my.cnf
/usr/local/etc/my.cnf ~/.my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
check where the Unix socket file is running (almost as described here):
$ netstat -ln | grep mariadb
.... /usr/local/mariadb/data/mariadb.sock
(you might want to grep mysql instead of mariadb)
Add the socket file you found to ~/.my.cnf (create the file if necessary)(assuming ~/.my.cnf was listed when running the mysql --verbose ...-command from above):
[client]
socket = /usr/local/mariadb/data/mariadb.sock
Restart your mariadb:
$ brew services restart mariadb
After this I could run mysql and got:
$ mysql -uroot
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
So I run the command with superuser privileges instead and after entering my password I got:
$ sudo mysql -uroot
MariaDB [(none)]>
Notes:
I'm not quite sure about the groups where you have to add the socket, first I had it [client-server] but then I figured [client] should be enough. So I changed it and it still works.
When running mariadb_config | grep socket I get:
--socket [/tmp/mysql.sock]
which is a bit confusing since it seems that /usr/local/mariadb/data/mariadb.sock is the actual place (at least on my machine)
I wonder where I can configure the /usr/local/mariadb/data/mariadb.sock to actually be /tmp/mysql.sockso I can use the default settings instead of having to edit my .my.cnf (but I'm too tired now to figure that out...)
At some point I also did things mentioned in other answers before coming up with this.
After installing macos mojave, had to wipe mysql folder under /usr/local/var/mysql and then reinstall via brew install mysql otherwise permission related things would come up all over the place.
In my case, the culprit was found in the logfiles:
$ tail /usr/local/var/mysql/<hostname>.lan.err
2019-09-19 7:32:21 0 [ERROR] InnoDB: redo log file './ib_logfile0' exists. Creating system tablespace with existing redo log files is not recommended. Please delete all redo log files before creating new system tablespace.
2019-09-19 7:32:21 0 [ERROR] InnoDB: Database creation was aborted with error Generic error. You may need to delete the ibdata1 file before trying to start up again.
So I renamed ib_logfile0 to get rid of the error (I had to do the same with ib_logfile1 afterwards).
mv /usr/local/var/mysql/ib_logfile0 /usr/local/var/mysql/ib_logfile0_bak
mv /usr/local/var/mysql/ib_logfile1 /usr/local/var/mysql/ib_logfile1_bak
brew services restart mariadb
I got the same error and this is what helped me:
$ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
$launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
$mysql -uroot
mysql>
I faced the same problem on my mac and solved it, by following the following tutorials
https://mariadb.com/resources/blog/installing-mariadb-10116-mac-os-x-homebrew
But don't forget to kill or uninstall the old version before continuing.
Commands:
brew uninstall mariadb
xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - See more at: https://mariadb.com/resources/blog/installing-mariadb-10116-mac-os-x-homebrew#sthash.XQoxRoJp.dpuf
brew doctor
brew update
brew info mariadb
brew install mariadb
mysql_install_db
mysql.server start
Just to add to these answers, In my case I had no local mySQL server, it was running inside a docker container. So the socket file does not exist and will not be accessible for the "mysql" client.
The sock file gets created by mysqld and mysql uses this to communicate with it.
However if your mySql server is not running local, it does not require the sock file.
By specifying a host name/ip the sock file is not required e.g.
mysql --host=127.0.0.1 --port=3306 --user=xyz --password=xyz
If "mysqld" IS running, it's possible your data is corrupted. Try running this:
mysqld
Read through the wall of data, and check if mysqld is reporting that the database is corrupted. Corruption can present in many unintuitive ways:
mysql -uroot returns "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)".
mysql.server start returns "ERROR! The server quit without updating PID".
Sequel Pro and MySQL Workbench responds that they can't connect to MySQL on localhost or 127.0.0.1.
To recover your data, open my.cnf and add the following line in the [mysqld] section:
innodb_force_recovery=1
Restart mysqld:
$ brew services restart mysql#5.6
Now you can connect to it, but it’s in limited read-only mode.
If you're using InnoDB, run this to export all your data:
$ mysqldump -u root -p --all-databases --add-drop-database --add-drop-table > data-recovery.sql
The file is created in your ~ dir. It may take some time.
Once finished, remove innodb_force_recovery=1 from my.cnf, then restart mysql in normal mode:
$ brew services restart mysql#5.6
Drop all the databases. I did this using Sequel Pro. This deletes all your original data. Make sure your data-recovery.sql looks good before doing this. Also consider backing up /usr/local/var/mysql to be extra careful.
Then restore the databases, tables, and data with this:
$ mysql -uroot < ~/data-recovery.sql
This can be a long import/restoration process. Once complete, you’re good to go!
Thanks go to https://severalnines.com/database-blog/my-mysql-database-corrupted-what-do-i-do-now for the recovery instructions. The link has further instructions on MyISAM recovery.
You'll need to run mysql_install_db - easiest way is if you're in the install directory:
$ cd /usr/local/Cellar/mysql/<version>/
$ mysql_install_db
Alternatively, you can feed mysql_install_db a basedir parameter like the following:
$ mysql_install_db --basedir="$(brew --prefix mysql)"
After a restart I could not connect with the local mariadb, a search also brought me to this page and I wanted to share my solution with you.
I noticed that the directory my.cnf.d in /usr/local/etc/ is missing.
This is a known bug with homebrew that is described and solved there. https://github.com/Homebrew/homebrew-core/issues/36801
fast way to fix: mkdir /usr/local/etc/my.cnf.d
When running mysql_secure_installation and entering the new password I got:
Error: Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2)
I noticed when trying the following from this answer:
netstat -ln | grep mysql
It didn't return anything, and I took that to mean that there wasn't a .sock file.
So, I added the following to my my.cnf file (either in /etc/my.cnf or in my case, /usr/local/etc/my.cnf).
Under:
[mysqld]
socket=/tmp/mysql.sock
Under:
[client]
socket=/tmp/mysql.sock
This was based on this post.
Then stop/start mysql again and retried mysql_secure_installation which finally let me enter my new root password and continue with other setup preferences.
This works for me:
brew upgrade mysql
If brew does not complete "postinstall"
I only have to use:
sudo chown -R $(whoami) /usr/local/*
then completed postinstall by:
brew postinstall mysql#5.7
next step just start mysql#5.7 service.
It worked for me on MacOS Monterey.
I also ran into this... it seemingly resulted from some leftover artifacts of multiple mysql installs on my dev machine. Every time I attempted to start or restart the mysql service it would crash. Ultimately, working through the err file helped me solve my issue.
Setup:
M1 Macbook Pro running OS 12 (Monterey)
Homebrew install of mysql#5.7
At various points during troubleshooting I tried uninstalling everything mysql in my Homebrew list, deleting /opt/homebrew/var/mysql, and reinstalling mysql#5.7 to no avail. I also tried restarting my machine.
I was able to validate what was happening when the service failed to start by reviewing the err (e.g., MyComputerName.local.err) log that it was spitting out into /opt/homebrew/var/mysql/ when I tried to start/restart the service via Homebrew. In the beginning, I was seeing things like this that pointed to issues with the config:
mysqld: Table 'mysql.plugin' doesn't exist
TIMESTAMPZ 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
TIMESTAMPZ 0 [ERROR] unknown variable 'mysqlx-bind-address=127.0.0.1'
TIMESTAMPZ 0 [ERROR] Aborting
I deleted my.cnf and my.cnf.default in /opt/homebrew/etc and attempted to start the service again. Then, the failure changed to this:
TIMESTAMPZ 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
TIMESTAMPZ 0 [ERROR] Fatal error: Failed to initialize ACL/grant/time zones structures or failed to remove temporary table files.
TIMESTAMPZ 0 [ERROR] Aborting
At this point I noticed several mysql lock files in /opt/homebrew/var/homebrew/locks/ (mysql.formula.lock, mysql.formula#5.7.lock, mysql.formula#8.0.lock); I deleted those files, as well as anything mysql in /opt/homebrew/var/homebrew/linked.
After that, I was able to start mysql without any issues.
just to complete this thread.
therefore MAMP (PRO) is used pretty often
the path here is
/Applications/MAMP/tmp/mysql/mysql.sock
I manually started mysql in the system preferences pane by initialising the database and then starting it. This solved my problem.
I had same problem. After trying all these methods without success I did the following:
tail -f the-mysql-or-maria-db-error-file.err
in another console:
brew services restart mariadb
I saw the following error:
"MAC HOMEBREW Crash recovery failed. Either correct the problem (if
it's, for example, out of memory error) and restart, or delete tc log
and start mysqld with"
So I changed the tc.log extesion to tc.log.txt and restart mariadb
brew services restart mariadb
and done!
[LINUX]
Though answer is expected to be for MacOS only but in Linux we may face the same Error.
I was facing the same issue in Linux. I ran this command:
sudo /etc/init.d/mysql.server start
and I was able to run the MySQL server
Ref. https://gist.github.com/vinodpandey/1a4b5b8228f9a000ca236820185fc3bc
I just ran into this problem, I did some tricks but for me it didn't work. If you used macOS, here is what I did. We start by uninstalling mysql and then reinstalling it.
brew uninstall mysql
brew install mysql
Note: you must have homebrew installed
This happened to me today 2023-02-23 after a homebrew update.
Homebrew had created a new my.cnf.default file, I backed up my old one and replaced it with this new default and restarted Mariadb and everything worked including the old root password.
For me, I had installed mariadb long time ago, then installed mysql#5.7.
When I executed mysql -uroot, I get the error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Reading the answers:
I uninstalled mariadb
Deleted the folder /usr/local/var/mysql
Ran the command mysqld --initialize
Then I was able to mysql -uroot -p
Related
How to reset mysql to factory settings?
So I dropped all the users in mysql.user and restarted the mysql database. Now I can't seem to get into the mysql again or how to reset it as if it was a clean install. I tried uninstalling it with brew uninstall mariadb and then reinstalling it with brew install mariadb, but that didn't fix it. The only resources I can find is about how to restore a user from inside mysql (which I am struggling to get into) or how to delete mysql completely by deleting files in certain directories (which don't exist in the first place).
There are two methods: method 1: 1.Uninstall the mysql database, delete the data files in the /data directory, and then reinstall Method 2: 1.Stop the mysql service (systemctl stop mysqld) 2.Delete the files in the /data directory (rm -fr /data/*) 3.Initialize mysql (mysqld --defaults-file=/mysql/my.cnf --initialize --user=mysql --basedir=/mysql/app/mysql --datadir=/mysql/data/3306/data/) PS1: The path may be different from yours, you need to change it according to your own data directory
I did end up finding a solution to my problem. I didn't end up getting it from one source, but more cobbled a bunch of different stuff together until I figured out that this worked. Stop the server: `brew services stop mariadb Start it with this command: mysql --skip-grant-tables Run this command to fix the table with no users: mysql_upgrade --force Force kill mysql: ps -ef | grep mysql followed by kill -9 <pid> Start up the server again: brew services start mariadb I did first try mysql_install_db instead of mysql_upgrade --force as some site suggested, but that didn't work since mysql.user table still existed. It was just empty.
MacOS Can't start MySQL Server
I have MacOS Sierra. I have installed MySQL Server which has been working, however, after a reboot of the Mac Book, I cannot start the MySQL Server. I have tried changing the port from 3306 to 3307 in the my.cnf file. Question I would appreciate any help on how to start the MySQL Server, and also have it so it starts automatically on MacOS Boot up. (I am new to Mac, so apologies if this is a basic question). UPDATE Looking for error logs, I can't find anything with todays (2/2/2017) timestamp. Nothing in the data dir: No files with "mysql" have been modified since I've tried to start the MySQL Server (2/2/2017 after 09:00).
I suggest using Terminal commands. Start MySQL sudo /usr/local/mysql/support-files/mysql.server start Stop MySQL sudo /usr/local/mysql/support-files/mysql.server stop Restart MySQL sudo /usr/local/mysql/support-files/mysql.server restart
In my case, I had inadvertently transferred ownership of entire /usr/local directory to myself, which resulted in snatching-away of write permission of /usr/local/mysql-5.7.20-macos10.12-x86_64/data directory from the daemon user called "_mysql". Restoring the ownership fixed the issue.
After a discussion with Richard, the following solves the issue: my.cnf: [client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock datadir = /usr/local/mysql-5.7.17-macos10.12-x86_64/data tmpdir = /tmp Essentially, although we'd added the relevant stanza's to the my.cnf file, we'd still missed the section heading. Adding the [mysqld] section allowed mysql to start.
After installing Mysql 8.0.12 on MacOS High Sierra, Mysql server would not start up. I tried several suggestions like removing mysql and reinstalling it, rebooting the computer and changing file permissions, all to no avail. I finally got it to work by removing /etc/my.cnf.
In my case, ownership of the msyql directory had somehow changed to admin:admin. I use ares suggestion to run mysqld from the terminal: sudo /usr/local/mysql/support-files/mysql.server start Which showed a permissions error writing to a msyql file. I fixed with this command: sudo chown -R _mysql:wheel /usr/local/mysql/data from this post: What user should own /usr/local/mysql on Mac?
In my case, First time, after installing Mysql, Mac needs a restart. So restarting Mac, after installation, fixed the issue for me. MacOS doesn't come with a my.cnf file. Default settings will suffice the needs for basic usage. Once we need to override them, it makes sense to have a custom configurations.
This solution works for me. You have to open the activity monitor up and search for mysqld service. Once selected, just click the delete button located in the up left part and you will see the memory goes down. After that, you can verify your mysql server status from System Preferences and the service is up. With this solution you don't have to restart your machine and get to work fast.
My problem was that due to an incorrect restart of my machine, a database was corrupted. According to their docs doing a forced InnoDB Recovery did the trick: /etc/my.cnf innodb_force_recovery = 2
My problem was that I installed MySQL a while ago with Homebrew (forgot I did) and then went the more manual route, so I think the two installs were fighting each other. I wiped my Mac of both with the help of this walkthrough: https://gist.github.com/vitorbritto/0555879fe4414d18569d Note: Where it says to use subl - that's Sublime, use whatever editor you like. I was able to get things to work with a fresh install after that.
I had this same issue on Monterey. Make sure you're selecting the correct download according to your processor architecture.
I ran into same problem with following .err log. MacOS: Catalina 10.15.7. MySQL version: 5.7.19. 2021-08-19T02:17:26.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data 2021-08-19T02:17:27.303813Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-08-19T02:17:27.318280Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled 2021-08-19T02:17:27.335112Z 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.19) starting as process 2828 ... 2021-08-19T02:17:27.421394Z 0 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive 2021-08-19T02:17:27.428986Z 0 [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root! 2021-08-19T02:17:27.429541Z 0 [ERROR] Aborting 2021-08-19T02:17:27.429688Z 0 [Note] Binlog end 2021-08-19T02:17:27.432786Z 0 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete And I found adding --user=root to Ares's answer will start mysql server successfully. sudo /usr/local/mysql/support-files/mysql.server start --user=root Ref.: MySQL Server Command User Options
How do you start a MySQL server on linux?
Pretty simple question here. I just want a SQL database on my version of Kali linux so I can practice SQL. I opened the command line and entered tried to start mysql and get an error. > mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' I also made sure it was already installed using apt-get. What are the steps I need to take to be able to make a database with tables and data that I can query?
I don't know about Kali, but on Ubuntu it would be $ sudo service mysql start Once that command returns, the mysqld service has started, so you can use the mysql client to connect to it. Of course, you also have to make sure you have the mysql-server package installed, not just mysql-client and mysql-common, and that you've initialized the database instance. Complete post-installation instructions can be found in the official documentation, but the short version is Make sure the installer has created the mysql user account. This is the account that will "own" the server process once it starts. Change to your data directory. (I used the installer's default of /var/lib/mysql; you can change this by editing my.cnf.) As root, execute the server daemon with the --initialize switch. Check whereis to determine the correct path, then $ sudo /path/to/mysqld --initialize --user=mysql This command will twiddle itself for a while, then display an automatically-generated password and exit. Once the command returns, the database instance has been initialized and the system tables created. You can now start the database instance normally (using service start), then log in as the database user root (which is not the same as the system user root) using the password from above, then change your password, create a new database user, log in as that user, create a user database, and start creating tables. Again, the official documentation is the place to look for this; if any of the instructions in the official documentation differ from my instructions, you should ignore me and follow the official documentation's instructions.
If sudo service mysql start doesn't work for you, please try running mysqld_safe and don't kill the process. Use another tab to check the status of mysql service. This should solve your mysqld.sock issue. If it doesn't work out, then please edit your my.cnf file and add the following: socket=/var/lib/mysql/mysql.sock And the permissions, sudo chmod -R 755 /var/lib/mysql/ Hopefully, this should do it.
On my version of kali (2022-rolling), even though it's installed as MySQL, it's run as mariadb. To start it is: sudo /etc/init.d/mariadb start
Try this sudo service mysql start
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in Ubuntu [duplicate]
I am getting the following error when I try to connect to mysql: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) Is there a solution for this error? What might be the reason behind it?
Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.
Ensure that your mysql service is running service mysqld start Then, try the one of the following following: (if you have not set password for mysql) mysql -u root if you have set password already mysql -u root -p
If your file my.cnf (usually in the etc folder) is correctly configured with socket=/var/lib/mysql/mysql.sock you can check if mysql is running with the following command: mysqladmin -u root -p status try changing your permission to mysql folder. If you are working locally, you can try: sudo chmod -R 777 /var/lib/mysql/ that solved it for me
The MySQL server is not running, or that is not the location of its socket file (check my.cnf).
Most likely mysql.sock does not exist in /var/lib/mysql/. If you find the same file in another location then symlink it: For ex: I have it in /data/mysql_datadir/mysql.sock Switch user to mysql and execute as mentioned below: su mysql ln -s /data/mysql_datadir/mysql.sock /var/lib/mysql/mysql.sock That solved my problem
If you are on a recent RHEL, you may need to start mariadb (an open source mysql db) instead of the mysql db: yum remove mysql yum -y install mariadb-server mariadb service mariadb start You should then be able to access mysql in the usual fashion: mysql -u root -p
Just edit /etc/my.cnf Add following lines to my.cnf [mysqld] socket=/var/lib/mysql/mysql.sock [client] socket=/var/lib/mysql/mysql.sock Restart mysql and connect again mysql -u user -p password database -h host;
In my case I have moved socket file to another location inside /etc/my.cnf from /var/lib/mysql/mysql.sock to /tmp/mysql.sock Even after restarting the mysqld service, I still see the error message when I try to connect. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) The problem is with the way that the client is configured. Running diagnostics will actually show the correct socket path. eg ps aux | grep mysqld Works: mysql -uroot -p -h127.0.0.1 mysql -uroot -p --socket=/tmp/mysql.sock Does not Work: mysql -uroot -p mysql -uroot -p -hlocalhost You can fix this problem by adding the same socket line under [client] section inside mysql config.
Check if your mysqld service is running or not, if not run, start the service. If your problem isn't solved, look for /etc/my.cnf and modify as following, where you see a line starting with socket. Take a backup of that file before doing this update. socket=/var/lib/mysql/mysql.sock Change to socket=/opt/lampp/var/mysql/mysql.sock -u root
MariaDB, a community developed fork of MySQL, has become the default implementation of MySQL in many distributions. So first you should start, $ sudo systemctl start mariadb If this fails rather try, $ sudo systemctl start mysqld Then to start mysql, $ mysql -u root -p As of today, in Fedora the package is named mariadb And in Ubuntu it is called mariadb-server. So you may have to install it if its not already installed in your system.
Make sure you have enough space left in /var. If Mysql demon is not able to write additional info to the drive the mysql server won't start and it leads to the error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) Consider using expire_logs_days = 10 max_binlog_size = 100M This will help you keep disk usage down.
Please check whether another mysql service is running.
Make sure you started the server: mysql.server start Then connect with root user: mysql -uroot
Here's what worked for me: ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock service mysqld restart
One way to reproduce this error: If you meant to connect to a foreign server but instead connect to the non existent local one: eric#dev ~ $ mysql -u dev -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) eric#dev ~ $ So you have to specify the host like this: eric#dev ~ $ mysql --host=yourdb.yourserver.com -u dev -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 235 Server version: 5.6.19 MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +-------------------------+ | Database | +-------------------------+ | information_schema | | mysql | | performance_schema | +-------------------------+ 3 rows in set (0.00 sec) mysql> exit Bye eric#dev ~ $
If your mysql was previously working and has stopped suddenly just "reboot" the server. Was facing this issue on my CentOS VPS.-> Was constantly getting Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'(2) Tried all techniques, finally restarting the server fixed the issues -> shutdown -r now Hope this helps !!
try echo 0 > /selinux/enforce
if you change files in /var/lib/mysql [ like copy or replace that ], you must set owner of files to mysql this is so important if mariadb.service restart has been faild chown -R mysql:mysql /var/lib/mysql/* chmod -R 700 /var/lib/mysql/*
First enter "service mysqld start" and login
It worked for me with the following changes Whatever path for socket is mentioned in [mysqld] and same in [client] in my.cnf and restart mysql [mysqld] socket=/var/lib/mysql/mysql.sock [client] socket=/var/lib/mysql/mysql.sock
Please ensure you have installed MySQL server correctly, I met this error many times and I think it's complicated to debug from the socket, I mean it might be easier to reinstall it. If you are using CentOS 7, here is the correct way to install it: First of all, add the mysql community source yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm Then you can install it by yum install mysql-community-server Start it with systemctl: systemctl start mysqld
My problem was that I installed mysql successfully and it worked fine. But one day, the same error occurred. Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) And no mysql.sock file existed. This sollution solved my problem and mysql was up and running again: Log in as root: sudo su - Run: systemctl stop mysqld.service systemctl start mysqld.service systemctl enable mysqld.service Test as root: mysql -u root -p mysql should now be up and running. I hope this can help someone else as well.
Note that while mysql reads the info of the location of the socketfile from the my.cnf file, the mysql_secure_installation program seems to not do that correctly at times. So if you are like me and shuffle things around at installationtime you might get into the situation where you can connect to the database with mysql just fine, but the thing can not be secured (not using that script anyway). To fix this the suggestion from sreddy works well: make a softlink from where the script would expect the socket to where it actually is. Example: ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock (I use /tmp/ as a default location for sockets)
This might be a stupid suggestion but make 100% sure your DB is still hosted at localhost. For example, if a Network Admin chose (or changed to) Amazon DB hosting, you will need that hostname instead!
In my case, I was importing a new database, and I wasnt able to connect again after that. Finally I realized that was a space problem. So you can delete the last database and expand you hard drive or what I did, restored a snapshot of my virtual machine. Just in case someone thinks that is useful
I came to this issue when i reinstall mariadb with yum, which rename my /etc/my.cnf.d/client.cnf to /etc/my.cnf.d/client.cnf.rpmsave but leave /etc/my.cnf unchanged. For I has configed mysqld's socket in /etc/my.cnf, and mysql's socket in /etc/my.cnf.d/client.cnf with customized path. So after the installation, mysql client cannot find the mysql's socket conf, so it try to use the default socket path to connect the msyqld, which will cause this issue. Here are some steps to locate this isue. check if mysqld is running with ps -aef | grep mysqld $ps -aef | grep mysqld | grep -v grep mysql 19946 1 0 09:54 ? 00:00:03 /usr/sbin/mysqld if mysqld is running, show what socket it use with netstat -ln | grep mysql $netstat -ln | grep mysql unix 2 [ ACC ] STREAM LISTENING 560340807 /data/mysql/mysql.sock check if the socket is mysql client trying to connect. if not, edit /etc/my.conf.d/client.cnf or my.conf to make the socket same with it in mysqld [client] socket=/data/mysql/mysql.sock You also can edit the mysqld's socket, but you need to restart or reload mysqld.
Just rain into the same problem -- and here's how I addressed it. Assuming mysqld is running, then the problem might just be the mysql client not knowing where to look for the socket file. The most straightforward way to address this consists in adding the following line to your user's profile .my.cnf file (on linux that's usually under /home/myusername): socket=<path to the mysql socket file> If you don't have a .my.cnf file there, then create one containing the following: [mysql] socket=<path to the mysql socket file> In my case, since I moved the mysql default data folder (/var/lib/mysql) in a different location (/data/mysql), I added to .my.cnf the following: [mysql] socket=/data/mysql/mysql.sock Hope this helps.
ran into this issue while trying to connect mysql in SSH client, found adding the socket path to the command helpful when switching between sockets is necessary. > mysql -u user -p --socket=/path/to/mysql5143.sock
This is a problem if you are running out of disk space. Solution is to free some space from the HDD. Please read more to have the explanation : If you are running MySQL at LINUX check the free space of HDD with the command disk free : df if you are getting something like that : Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 5162828 4902260 0 100% / udev 156676 84 156592 1% /dev /dev/sda3 3107124 70844 2878444 3% /home Then this is the problem and now you have the solution! Since mysql.sock wants to be created at the mysql folder which is almost always under the root folder could not achieve it because lack of space. If you are periodicaly give the ls command under the mysql directory (at openSUSE 11.1 is at /var/lib/mysql) you will get something like : hostname:/var/lib/mysql # .protected IT files ibdata1 mysqld.log systemtemp .tmp NEWS greekDB mysql mysqld.pid test ARXEIO TEMP1 ib_logfile0 mysql.sock polis DATING deisi ib_logfile1 mysql_upgrade_info restore The mysql.sock file appearing and disappearing often (you must to try allot with the ls to hit a instance with the mysql.sock file on folder). This caused by not enough disk space. I hope that i will help some people!!!! Thanks!
I had to disable explicit_defaults_for_timestamp from my.cnf.
Can't connect to local MySQL server through socket
I am getting the following error error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' How can i restore the mysql.sock file? [Edit] I am trying to connect to mysql database from my ROR application. It was working fine till yesterday. But today I found out the mysql.sock file was missing. I don't know the reason of what led to deletion of mysql.sock file from location /var/lib/mysql/. How can i restore the mysql.file? Thanks.....
Try this: $ mysql_config |grep -- --socket It should show you the socket path as compiled in to the server. If that fails, try running the mysql command and look for the socket path in the output of \s.
here is a rude yet quick way to solve this, but if your data or operation is important, you'd better find an other way kill all the mysql process now your mysql should be stopped, but locked. So delete the lock file and restart mysqld rm /var/lock/subsys/mysqld /etc/init.d/mysqld start
I had the same problem and after spending some time i got the solution. From your terminal run this command -- sudo apt-get install mysql-server