Change port # for mysql on mac - mysql

I just installed mysql on a mac running 10.6. The mysql version is 5.1.56. I need to have mysql run on port 3307. This article says we can change the port by modifying this file:
# vi /etc/my.cnf
http://www.cyberciti.biz/faq/change-default-mysql-port-under-linuxunix/
but there is no such file in /etc. I can see mysql is running ok because I can connect to it just fine, is the way we change the port # different now?
Thanks

Looks like the new installers are adding the port as 3307
To change that follow below steps
sudo vi /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
change 3307 from this line <string>--port=3307</string> to 3306

On OSX you can create /etc/my.cnf if it does not exist. You can base it on samples found in /usr/local/mysql/support-files. Don't forget to restart MySQL for your my.cnf to take effect.

On MacOs High Sierra running MySql v8 server, you need the following:
Edit /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
Under ProgramArguments, you will see many entries with
<string>...</string> etc,
Add the following line: <string>--port=16000</string>
Also, to restart you need to do the following:
launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
and then
launchctl load -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
I hope this helps.

By default, the install doesn't create a my.cnf file on Snow Leopard. You can create one yourself under /etc or you can copy one from /usr/local/mysql/support-files/
Then run :
sudo cp my-huge.cnf /etc/my.cnf
Check out the explanation here

I experienced a similar problem and here's what worked for me. If you installed MySQL using brew install mysql then this should work for you.
For context, I'm using macOS Monterey 12, Homebrew 3.4.5, and MySQL 8.0.
MySQL is installed in:
/opt/homebrew/Cellar/mysql/8.0.28_1/
The configuration file my.cnf is located at /opt/homebrew/etc.
You can change the default port by specifying a new port in the my.cnf file.
First navigate to the homebrew etc folder
cd /opt/homebrew/etc
Append the new port value to the my.cnf file
echo "port = 3307" >> my.cnf
restart the MySQL service
brew services restart mysql

I tried a long time to get mysql running on my Mac (OSX 10.11.13) with mysql 5.7.11 to develop wordpress sites on my local machine...
When I used the app duplicator to migrate a website to my local machine I got errors during the database import. This was caused by to stricked sql_mode...
The trick that made it was:
Going to
/usr/local/mysql-5.7.11/support-files/
and copy the content from my-default.cnf
and paste it to
/etc/my.cnf (if this file does not exist create it!)
at the very last line of my.cnf I added:
sql_mode="NO_ENGINE_SUBSTITUTION"
Et voilà: sql_mode is changed permanently!
Whoop whoop!

I installed docker and was having problems with my company wanting to use port 3306 (which is what I was using privately).
Similar problem to you. So this is what I did to fix it.
sudo vi /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
Then you get a long list of script tags and
Insert <string>--port=3307</string> into the array. Make sure you put it under the other strings and within the array.
I changed my port to 3307 instead of 3306 and now docker is working.

Related

How to install and start MySQL 5.7 on macOS BigSur (Apple Silicon) with Homebrew?

