Cannot start MariaDB after installing alongside MySQL on Ubuntu 14.04 - mysql

I have installed MariaDB 10.0.14 following the official instructions in mariadb.com line by line.
I'm running Ubuntu 14.04 (upgraded from 12.04) and already have MySQL server installed. When I try to run mariadb I first stop the MySQL service:
$ sudo /etc/init.d/mysql stop
$ sudo /etc/init.d/mariadb start
but nothing happens. Apparently mariadb.sock cannot be created:
$ mysql -e "SELECT VERSION();" --socket=/opt/mariadb-data/mariadb.sock
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/mariadb-data/mariadb.sock' (111)
My error log is:
141112 13:50:37 mysqld_safe Starting mysqld daemon with databases from /opt/mariadb-data
141112 13:50:37 [Note] Server socket created on IP: '::'.
141112 13:50:37 [ERROR] mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 13 "Permission denied")
141112 13:50:37 [ERROR] Can't start server: can't create PID file: Permission denied
141112 13:50:37 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
I can't understand why I get permissions denied upon starting the service. I've created new system group and user, both called mariadb, that should handle this, as stated in the instructions.
Perhaps I should grant full r/w rights for my /opt/mariadb-data directory, but I'm not sure this is a good approach. What should I do?

Even though #hartmut-holzgraefe provides a nice solution in his answer, I decided to try a different approach. Since it works really well and independently from my MySQL server I thought I might share it with everybody as well.
Going with a Docker container for MariaDB (tutum/mariadb) turns out to be a quicker and perhaps cleaner solution than trying to install it alongside MySQL.
Steps
The steps to run mariadb via docker are:
Install Docker
Pull whichever version of MariaDB you want from tutum-docker-mariadb (in my case 10.1)
Build an image and run it to set up a container
Commands
The commands for step 3 are listed in the repo's README.
I'll only mention you may want to explicitly preset the hostname and the port of your machines, like so:
# build the image
docker build -t tutum/mariadb .
# run it
docker run -d -p 127.0.0.1:3307:3306 -e MARIADB_PASS="mypass" tutum/mariadb
# connect to mariadb
mysql -uadmin -pmypass -h127.0.0.1 -P3307
admin is a default user created with the initial run of the
container
mypass is a custom pass to override the random
password that is initially generated (otherwise you'd have to use
docker logs to get it it)
127.0.0.1:3307:3306
means you are binding the 3306 port inside the container with the
3307 port on you localhost.

Are you sure you got the --defaults-file=/opt/mariadb-data/my.cnf modifications to the /etc/init.d/mariadb correctly applied?
Problem is that mysqld still tries to create the .pid file in /var/run/mysql, not under /opt/mariadb. The /var/run/mysql directory belongs to the "mysql" system user, not the "mariadb" user you created according to the instructions.
And the /var/run/mysql setting can only come from the system /etc/mysql/my.cnf file, which is read by mysqld_safe / mysqld by default, UNLESS this is overridden with --defaults-file=...
Also note that --defaults-file needs to be the very first command line option

Related

Warning: mysql_connect(): Can't connect to local MySQL server through socket

