How to stop and start the mysql? - mysql

When I'm use the find command to locate the MySQL location, it returns the below paths. But, I want to restart the MySQL. If I stop the path (/usr/bin/mysql) it shows the error
[Access denied to user 'root'#'localhost' with password='NO]'
so, Which MySQL will be get restart in the below paths ?
/usr/bin/mysql
/usr/share/mysql
/usr/lib/mysql
/usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/mysql
/usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi/DBD/mysql
/usr/lib64/mysql
/var/lib/mysql
/var/lib/mysql/mysql

to stop mysql account is:
sudo mysqladmin -h localhost -u root -p shutdown
to start is:
sudo systemctl start mysql.service
to confirm:
sudo mysql -h localhost -u root -p
NB:
the default password for root account is root
**not sure but i think this closes connection for all accounts & also starts for all accounts though you have stop using an account with privilege access/root access by using the grant statement.
example
GRANT ALL PRIVILEGES ON databasename.* TO 'username'#'localhost';
source http://www.luciopanasci.it/Ebooks/MySQL%20Cookbook,%203rd%20Edition.pdf
page 2 topic 1.1**

As root, run
service mysqld restart
If that does not work, try
/etc/init.d/mysqld restart
Instead of restart you can use :
start to start mysql
stop to stop it

Usually you do
/etc/init.d/mysql start/stop/restart
It may be called mysqld or mysql-server or something like that. If you're on Ubuntu, you have to do
service mysql start/stop/restart
there it may be mysqld or mysql-server or something like this as well.

If Mysql is running as a service:
sudo service mysql restart;
or
sudo service path/to/mysql restart;
Edit:
check if mysql is running
service --status-all
service --status-all | grep mysql

If Mysql is running, use
To Restart: service mysqld restart
Other command which can be used if necessary are given below.
To Stop: service mysqld stop
To Start: service mysqld start

Related

How to fix 'Unable to connect to MySQL. Debugging errno: 2002' error after fresh MAMP-installation? [duplicate]

I installed LAMP on Ubuntu 12.04 LTS (Precise Pangolin) and then set root password on phpMyAdmin. I forgot the password and now I am unable to login. When I try to change password through terminal I get:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)
How can I fix this? I am unable to open LAMP, uninstall it or reinstall it.
I once had this problem and solved it by installing mysql-server, so make sure that you have installed the mysql-server, not the mysql-client or something else.
That error means the file /var/run/mysqld/mysqld.sock doesn't exists, if you didn't install mysql-server, then the file would not exist. So in that case, install it with
sudo apt-get install mysql-server
But if the mysql-server is already installed and is running, then you need to check the config files.
The config files are:
/etc/my.cnf
/etc/mysql/my.cnf
/var/lib/mysql/my.cnf
In /etc/my.cnf, the socket file config may be /tmp/mysql.sock and in /etc/mysql/my.cnf the socket file config may be /var/run/mysqld/mysqld.sock. So, remove or rename /etc/mysql/my.cnf, let mysql use /etc/my.cnf, then the problem may solved.
Try this:
mysql -h 127.0.0.1 -P 3306 -u root -p <database>
Also (to see if it's running):
telnet 127.0.0.1 3306
Probably it is just a misconfiguration in the my.cnf file, in /etc/somewhere (depending on the Linux distribution).
I am seeing all these answers, but none offer the option to reset the password and no accepted answer. The actual question being he forgot his password, so he needs to reset, not see if it's running or not (installed or not) as most of these answers imply.
To reset the password
Follow these steps (can be helpful if you really forget your password and you can try it anytime, even if you're not in the situation at the moment):
Stop mysql
sudo /etc/init.d/mysql stop
Or for other distribution versions:
sudo /etc/init.d/mysqld stop
Start MySQL in safe mode
sudo mysqld_safe --skip-grant-tables &
Log into MySQL using root
mysql -u root
Select the MySQL database to use
use mysql;
Reset the password
-- MySQL version < 5.7
update user set password=PASSWORD("mynewpassword") where User='root';
-- MySQL 5.7, mysql.user table "password" field -> "authentication_string"
update user set authentication_string=password('mynewpassword') where user='root';
Flush the privileges
flush privileges;
Restart the server
quit
Stop and start the server again
Ubuntu and Debian:
sudo /etc/init.d/mysql stop
...
sudo /etc/init.d/mysql start
On CentOS, Fedora, and RHEL:
sudo /etc/init.d/mysqld stop
...
sudo /etc/init.d/mysqld start
Login with a new password
mysql -u root -p
Type the new password and enjoy your server again like nothing happened
This was taken from Reset a MySQL root password.
I tried the following steps:
Log in as super user or use sudo
Open /etc/mysql/my.cnf using gedit
Find bind-address, and change its value to the database server host machine's IP address. For me, it was localhost or 127.0.0.1
Save and close the file.
Come back to terminal and execute sudo service mysql start
And it worked for me.
In my case it was that the disk was full and mysqld couldn't start anymore.
Try to restart mysql service.
> service mysql restart
or
> service mysql stop
> service mysql start
If it doesn't recognize stop command then it's definitely the disk space. You should make some space in the partition mysql is allocated or make the disk larger.
Check the disk space with
> df -h
I fixed this problem by executing the following command:
mysql.server start
And if you are on a mac and used brew to install mysql, simply use:
brew services start mysql
I had a similar problem. mysql wouldn't start:
sudo service mysql start
start: Job failed to start
If I disabled apparmor:
sudo aa-complain /etc/apparmor.d/*
the problem went away. The issue was that mysqld was trying to access /run/mysqld/mysqld.sock but the apparmor profile only gave permission to /var/run/mysqld/mysqld.sock (/var/run is symlinked to /run, so these are actually the same). Not sure why mysqld isn't using the var path since that's what's set in all the configuration files, but you can fix the problem by adding the following to /etc/apparmor.d/usr.sbin.mysqld
/run/mysqld/mysqld.pid rw,
/run/mysqld/mysqld.sock rw,
I solved this by killing the mysql process:
ps -ef | grep mysql
kill [the id]
And then I started the server again with:
sudo /etc/init.d/mysql restart
But start works as well:
sudo /etc/init.d/mysql start
Then I logged in as admin, and I was done.
Somehow the MySQL server process did not create the socket, or the client is looking for the socket in the wrong place.
My first suggestion would be to check if the MySQL server is running. Second suggestion might be, is the MySQL server running on another host? If so, add the -h <hostname> flag to your MySQL client in the terminal.
If MySQL is indeed running, and running locally, check your my.cnf file. There should be a line like
socket = /var/run/mysqld/mysqld.sock
See if that matches the socket location that you mentioned in your post.
From experience, I would say the most likely scenario is your MySQL server either is not running at all or is not running on the same host as where you run your MySQL client from the terminal.
I just experienced the same issue after I had to restart my production server. I am running Debian 8.1 (Jessie) on a DigitalOcean droplet.
This is what I did to resolve my issue:
Check if the file /var/run/mysqld/mysqld.sock exists. If it doesn't, manually create it by entering touch /var/run/mysqld/mysqld.sock (which is what I had to do).
So the MySQL process can use this file. Change ownership of said file by entering chown mysql /var/run/mysqld/mysqld.sock.
Once '2' has been done, restart the MySQL service by entering service mysql restart or /etc/init.d/mysql restart.
After going through the above steps, my issue was solved. I rarely have this issue, and there is probably a better way, so by all means provide constructive feedback if need be :).
Check the "bind-adress" parameter in my.cnf.
Else try with the command:
mysql -h 127.0.0.1 -P 3306 -u root -p
-h for host 127.0.0.1, that is, localhost
-P (notice -P as uppercase) for port 3306, that is, the default port for MySQL
Your mysql-server might not be running. Ensure it runs by typing mysql.server start into the terminal.
I think whenever you get the error
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
I will recommend first to check whether your mysql daemon is running... Most of the time it will not running by default. You can check it by /etc/init.d/mysqld status.
If it's not running then start it first:
.../etc/init.d/mysqld start.
I bet it will 110% work.
If you're using Amazon EC2, and you're having this problem on the instance, then you only need to do:
sudo yum install mysql-server
sudo service mysqld restart
Amazon EC2 doesn't have a server installed (only the client is installed), so in case of that you need to install that on your instance, and after that try
mysql -u root -p
to check if that worked.
Instead of using localhost:
mysql -u myuser -pmypassword -h localhost mydatabase
Use 127.0.0.1
mysql -u myuser -pmypassword -h 127.0.0.1 mydatabase
(also note, no space between -p and mypassword)
Enjoy :)
Here's what worked for me:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
service mysql restart
This creates a link.
Make sure you have backups of important databases and then try uninstall MySQL related stuff:
apt-get remove --purge mysql\*
Then install it again:
apt-get install mysql-server mysql-client
This worked for me and data was kept.
If PHP MySQL shows errors you might have to reinstall PHP MySQL:
apt-get install php5-fpm php5-mysql
I also facing same problem it will be occur if your mysql server is not running by default it will again stop after some sec so you again run ($ sudo service mysql start ) command you can change if know.
for that use command
$ sudo service mysql start
(put user password if required because we use sudo )
and then run
$ sudo mysql -u root -p (put user password if required )
now you got your database
Fist,try to restart it with
service mysql stop
service mysql start
If above not resolve the issue, now let's go...
Uninstall completely MySQL
sudo apt-get remove --purge mysql\*
reinstall it
sudo apt install mysql-server mysql-client
test if it run
sudo mysql
Install php drivers
sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-cgi php7.4-cli php7.4-common
You can replace php7.4 by php7.x or php8.0.12 or later
Very nice !
❗️Be careful, you lose your data if they are not saved. Please backup your data if possible yet.
I got this problem too, but I just did:
sudo service mysql restart
It worked for me.
It seems your MYSQL is stopped. use below command to start MySQL again
sudo service mysql start
I FOUND THE SOLUTION
Before firing the command : mysql_secure_installation
Step 1: sudo systemctl stop mariadb
Step 2: sudo systemctl start mariadb
Step 3: mysql_secure_installation
Then it will ask root password and you can simply press Enter and set your new root password.
If your installation was recent, you should to confirm if your installation is the installation SERVER... as mysql-server-5.5.. Maybe you installed only "mysql" .. this is only client instead of the server.
If you have XAMPP installed on your Linux machine, try to copy your my.cnf file from /opt/lampp/etc/my.cnf to /etc/my.cnf.
Then, run the mysql -u root again... You should now have the correct socket and be able to run the MySQL client.
In my case, the default port 3306 was being used by some other process and thus it was not starting. After I stopped the other service and did sudo service mysql start, it worked fine. BTW, you can use something like sudo lsof -Pn -iTCP:3306 to see who may be using the port.
In my case it worked by doing some R&D:
I am able to connect to MySQL using
root-debian#mysql -h 127.0.0.1 -u root -p
But it's not working with mysql -u root -p.
I did not find any bind-address in my.cnf. So I outcommented the parameter socket=/var/lib/mysql/mysqld.sock in my.cnf which was causing me a problem with login.
After restarting the service it went fine:
root#debian:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.19 MySQL Community Server (GPL)
In my case, It seems like I wasnt really able to kill the mysql process, when I run
sudo service mysql stop
ps -ef | grep mysql
The mysql process was always there, it looks like it was blocking the socket file and new mysql process wasnt able to create it itself.
so this helped
cd /var/run
sudo cp mysqld/ mysqld.bc -rf
sudo chown mysql:mysql mysqld.bc/
sudo service mysql stop
sudo cp mysqld.bc/ mysqld -rf
sudo chown mysql:mysql mysqld -R
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
Now Im able to log in database using
mysql -u root
Then to update root password:
UPDATE user SET authentication_string=password('YOURPASSWORDHERE') WHERE user='root';
FLUSH PRIVILEGES;
PS: I had trouble updating root passwod, seems like problem with "auth_socket" plugin, so I had to create new user with full privileges
insert into user set `Host` = "localhost", `User` = "super", `plugin` = "mysql_native_password", `authentication_string` = NULL, `password_expired` = "N", `password_lifetime` = NULL, `account_locked` = "N", `Select_priv` = "Y",
`Insert_priv` = "Y", `Update_priv` = "Y", `Delete_priv` = "Y", `Create_priv` = "Y", `Drop_priv` = "Y", `Reload_priv` = "Y", `Shutdown_priv` = "Y", `Process_priv` = "Y", `File_priv` = "Y",
`Grant_priv` = "Y", `References_priv` = "Y", `Index_priv` = "Y", `Alter_priv` = "Y", `Show_db_priv` = "Y", `Super_priv` = "Y", `Create_tmp_table_priv` = "Y", `Lock_tables_priv` = "Y",
`Execute_priv` = "Y", `Repl_slave_priv` = "Y", `Repl_client_priv` = "Y", `Create_view_priv` = "Y", `Show_view_priv` = "Y", `Create_routine_priv` = "Y", `Alter_routine_priv` = "Y",
`Create_user_priv` = "Y", `Event_priv` = "Y", `Trigger_priv` = "Y", `Create_tablespace_priv` = "Y";
This creates user "super" with no password and then you can connect with mysql -u super
On Debian server Jessie, my working solution was to simply do
service mysql restart
service mysql reload
as root user
i solved this problem with restart mysql
/etc/init.d/mysql stop
and
/etc/init.d/mysql start
that's it.
You are missing permission to create /var/run/mysqld directory.So please create and
give permission as following.
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld

While trying to reset root mysql password in ubuntu I get error while running 'sudo mysql -u root' command

I have run into a very common problem while setting up a lamp stack in ubuntu. The problem that I got into is, I try to log into phpmyadmin using 'root' as username and a password that I set during instillation, I don't know if the password is wrong or I forget or something else went wrong, any way I can't login. Now I search For the solution to reset the mysql password and I get the most suggested solution which is like below:
sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables &
sudo mysql -u root
And during this third step I get the error like below:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
And Now I am stuck here, And I cannot reset password: So Help me
In /etc/my.cnf, the socket file config may be /tmp/mysql.sock and in /etc/mysql/my.cnf the socket file config may be /var/run/mysqld/mysqld.sock.
So, remove or rename /etc/mysql/my.cnf, let MYSQL use /etc/my.cnf, then the problem may be solved.
For me stopping and starting mysql did not help ('sudo /etc/init.d/mysql stop', 'sudo /etc/init.d/mysql start')
Because I had to use the command to stop mysql:
sudo /etc/init.d/mysql stop
and then run other commands
sudo mysqld_safe --skip-grant-tables &
sudo mysql -u root
before starting mysql:
sudo /etc/init.d/mysql start
But simply restarting my system that is my laptop solved my problem.

Resetting MySQL password & missing mysqld.sock file

I am trying to run a local mysql server, on my own computer. I have lost the password that I initially set up. When I try to connect to mysql, I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I have therefore tried these steps to reset my MySQL password, but the line
mysql -u root mysql
returns the same error message:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
When running the command
mysqladmin -u root -p status
I get the following message:
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
I have been checking and the file mentioned (/var/run/mysqld/mysqld.sock) doesn't actually exist. I am not sure what is causing this.
I have tried a couple of solutions online including this, this, this and this but none of this solutions worked for me. I would like to add that mysql-server is installed on my machine.
Any help appreciated. Please accept my apologies if there is any confusion to the above statements. I tried to explain what is happening as much as I can, but I am a beginner and I am clueless as to what is happenening there.
1) Stop the mysql demon process using this command :
sudo /etc/init.d/mysql stop
2) Start the mysqld demon process using the --skip-grant-tables option with this command
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
Because you are not checking user privs at this point, it's safest to
disable networking. In Dapper, /usr/bin/mysqld... did not work.
However, mysqld --skip-grant-tables did.
1) start the mysql client process using this command
mysql -u root
2) from the mysql prompt execute this command to be able to change any password
FLUSH PRIVILEGES;
3) Then reset/update your password
SET PASSWORD FOR root#'localhost' = PASSWORD('password');
4) If you have a mysql root account that can connect from everywhere, you should also do:
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
once have received a message indicating a successful query (one or more rows affected), flush privileges:
FLUSH PRIVILEGES;
Then stop the mysqld process and relaunch it with the classical way:
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
This is borrowed from https://help.ubuntu.com/community/MysqlPasswordReset , you can check also another method to reset mysql password.
Looks like your MySQL server is not running at all.
Can you check it?
service mysql status
You need to try restart it and make sure that you can stop and start it using
service mysql stop
service mysql start
If you have any errors post them here please.
Reinstalling mysql-server worked for me. (I am using mysql 5.7)
sudo wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.9-1_all.deb
sudo apt-get update
sudo apt-get install mysql-server
mysql_secure_installation
first kill user
sudo pkill -u <user>
Then install mysql server
sudo apt install mysql-server
Configure it
sudo mysql_secure_installation
Enter into mysql shell
sudo mysql

can't log into mysql as root, even after restarting with --skip-grant-tables

I'm running MySQL 5.0.77 on FreeBSD 7.3. The MySQL root password was lost, and I want to reset it. But I have the following problem.
I stopped MySQL and re-started with the --skip-grant-tables option:
/usr/local/etc/rc.d/mysqld start --skip-grant-tables
The service starts, but then when I try to log in without a password:
mysql -u root
I get this response:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
I can log in as other users (that lack the SUPERUSER privilege), but not root. Anyone know what I'm doing wrong?
this is what did the trick for me:
https://help.ubuntu.com/community/MysqlPasswordReset
I've just passed by some similiar situation, after some frustration I've realized that I have 2 MySQL installations running in my computer, one in my OS and one in a local server package. In my case this annoying situation just got resolved shutting down the SQL service of my OS
I tried with sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking" restarted the mariadb server and still got the access denied error with sudo mysql -u root.
What worked for me was this:
sudo systemctl stop mariadb
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
sudo mysql -u root
Now in the mysql console:
FLUSH PRIVILEGES;
Got an error when I did:
SET PASSWORD FOR root#'localhost' = PASSWORD('password');
But this worked:
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
Finished up with:
FLUSH PRIVILEGES;
exit
Back on shell:
sudo /etc/init.d/mysql stop
For me, I could still connect using mysql -u root so I stopped the server in a bad way: get PID(s) with ps -aux | grep mysql and terminate them with sudo kill -9 <pid>
Once we are sure mysql server is not running, restart normal way:
sudo systemctl start mariadb
Now password will be required again, but we know what root password is.
Before final systemctl start mariadb, might want to run sudo systemctl set-environment MYSQLD_OPTS="" to be safe in case you had attempted with that method.

Can't start mySQL

Even after I reset the root password with the following command I can not log to MySQL: (other commands listed to provide additional info)
# sudo dpkg-reconfigure mysql-server-5.1
# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
# telnet 127.0.0.1 3306
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
# ps -Aw |grep mysql
26522 ? 00:00:00 mysqld
# /etc/init.d/mysql start
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql start
Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start mysql
update:
# sudo mysqladmin -u root password 123
mysqladmin: connect to server at 'localhost' failed
it seems MySQL is not running properly
Update:
I am using MySQL 5.1 under Ubuntu.
It was OK until that I make some change in my.cnf to enable Remote Access. I undo my changes but problems did not solved! (Perhaps I forgot to undo some thing!)
update:
# service mysql status
mysql start/running, process 26650
# /etc/init.d/mysql status
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql status
Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the status(8) utility, e.g. status mysql
mysql start/running, process 26650
to set root password:
mysqladmin -u root password NEWPASSWORD
To recover it:
http://www.cyberciti.biz/tips/recover-mysql-root-password.html
My best guess is that if you have reconfigured / reinstalled MySQL, you need to login without a password, e.g.:
mysql -u root
or even
mysql
Edit: To see if the service is running, you could try:
service mysqld status
and if it is not running, try:
service mysqld start then check the status again.
It seems like MySQL is definitely not running. You can verify this with /etc/init.d/mysqld status or service mysqld status (don't link directly to the service command).
My spidey sense is telling me either your O/S or MySQL installation were performed improperly. It would help tremendously to know more about the (linux?) environment you're running, and how it was configured (or who configured it, in the case of PaaS).
Start as a service
sudo service mysql restart
to verify & check the service and the port
sudo netstat -tap | grep mysql
you should change the access permission. it should be 0644
sudo chmod 644 /etc/mysql/my.cnf
and then
sudo dpkg-reconfigure mysql-server-5.5