I need a specific version of MySQL (5.7) to be installed on my MacBook with M1.
I'm trying to do that with Homebrew.
brew install mysql#5.7
The output:
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
mysql#5.7 is keg-only, which means it was not symlinked into /opt/homebrew, because this is an alternate version of another formula.
If you need to have mysql#5.7 first in your PATH, run: echo 'export PATH="/opt/homebrew/opt/mysql#5.7/bin:$PATH"' >> ~/.zshrc
For compilers to find mysql#5.7 you may need to set: export LDFLAGS="-L/opt/homebrew/opt/mysql#5.7/lib" export CPPFLAGS="-I/opt/homebrew/opt/mysql#5.7/include"
To have launchd start mysql#5.7 now and restart at login: brew services start mysql#5.7 Or, if you don't want/need a background service you can just run: /opt/homebrew/opt/mysql#5.7/bin/mysql.server start
Right after that, I try to run:
echo 'export PATH="/opt/homebrew/opt/mysql#5.7/bin:$PATH"' >> ~/.zshrc
source .zshrc
mysql_secure_installation
And get the error:
Securing the MySQL server deployment.
Enter password for user root:
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Also, I've tried:
brew services start mysql#5.7
And also get the error:
Error: Permission denied # rb_sysopen - /Users/vivanc/Library/LaunchAgents/homebrew.mxcl.mysql#5.7.plist
Seems like there is a permissions-related problem.
Any advice is appreciated.
If you are running into issues like "Can't connect to local MySQL server through socket '/tmp/mysql.sock'" or "The post-install step did not complete successfully MySQL", and you installed a previous version of mysql (e.g. 8.x) previously, you may have been at the point where you need to clean everything before reinstalling your preferred version of mysql#x.x.
If you've already visited these to links:
Uninstall all those broken versions of MySQL and re-install it with Brew on Mac Mavericks (Coderwall) + Install MySQL 5.7 on macOS using Homebrew (github) and your're still having trouble with starting your mysql-service, you should try to also remove also /opt/homebrew/etc/my.cnf file.
Remove it together with all the related files too! Summary for an M1 Apple Silcion machine, after uninstalling via brew uninstall mysql or brew uninstall mysql#x.x, please remove:
/opt/homebrew/var/mysql
/opt/homebrew/etc/my.cnf
After this, everything worked like a fresh install (for me). Hope this saved someone's time.
While this question is specific to Apple Silicon and mysql 5.7, and I will address that in this answer, I would like to start by adding some general notes to save others time:
Homebrew supports mysql 5.7 for both intel and apple silicon.
https://formulae.brew.sh/formula/mysql#5.7
Homebrew supports mysql 5.6 for the intel chip, but not apple silicon:
https://formulae.brew.sh/formula/mysql#5.6
If you need 5.6 on apple silicon, it's probably a good idea to just settle with 5.7 since production will need to be upgraded eventually and the differences aren't that big.
In my situation I originally did brew install mysql and it gave me the latest mysql (currently 8.0). When I tried going back over it and doing brew install mysql#5.6, of course this didn't work due to not being supported on the m1 max (apple silicon). I ran into issues then trying to get brew install mysql#5.7 working. I followed some guides mentioning various suggestions. After playing with it for a while, it seems that running two versions of mysql at once will corrupt your mysql files and make it hard to work with and confuse homebrew.
I came across these guides:
brew install mysql on macOS
https://coderwall.com/p/os6woq/uninstall-all-those-broken-versions-of-mysql-and-re-install-it-with-brew-on-mac-mavericks
https://www.codegrepper.com/code-examples/shell/brew+uninstall+mysql
Before reading further, note that in my situation, I had a zip available for my whole local mysql database needs and could risk destroying what I have.
Warning! Reading further and executing these commands carelessly may delete your mysql storage. Make sure you have a backup.
brew remove mysql is a good command that got rid of my latest install.
I also needed brew remove mysql#5.7 even though this is the version I'm trying to install, but they were stepping on each other's foot...
brew cleanup is nice, I noticed it freed up some lock files.
I don't recall having any luck with:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
but you can try it.
rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist has worked for me.
But note there will also be another file in there for your 5.7 setup.
I'd recommend:
cd ~/Library/LaunchAgents/ then look around to see if you have any mysql* in there. Then if you do, such as homebrew.mxcl.mysql#5.7.plist, then remove it.
These are some that worked for me:
sudo rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf
rm /usr/local/etc/my.cnf.default
Also:
cd opt/homebrew/var
Then if you notice a mysql directory in there, remove it.
It's possible you might have docker or something else similar also running mysql or mysqld.
ps -ax | grep "[m]ysql"
(Note the brackets and quotes in the above is to prevent the grep from showing up in the process list and matching itself unlike the lazier version: ps -ax | grep mysql which will give the impression there's an extra mysql process running)
Once you're ready, please run:
brew services list
double-check you don't have mysql in there.
Then double check you don't have any mysql process running:
ps -ax | grep "[m]ysql"
This can happen for example if you enter mysqld for example....
You might need to do brew services stop mysql or brew services stop mysql#5.7 or similar and repeat steps if you see anything on the service list or process list.
Finally, you should be ready for a fresh install.
The fresh installation process should be rather straight forward:
brew install mysql#5.7
brew link --force mysql#5.7
brew services start mysql#5.7
Then run brew services list to make sure your installation worked correctly.
If you see it's green and "started", your installation was successful!
You should also run:
mysql_secure_installation
Then choose a password for root and go through the list of questions like validation, etc.
Once you're installed, you might also run into mysql mode issues.
cd /opt/homebrew/etc and you should find a my.cnf file.
Edit it using either vim or nano.
You should see something like:
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
Edit it and set the mysql mode. For example, I don't want the no_zero_in_date mode, so I use:
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
sql-mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Then restart mysql like so:
brew services restart mysql#5.7
Then double check you still have a green status "started":
brew services list
If you do, then you should have the no_zero_in_date mode disabled.
You can test it with a query:
SELECT ##sql_mode;
You should now have mysql 5.7 running on Apple Silicon and have the sql_mode set.
If you run into more troubleshooting, do your best to get into a known state such as uninstalled, doing a fresh install, or already installed with some version, etc.
I solved it after hours of searching, it was because I had not cleaned everything regarding my previous MySQL 8.X installation. I followed this and got suspicious when I realized I don't have most of the folders there. I then did a search for all folders named mysql on my computer and found a mysql folder in opt/homebrew/var. After removing it, reinstalling MySQL 5.7, and starting the server everything is working as expected.
When setting up my Apple M1 Macbook Pro I was experiencing the issue of the following error message being thrown:
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
My fix was to modify the MySQL config file:
nano /usr/local/etc/my.cnf
Append the following lines:
tmpdir=/tmp
user=root
Then run:
brew services restart mysql#5.7
This fixed my issue, and I was then able to connect successfully with my DB client.
Seems like I found the solution (or workaround).
After installing the MySQL 5.7 with Homebrew just run:
mysql.server start
MySQL is started and then you are able to run mysql_secure_installation and mysql commands.
I got this warning:
You should change the ownership of these directories to your user.
sudo chown -R $(whoami) /usr/local/share/doc /usr/local/share/man /usr/local/share/man/man1
And make sure that your user has write permission.
chmod u+w /usr/local/share/doc /usr/local/share/man /usr/local/share/man/man1
I run those two command then it works.

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.

For homebrew mysql installs, where's my.cnf?

For homebrew mysql installs, where's my.cnf? Does it install one?
There is no my.cnf by default. As such, MySQL starts with all of the default settings. If you want to create your own my.cnf to override any defaults, place it at /etc/my.cnf.
Also, you can run mysql --help and look through it for the conf locations listed.
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
The following groups are read: mysql client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
As you can see, there are also some options for bypassing the conf files, or specifying other files to read when you invoke mysql on the command line.
The homebrew mysql contains sample configuration files in the installation's support-files folder.
ls $(brew --prefix mysql)/support-files/my-*
If you need to change the default settings you can use one of these as a starting point.
cp $(brew --prefix mysql)/support-files/my-default.cnf /usr/local/etc/my.cnf
As #rednaw points out, a homebrew install of MySQL will most likely be in /usr/local so the my.cnf file should not be added to the system /etc folder, so I’ve changed the command to copy the file into /usr/local/etc.
If you are using MariaDB rather than MySQL use the following:
cp $(brew --prefix mariadb)/support-files/my-small.cnf /usr/local/etc/my.cnf
One way to find out:
sudo /usr/libexec/locate.updatedb
# wait a few minutes for it to finish
locate my.cnf
in my system it was
nano /usr/local/etc/my.cnf.default
as template and
nano /usr/local/etc/my.cnf
as working.
Nothing really helped me - I could not overwrite settings in a /etc/my.cnf file.
So I searched like John suggested https://stackoverflow.com/a/7974114/717251
sudo /usr/libexec/locate.updatedb
# wait a few minutes for it to finish
locate my.cnf
It found another my.cnf in
/usr/local/Cellar/mysql/5.6.21/my.cnf
changing this file worked for me! Don't forget to restart the launch Agent:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Update:
If you have a fairly recent installation of homebrew you should use the brew services commands to restart mysql (use your installed homebrew mysql version, i.e. mysql or mysql#5.7):
brew services stop mysql
brew services start mysql
Since mysql --help shows a list of files, I find it useful to pipe the result to ls to see which of them exist:
$ mysql --help | grep /my.cnf | xargs ls
ls: /etc/my.cnf: No such file or directory
ls: /etc/mysql/my.cnf: No such file or directory
ls: ~/.my.cnf: No such file or directory
/usr/local/etc/my.cnf
For my (Homebrew installed) MySQL 5.7, it seems the files is on /usr/local/etc/my.cnf.
Add another answer cause
The accepted anwser is right. Since we're talking about Homebrew installed mysql, not MySQL installed manually, there's more direct way to find the conf.
The former answers may be a little outdated, Homebrew on M1 Mac is stored in a different location
Conf
The my.cnf is copied by Homebrew to following places during installation
/usr/local/etc/my.cnf for x86 Mac
/opt/homebrew/etc/my.cnf for M1 Mac
Homebrew chooses /usr/local, or /opt/homebrew to store packages, so the default conf files are not stored in /etc/ but /usr/local/etc or /opt/homebrew/etc.
In fact, homebrew changed the -DSYSCONFDIR= (default conf location) flag during compiling mysql from source.
Start the Service
A short answer: run brew info mysql and check the tips.
The recommended way is brew services start mysql, which uses the launchd to manage services. (launchd is deemed a systemd alternative on macOS)
For anyone wanna start it manually, mysql.start without any option is enough to start the service. (mysql.start is a script provided by mysql to help start the service)
On your shell type my_print_defaults --help
At the bottom of the result, you should be able to see the file from which the server reads the configurations. It prints something like this:
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
Server version: 8.0.19 Homebrew. macOS Catalina 10.15.5 and installed MySQL via Homebrew. Found this file here:
/usr/local/etc/my.cnf
This solution helped :)
You can find where the my.cnf file has been provided by the specific package, e.g.
brew list mysql # or: mariadb
In addition to verify if that file is read, you can run:
sudo fs_usage | grep my.cnf
which will show you filesystem activity in real-time related to that file.
I believe the answer is no. Installing one in ~/.my.cnf or /usr/local/etc seems to be the preferred solution.
run
sudo find / -name my.cnf
Usually the first result is the correct one.
Should be in
/usr/local/etc/
In case of Homebrew, mysql would also look for my.cnf in it's Cellar directory, for example:
/usr/local/Cellar/mysql/5.7.21/my.cnf
For the case one prefers to keep the config close to the binaries - create my.cnf here if it's missing.
Restart mysql after change:
brew services restart mysql
If you are using mac m1 (Apple silicon), the my.cnf is located at
/opt/homebrew/etc/my.cnf
and can also be found by mysql --help
I have installed MySQL 5.7 using Homebrew
my.cnf file is located in "/opt/homebrew/etc/my.cnf"
For MacOS (High Sierra), MySQL that has been installed with home brew.
Increasing the global variables from mysql environment was not successful. So in that case creating of ~/.my.cnf is the safest option. Adding variables with [mysqld] will include the changes (Note: if you change with [mysql] , the change might not work).
<~/.my.cnf> [mysqld] connect_timeout = 43200 max_allowed_packet =
2048M net_buffer_length = 512M
Restart the mysql server. and check the variables.
y
sql> SELECT ##max_allowed_packet;
+----------------------+ | ##max_allowed_packet |
+----------------------+ | 1073741824 |
+----------------------+
1 row in set (0.00 sec)
$ps aux | grep mysqld
/usr/local/opt/mysql/bin/mysqld --basedir=/usr/local/opt/mysql --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mysql/lib/plugin
Drop your my.cf file to /usr/local/opt/mysql
brew services restart mysql

Missing /var/lib/mysql/mysql.sock file

I'm trying to access mysql and when I run the mysql command, I get the following.
[root#ip-10-229-65-166
tpdatabase-1.8.0.28356]# mysql
ERROR 2002 (HY000): Can't connect to
local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)
Doing this returns nothing
[root#ip-10-229-65-166 mysqld]# find
-name mysql*
[root#ip-10-229-65-166 mysqld]#
/etc/init.d/mysql stop
-bash: /etc/init.d/mysql: No such file or directory
[root#ip-10-229-65-173
tpdatabase-1.8.0.28356]# mysql_config
|grep -- --socket
--socket [/var/lib/mysql/mysql.sock]
Maybe a permissions problem?
I do have mysql installed using yum install mysql
I'm running CentOS 5.4 on a Amazon EC2 Cloud Instance
You need to install the server package:
sudo yum install mysql-server
After installation, you have to start the service:
sudo service mysqld start
rm -rvf /var/lib/mysql/ib_logfile*
touch /var/lib/mysql/mysql.sock
touch /var/lib/mysql/mysql.pid
chown -R mysql:mysql /var/lib/mysql
systemctl restart mysql
try this and check the permission of both /var/lib/mysql/mysql.sock and /tmp
We faced a similar problem in my office setup with the same OS. The actual thing that was happening was low space on the server. It is quite strange, but i believe if the server gets full the files cannot be loaded or of some other resort. Please check to be sure the space on the server is enough else you have to try removing the installed mysql and reinstall. Best of luck.
just search for mysqld, this is the server daemon:
find / | grep mysqld
If you found it, the server package may be correctly installed but without initd scripts. For testing, you may start the mysql server by hand executing the above file.
mysql might have been packaged separately as client and server. Check your package repositories to make sure that the server has been installed. The mysql package might only be the client. Check if there's a package called mysql-server or something similar. Check the list of files installed by the mysql package to see if it actually installs the server.
If the files are not even there, it's not a permissions problem. Also, you're running as root.
I recently encountered this problem after an upgrade on Ubuntu 18.04. The solution here solved it https://serverfault.com/a/957723/439448.
Make sure you have privileges to access the /var/lib/mysql/mysql.sock file. And if you still get the error, create a default my.cnf file with
[client]
socket = /path/to/mysql/data/mysql.sock
[mysqld]
server-id = 2
socket = /path/to/mysql/data/mysql.sock
port = 4000 #any port you wish
basedir = /path/to/mysql
datadir = /path/to/mysql/data
and initialize the database again. Make sure you give --defaults-file=/path/to/mysql/my.cnf when you initialize. If you get error saying files exist in /path/to/mysql/data, remove them and try again. Once done, when you run mysql also, give --defaults-file=/path/to/mysql/my.cnf.
Should work.
Before hitting mysql on command prompt make sure your correct my.cnf configuration file is in placed and set the permission of etc folder as
chmod -R 777
etc folder as below list to avoid
error MySQL - ERROR 2002 (HY000):
e.g /etc/my.cnf, /etc/mysql/my.cnf, $MYSQL_HOME/my.cnf [datadir]/my.cnf ~/.my.cnf

mysql.sock is not created OSX

I run mysql on OSX, now when I restart my computer it does not create the mysql.sock, meaning that all my connections gives me a error 2002.
anyone knows how to prevent this?
I had this same issue on Snow Leopard, the socket spawns in the wrong location for some reason on OS X.
To get the socket spawning in the correct location:
create a new file "my.cnf" in /etc with the following lines:
[mysqld]
socket=/var/mysql/mysql.sock
[client]
socket=/var/mysql/mysql.sock
and restart mysqld: sudo /usr/local/mysql/bin/mysqld_safe
This will force the MySQL socket file to spawn in the proper location, and everything should work normally. Good luck!
I use Mac OS 10.12 Sierra, I meet the same mysql question. I can find mysql.sock in my file system.
Resolve question method:
1 remove my.cnf
sudo rm -rf /etc/my.cnf
restart your mysql servers
brew services restart mysql
or
sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
sudo launchctl load -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
you can check it
sudo lost -i tcp:3306
mysql is running ok.
I had that problem on a Linux box once, and I found that the directory (/var/run/mysqld) was not writable by the user mysql runs as. I would check that if I were you.
I recommend to execute
sudo find / -name "mysql.sock"
to be sure the file is not anywhere.
In my cas I used the command mysql with variant socket in this way
mysql --socket=/var/lib/mysql/mysql.sock
I've been having trouble in restarting MySQL on a box running Sierra. Every time that I tried to connect after restarting my machine, I would lose the ability to connect to my MySQL instance. Based on this post, I determined that there was, indeed, no my.cnf file in my environment. I followed DashRantic's instructions to force a build of a socket file. It appears to have resolved the problem.