My WordPress site ( http://steamboatperinatalconference.com ) just recently started going down and spitting this error
" Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /var/www/html/wp-includes/wp-db.php on line 1568 "
I have checked the db password and its ok, also reset the instance and sql server and both are running, I'm able to connect to my database through mysqlworkbench.
Not sure why my website won't display if the database is up and running.
Any help is appreciated.
First kill the processes with:
sudo pkill mysql
and
sudo pkill mysqld
restart mysql
sudo service mysql restart
now you should be able to log in
mysql -u root -p
It turns out in order to fix the issue I was having I needed to run a sudo yum update on the AWS ec2 instance, after performing the update the page was back up and running
my.cnf (location of file /etc/mysql/ folder) configured with
socket=/var/lib/mysql/mysql.sock
check for MySQL is running with the following command:
mysqladmin -u root -p status
changing permission to MySQL folder. If you are working locally, you can try:
sudo chmod -R 755 /var/lib/mysql/

ERROR 2002 (HY000): Can't connect to local MySQL server through sock error on homebrew after MySQL 8.0 [duplicate]

When I attempted to connect to a local MySQL server during my test suite, it
fails with the error:
OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
However, I'm able to at all times, connect to MySQL by running the command line
mysql program. A ps aux | grep mysql shows the server is running, and
stat /tmp/mysql.sock confirm that the socket exists. Further, if I open a
debugger in except clause of that exception, I'm able to reliably connect
with the exact same parameters.
This issue reproduces fairly reliably, however it doesn't appear to be 100%,
because every once in a blue moon, my test suite does in fact run without
hitting this error. When I attempted to run with sudo dtruss it did not reproduce.
All the client code is in Python, though I can't figure how that'd be relevant.
Switching to use host 127.0.0.1 produces the error:
DatabaseError: Can't connect to MySQL server on '127.0.0.1' (61)
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.
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.
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
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.
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': '',
},
...
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.
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
Run the below cmd in terminal
/usr/local/mysql/bin/mysqld_safe
Then restart the machine to take effect. It works!!
After attempting a few of these solutions and not having any success, this is what worked for me:
Restart system
mysql.server start
Success!
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.
Check number of open files for the mysql process using lsof command.
Increase the open files limit and run again.
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.
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.
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.
Make sure your /etc/hosts has 127.0.0.1 localhost in it and it should work fine
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' : ''
}
}
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!
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.
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.
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;
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 iotopshow you something wrong when you encounter the problem ?
# shell script ,ignore the first
$ $(dirname `which mysql`)\/mysql.server start
May be helpful.
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
If you installed through Homebrew, try to run
brew services start mysql
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
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=
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.
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.
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.
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.

Mysql: Access denied for user 'root'#'localhost' right after installation on macOS

I just installed MySQL v8.0.11 on my macOS High Sierra v10.13.4 from the dmg package downloaded from the MySQL website. The installer did not ask for any privileges or access settings during installation.
After installation finished, I tried running:
$ mysql -u root
I was returned the error ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO).
I then tried running the same command with sudo but I was returned the same error.
Going through answers to similar questions, I tried running:
sudo /etc/init.d/mysql stop
It returns the error sudo: /etc/init.d/mysql: command not found
If then I go ahead and try the next step which is
sudo mysqld_safe --skip-grant-tables
I am returned the output
[2] 27806
[2] + 27806 suspended (tty output) sudo mysqld_safe --skip-grant-tables
Also, running
mysqld --skip-grant-tables
returns me the following errors:
mysqld: Can't change dir to '/usr/local/mysql-8.0.11-macos10.13-x86_64/data/' (OS errno 13 - Permission denied)
2018-04-20T14:29:23.579709Z 0 [System] [MY-010116] [Server] /usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysqld (mysqld 8.0.11) starting as process 29470
2018-04-20T14:29:23.593533Z 0 [Warning] [MY-010091] [Server] Can't create test file /usr/local/mysql-8.0.11-macos10.13-x86_64/data/Faheems-MacBook-Air.lower-test
2018-04-20T14:29:23.593582Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/mysql-8.0.11-macos10.13-x86_64/data/ is case insensitive
2018-04-20T14:29:23.593863Z 0 [ERROR] [MY-010172] [Server] failed to set datadir to /usr/local/mysql-8.0.11-macos10.13-x86_64/data/
2018-04-20T14:29:23.593889Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-04-20T14:29:23.594332Z 0 [System] [MY-010910] [Server] /usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysqld: Shutdown complete (mysqld 8.0.11) MySQL Community Server - GPL.
It still doesn't solve the original error or lets me run mysql.
I have tried almost every answer out there on related questions but nothing works.
Thanks to anyone who can help!
After trying a few fixes, I started the MySQL server with
$ sudo mysql.server start
then went ahead with $ mysql_secure_installation to set the password for the root user.
This worked for me.
Note: Homebrew asks you to first do mysql_secure_installation before starting the MySQL server for the first time, but that resulted in the above-mentioned error.
Its true that
Homebrew asks you to first do mysql_secure_installation before
starting the MySQL server for the first time, but that resulted in the
above-mentioned error.
Solution:
Remove mysql complete from your computer
Download and Install mysql without brew. Specify your desire password here or based on the version the installer might mention you a password
set the path for for mysql
export PATH=$PATH:/usr/local/mysql/bin
Check whether its installed properly mysql --version
mysql -uroot p to login
change the password if required mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('root')
I spent quite some time trying to install MySQL and successfully logging in to it from teminal. I first used the official dmg file (mysql-8.0.29-macos12-arm64.dmg), and then with brew (brew install mysql), but got 'access denied' no matter what I tried.
The steps that solved it for me:
First uninstall from brew with the following commands in the terminal
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
brew remove mysql
brew cleanup
When it is removed it can be installed again using the official dmg file. After following the installer, I set a password from System Preferences with 'Initialize Database' and then click 'Start MySQL Server'. When the marks beside the instances are green, we are ready.
I then opened my terminal and wrote mysql -u root -p but this just threw 'Command Not Found'. I then navigated to the directory inside terminal, the directory is listed inside 'MySQL' in 'System Preferences' which for me is '/usr/local/mysql-8.0.29-macos12-arm64'. Here I tried to run the command again, but it only worked when i used ./mysql -u root -p, the last thing to do then, was to add this path to my terminal profile, which is done with:
nano ~/.zshrc or nano ~/.bash_profile depending on which one you use, and adding: export PATH=${PATH}:/usr/local/mysql-8.0.29-macos12-arm64/bin to the bottom of the file.
After these steps, I can now use the terminal and enter MySQL by simply writing: mysql -u root -p and then entering the password I set inside System Preferences.
(My system: MacBook Air M1, MacOS 12.4)

