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

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.

Related

MySQL won't start in XAMPP Manager-osx

My XAMPP mysql server is working fine
then I download the separate mysql from oracle and installed it. It is running fine but after some days I cant start it as :> mysql -u root
and in xampp mysql server it is also not working it shows stopped always.
I am providing all the commands that I tried from google as well as StackOverflow
and also I am providing all results of all commands.
Please refer commands before answering.
Number indicates ways that I tried.
1.> `mysql -u root -p`
2.> `mysql -u root`
3.> `sudo su`
3.1.> `ps aux | grep mysql`
3.2.> `kill -9 8306`
4.> `sudo /Applications/XAMPP/xamppfiles/bin/mysql.server start`
5.> `sudo killall mysqld`
6.> Changing content of file as: `innodb_force_recovery = 1` in
`/Applications/XAMPP/xamppfiles/etc/my.cnf`
7.> Changing content of file as: read only permission in
`/Applications/XAMPP/xamppfiles/etc/my.cnf`
8.> Port changed from 3306 to 3307 3308 but nothing is happened
9.> Activity manager : mysql service is. not listed
10.> Reinstalled XAMPP
I followed this 10 ways so please dont answer on this 10 ways
Result of commands :
<.1.
Pratiks-MacBook-Pro:~ pratik$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' (2 "No such file or directory")
<.2.
Pratiks-MacBook-Pro:~ pratik$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' (2 "No such file or directory")
<.3.
Pratiks-MacBook-Pro:~ pratik$ sudo su
Password:
sh-3.2# ps aux | grep mysql
root 8306 0.0 0.0 4258468 180 s000 U+ 11:35AM 0:00.00 grep mysql
sh-3.2# kill -9 8306
sh: kill: (8306) - No such process
sh-3.2#
<.4.
Pratiks-MacBook-Pro:~ pratik$ sudo /Applications/XAMPP/xamppfiles/bin/mysql.server start
Starting MySQL
.2018-03-13 11:44:29 8513 mysqld_safe Logging to '/Applications/XAMPP/xamppfiles/var/mysql/Pratiks-MacBook-Pro.local.err'.
2018-03-13 11:44:29 8513 mysqld_safe Starting mysqld daemon with databases from /Applications/XAMPP/xamppfiles/var/mysql
ERROR!
<.5.
Pratiks-MacBook-Pro:~ pratik$ sudo killall mysqld
No matching processes were found

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

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.

Can't connect to local MySQL server through socket error in Docker Image

I am working on creating the mysql image with some sample data in it. I am getting the error when I am trying to start the mysql process during the image creation so that I can put data inside the image. Below are my file structure
[DockerFile]
FROM mysql:5.6.29
# MYSQL volumes
VOLUME ["/etc/mysql", "/var/lib/mysql"]
COPY data_dump.sql /tmp/
COPY init_db.sh /tmp/
RUN chmod -R 777 /tmp/init_db.sh
RUN chmod -R 777 /tmp/data_dump.sql
RUN /tmp/init_db.sh
EXPOSE 3306
[init_db.sh]
#!/bin/bash
echo "starting the bash command"
/usr/bin/mysqld_safe &
echo "started the mysqld_safe process"
sleep 15
mysql -u root -h localhost -e "CREATE DATABASE test"
echo "Done creating the test schema"
mysql -u root -h localhost test < /tmp/data_dump.sql
echo "Done creating the test data"
I am getting the following errors:
started the mysqld_safe process
170906 13:56:49 mysqld_safe Logging to '/var/lib/mysql/bcad5a346f31.err'.
170906 13:56:49 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
170906 13:56:49 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Done creating the test schema
Done creating the test data
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I found a solution, it turns out I was doing more than I needed to. Below is my DockerFile looks like now
[DockerFile]
FROM mysql:5.6.29
# 1. Set up the environment variables
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
ENV MYSQL_DATABASE testDB
ENV MYSQL_USER root
# 2. copy the data dump file in the /docker-entrypoint-initdb.d/ location so that it gets executed by default
COPY data-dump.sql /docker-entrypoint-initdb.d/data-dump.sql
# 3. change the data location in the my.conf file in the docker, it is needed to create the data from the /docker-entrypoint-initdb.d/data-dump.sql dump file
# for mysql 5.7 if the below line doesn't work try: RUN sed -i 's|/var/lib/mysql|/var/lib/mysql-data|g' /etc/mysql/mysql.conf.d/mysqld.cnf
RUN sed -i 's|/var/lib/mysql|/var/lib/mysql-data|g' /etc/mysql/my.cnf
# 4. start the mysql process so that it creates the required data
RUN /entrypoint.sh mysqld & sleep 30 && killall mysqld
# 5. remove the dump file from the image to save image space
RUN rm /docker-entrypoint-initdb.d/data-dump.sql

How do I reset my root password in MySQL?

I'm having trouble connecting to MySQL with from Ubuntu 16.04:
mysql -u root -p
which returns the error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
When I try starting the server with:
sudo service mysql start
mysqld_safe --skip-grant-tables &
the output is:
# 2017-01-26T20:11:45.329764Z mysqld_safe Logging to syslog.
2017-01-26T20:11:45.333031Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:11:45.336684Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:11:45.358956Z mysqld_safe A mysqld process already exists
If I kill the process with:
ps aux | grep "msyql"
kill -9 pid
and then run:
mysqld_safe --skip-grant-tables &
I get:
2017-01-26T20:10:07.979813Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:10:07.984114Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:10:07.987546Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
It's an endless loop. So then what if I reinstalled from scratch?
First remove MySQL:
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean
Then reinstall:
sudo apt-get update
sudo apt-get install mysql-server
sudo mysql_install_db // (deprecated command)
sudo /usr/bin/mysql_secure_installation
As far as here:
sudo /usr/bin/mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
Error: Access denied for user 'root'#'localhost' (using password: YES)
or:
mysqladmin -u root password [newpass]
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: NO)'
Then it falls down.
I've tried following all of the answers here - ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Sometimes a temporary password is stored according to others, but it's not stored in /var/log/mysqld.log as suggested
Anybody able to help?
If you are running mysqld_safe --skip-grant-tables & and getting the following error:
2017-01-26T20:10:07.984114Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:10:07.987546Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists
It might be because MySQL 5.7 and above create the /var/run/mysqld directory dynamically while running. When you stop it with service mysql stop to run mysqld_safe --skip-grant-tables , it's not available.
You can create it manually before running mysqld_safe:
service mysql stop
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
mysqld_safe --skip-grant-tables &
Then you can change the root password or whatever you were trying to do.
How you read this web page and tried the method(s) described there?
Resetting the Root Password: Unix and Unix-Like Systems

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