Mysql with docker: Can't connect to local MySQL server through socket - mysql

I cannot use MySQL anymore in my Docker container:
root#mysql-container:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
mysqld is running:
root#mysql-spirit-ssl:/etc/mysql/conf.d# /etc/init.d/mysql start
[info] A MySQL Server is already started.
Trying to stop mysqld timed out:
root#mysql-container:/# /etc/init.d/mysql stop
............................................................[info] Attempt to shutdown MySQL Community Server 5.7.17 timed out.
So I tried to start using the mysqladmin way:
root#mysql-container:/# /usr/bin/mysqladmin --port=8889 -u root shutdown
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)'
So I checked that MySQL daemon is running:
root#mysql-container:/# ps -eax
PID TTY STAT TIME COMMAND
1 ? Ssl 0:01 mysqld
And that socket exists:
root#mysql-container:/# ls -l /var/run/mysqld/mysqld.sock
-rwxrwxrwx. 1 mysql mysql 0 Jan 4 10:12 /var/run/mysqld/mysqld.sock
I already tried to:
restart my Docker container
comment bind address in my.cnf and restart my Docker container
kill mysqld process => does not work, process is still listed by ps -eax
recreate my Docker container
restart Docker
restart the server
delete pid and sock files, and /etc/init.d/mysql start
Result of cat /var/log/mysql/error.log:
2018-02-27T15:27:35.966028Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2018-02-27T15:27:35.966061Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
However I cannot kill that mysqld process, either with pkill mysqld, kill -9 1 or initctl --system stop mysql.
Could this be related to Docker?

Remark: The MySQL daemon could not be killed because it was owned by Docker user systemd+ and it was the entry point of the container. Indeed mysqld was process with PID 1. This means that MySQL daemon could be restarted by simply restarting the Docker container, and that MySQL configuration could be modified in between.
I noticed in MySQL logs tail -f /var/log/mysql/error.log that a data recovery was triggered on daemon start due to an anomaly detected during internal log scan: the database was not closed properly. However the recovery could not repair the data and an intentional crash was performed. As a consequence, the container was restarted and so on. This infinite loop prevented mysqld to start and the socket to be used by the client mysql.
1) This configuration of /etc/mysql/conf.d/my.cnf enabled to skip the recovery:
[mysqld]
innodb_force_recovery=4
and to use mysql client with socket to dump important schemas and/or delete corrupted schemas.
Do not forget to remove this line from my.cnf after you're done!
2) Perfoming a mysql upgrade and repair could also have been beneficial:
docker exec -it mysql-container mysql_upgrade -u root -p --force
mysqlcheck -u root -p --auto-repair --check --all-databases
Restarting the Docker container is necessary after this step.
3) Also, deleting MySQL internal logs (that were scanned and triggered the recovery) was necessary:
cd /var/lib/mysql/mysql/
rm ibdata1 ib_logfile0 ib_logfile1
Now I can use MySQL again, from inside and outside the container.

Related

Can't connect to local MySQL server and lost root password

I can't connect to a MySQL server, so I stopped MySQL with:
sudo /etc/init.d/mysql stop
# Stopping mysql (via systemctl): mysql.service.
Then, login with root and start MySQL again:
sudo -s
mysqld_safe --skip-grant-tables &
# .... mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
# 2021-05-29T03:01:11.967630Z mysqld_safe mysqld from pid file /var/lib/mysql...... ended
After that I executed:
mysqld_safe --skip-grant-tables
# Logging to '/var/lib/mysql/......err'.
# 2021-05-29T03:03:40.507375Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
# 2021-05-29T03:03:40.834556Z mysqld_safe mysqld from pid file /var/lib/mysql/...... ended
# [1]+ Done mysqld_safe --skip-grant-tables
At last I executed:
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
mysql -u root
# ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I also forgot my root password.
H, welcome.
You dont' need to call mysqld_safe two times, only one time is necessary. The parameter --skip-grant-tables makes any local connection to MySQL happens without password.
The message:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
Means that your server isn't listening on default unix socket located on /var/run/mysqld/mysqld.sock.
Kill every mysqld instance with:
ps -ef | grep mysql
# copy every id and...
kill <id>
Look for any clue in MySQL logs, probrably in /var/log/mysql/error.log and then try to start with mysqld_start --skip-grant-tables.

Installing Mysql on a VM under Alpine