MySQL error, missing sock, can still connect with everything except command line

I'm getting the following error when trying to connect to MySQL through the command line (Ubuntu 14.04)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Seems that the file /var/run/mysqld/mysqld.sock is missing. MySQL is still running
% sudo service mysql status
mysql start/running, process 42703
and I am able to connect with MySQLdb and sqlalchemy via Python and through MySQL Workbench and MySQL Pro. I was running a Python script and got the following error
(1053, 'Server shutdown in progress')
I restarted the script and everything ran like usual but since this point I have been unable to connect via command line. Not sure what happened or why the sock would disappear. I also ran
sudo find / -type s
and nothing related to MySQL is found.
UPDATE: Seems that someone uncommented bind-address = 127.0.0.1 (inside /etc/mysql/my.cnf), restarted the server, realized this was a mistake, put it back, and restarted. Would this remove the sock file?
Problem seems to be that multiple mysql processes are running. Try stopping mysql and mysqld processes followed by restarting mysql:
sudo pkill mysql
sudo pkill mysqld
sudo service mysql restart
now you should be able to log into mysql normally
mysql -u root -p
if it still doesn't work, and you're mysql database is local, try changing the bind-address from 127.0.0.1 to localhost before attempting to kill the processes like I showed you above.
Open up the configuration file:
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
look for the line containg bind-address and change the line to
bind-address = localhost
save the file and exit
now kill the processes and restart mysql ( first 3 steps in the beginning of this comment).

Unable to connect to MySQL

Whenever I try to connect to MySQL to access phpmyadmin, it returns an error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13)
I also tried to start MySQL through my server's terminal:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
When I restarted MySQL it displayed:
Stopping MySQL database server mysqld
df: `/var/lib/mysql/.': No such file or directory
df: no file systems processed
/etc/init.d/mysql: ERROR: The partition with /var/lib/mysql is too full!
How might I resolve this?
I've seen that a couple of times. It has meant that the actual MySQL server instance was down for some reason. It was fixed by a simple call to:
service mysql restart
Edit
I just noticed your comment The partition with /var/lib/mysql is too full!. This means your drive is too full to run MySQL. You need to either talk to your server administrator or just clean up the HD, but this will keep breaking until more room is available.
Normally this happens when you have don't have the mysql daemon started. most distros you can start it by typing in
/etc/init.d/mysqld start
and that should get you going. I think it can sometimes also be that after you did your install you will need to give root a password.
mysqladmin -u root password <enter password here>
You may want to check first that the dameon is atually running by doing a
ps -ef | grep mysql
Credit