I'm trying to install and use mysql on Alpine. I'm using Docker to generate a VM under Alpine.
My Dockerfile is really simple :
FROM alpine:3.11.3
CMD sh
Once I've run the image created (using docker build // docker run image_id), I install Mysql :
apk add --update --upgrade mysql mysql-client
Then, I install the database :
mysql_install_db
The problem is that once I've done that and I try to "mysql", the machine returns this :
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
And when I try to do "mysqld -u root", it returns me this problem :
2020-08-26 10:07:55 0 [ERROR] Could not open mysql.plugin table. Some plugins may be not loaded
2020-08-26 10:07:55 0 [ERROR] Can't open and lock privilege tables: Table 'mysql.servers' doesn't exist
2020-08-26 10:07:55 0 [ERROR] Can't start server : Bind on unix socket: No such file or directory
2020-08-26 10:07:55 0 [ERROR] Do you already have another mysqld server running on socket: /run/mysqld/mysqld.sock ?
2020-08-26 10:07:55 0 [ERROR] Aborting
I understood that the file mysqld.sock serves the server to discuss with the client. I tried to create the directory mysqld and then to create the file mysqld.sock but it doesn't work. It returns me the same error but with a different return value.
(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (111)
At least, I don't want to use an existing image of mysql on Dockerhub.
Do anybody has an idea of what step is missing to make it work ?
Sorry if I'm not writing proper, I'm not a native english speaker.
I was able to successfully install and start mysql on an alpine container with these commands:
$ docker run -it --rm alpine:latest
/ # apk add mysql mysql-client
/ # mkdir /run/mysqld
/ # mysql_install_db
/ # mysqld -u root --data=./data &> /dev/null &
Testing connection:
/ # mysql -e "SELECT VERSION();"
+----------------+
| VERSION() |
+----------------+
| 10.6.4-MariaDB |
+----------------+

AWS EC2 mysql root password issue

Installed the below in amazon ec2 new instance.
yum install -y httpd24 php56 mysql55-server php56-mysqlnd
Then did a start of mysqld using service mysqld start . It gave a list of commands that needs to be executed.
Followed the instructions in the service output to change the root password. When the below command is run it gives an error
/usr/libexec/mysql55/mysqladmin: connect to server at '177.37.1.30' failed
error: 'Host 'ip-177-37-1-30.ap-southeast-1.compute.internal' is not allowed to connect to this MySQL server'
How do I fix this?
service mysqld start
Initializing MySQL database: Installing MySQL system tables...
161102 4:22:07 [Note] /usr/libexec/mysql55/mysqld (mysqld 5.5.52) starting as process 3137 ...
OK
Filling help tables...
161102 4:22:07 [Note] /usr/libexec/mysql55/mysqld (mysqld 5.5.52) starting as process 3144 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/libexec/mysql55/mysqladmin -u root password 'new-password'
/usr/libexec/mysql55/mysqladmin -u root -h ip-177-37-1-30 password 'new-password'
Alternatively you can run:
/usr/libexec/mysql55/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/libexec/mysql55/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
Give Nat permissions in VPC where instance is running.
thank you.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

I have installed Centos7 update, removed MariaDB and done a fresh install of MySQL - additionally I have removed it and reinstalled it but everytime I connect to mysql -u root -p I get the titled error. Additionally I have looked at other tickets associated with this but still not making progress. If I run service mysqld restart it says "Redirecting to /bin/systemctl restart mysqld.service" and then just hangs. Really not a linux expert so not sure what is causing this?
I had the same problem and found out a few things after digging around. MariaDB is a drop-in replacement for mysql. On the new system, mysql is the MariaDB client (although I'm not clear on what that means). I checked to see if the service was running:
service mysqld status
which indicated:
Redirecting to /bin/systemctl status mysqld.service
mysqld.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
In other words, the mysqld service is not running.
Starting the service worked for me:
`systemctl start mariadb.service`
Now all the mysql commands work as expected.
To tie the last knot, enable the service at boot:
`systemctl enable mariadb.service`
Check the 'socket=' entry in your /etc/my.cnf within the [mysqld] section:
[mysqld1]
socket = /tmp/mysql.sock1
An alternate method is to provide the '-hhost' & '-Pport' parameters to the 'mysqladmin' or 'mysql' commands, to avoid using the socket.
mysql -u user -hservername -P3006 -p -Dschema
mysqladmin -u user -p -hhostname -Pport
Removing the dir fixed it - deleted the dir (with rm -rf "dir name") and then unintalled mysql "yum remove mysql mysql-server" then reinstalled "yum install mysql-server" and ran "service mysqld start"
If you have this problem with the start mysql service hanging - check the logs /var/log/mysqld.log as suggested by Jeremiah.
If you look into the MariaDB log file, you may see that the issue is related to creating pid file. To solve it: create the PID directory then change its owner to mysql:mysql

OS X Mavericks: mysql socket defined in my.cnf, but not getting created

I'm trying to install mysql5 (the latest from Oracle, via .dmg) on OS X Mavericks. My /opt/local/my.cnf looks like this:
[client]
socket=/tmp/mysql.sock
[mysqld]
socket=/tmp/mysql.sock
[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
When I try to connect, I get the following:
$ mysql5 -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
And there is no /tmp/mysql.sock file created.
Here's my /tmp dir:
$ ls -l /tmp
lrwxr-xr-x# 1 root wheel 11 Oct 24 08:31 /tmp -> private/tmp
I've also tried forcing the socket from the command line:
$ time sudo mysqld_safe5 --socket=/tmp/mysql.sock
Password:
131221 07:26:02 mysqld_safe Logging to '/opt/local/var/db/mysql5/Macintosh.local.err'.
131221 07:26:02 mysqld_safe Starting mysqld daemon with databases from /opt/local/var/db/mysql5
131221 07:17:26 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
I know it's reading the conf file because if I remove it, I get a different socket location when I attempt to connect:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)
But again, no luck. What do I need to do to get that socket created?
Change your /tmp folder to have permission for MySQL user. (chown mysql:mysql /tmp or chmod 777 /tmp would resolve your issue.)
Try using dtrace to see if mysqld is actually opening a socket, and if so, where. You'll also be able to tell what config files are being read by the server, although that doesn't seem to be the issue since passing --socket=... doesn't work.
I'm used to strace on Linux but recently started using dtrace at work. Here's a pretty simple example of how to trace open(2) calls, which you could modify to trace socket creation, or just all syscalls: https://github.com/tsibley/dtrace-scripts/blob/master/trace-open.d