269

I tried to find in some articles describing how to correctly start & stop mysql server.

I found this link: How to start/stop MySql server on Ubuntu 8.04 | Abhi's Blogging World

I ran this command:

/etc/init.d/mysql start 

but I see this error

ERROR 1045 (28000) Access denied for user....

After I tried this command:

sudo /etc/init.d/mysql start

I entered my password, and again I see the same error.

Next command:

sudo /etc/init.d/mysql - root -p start

results in:

ERROR 1049 (42000) Unknown database 'start'.

And when I run this command:

sudo service mysql start

MySQL server success started. Cool!

So, what's wrong with the other commands? Why do they result in error?

3
  • 3
    In fact, even with sudo it didn't work for me, but then I found in the script the following hint: Rather than invoking init scripts through /etc/init.d, use the service(8) and it was ok
    – Timofey
    Sep 20, 2012 at 20:21
  • 2
    Tim is correct. Use sudo service mysql start.
    – Ed Manet
    Mar 15, 2013 at 18:07
  • Generally you can use sudo -l to see what your specific user on your specific system is allowed to do with sudo. (Your permissions are configured in /etc/sudoers.) However I don't know for sure if it would help in this particular case. EDIT: Wait, never mind, the access denied error looks like it is coming from MySQL or something, not sudo. Dec 16, 2016 at 19:41

11 Answers 11

291

Your first two commands weren't run as root so that is expected behaviour. You need to be root to stop/start mysql.

However:

sudo /etc/init.d/mysql start

should work. Indeed it does, for me:

kojan:~> sudo /etc/init.d/mysql restart
[sudo] password for chris: 
Stopping MySQL database server: mysqld.
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..

I used restart rather than start, since it was already running, but the effect is the same. Are you sure you entered your password correctly? :) Have you edited your sudo config at all which would stop this working?

This one..

sudo /etc/init.d/mysql - root -p start

The arguments are wrong. an init.d script only takes start or stop or restart - just one word telling it what to do. You cannot give it multiple arguments as you were trying to do.

Anyway, the short answer is the one you actually got to work, is the recommended way. service is replacing all the init.d scripts over time, so you should get into the habit of using service. The page you link to was written in 2008, so has to be taken with some salt :)

5
  • 14
    Also try sudo service mysql start
    – user108762
    Nov 17, 2012 at 12:45
  • 2
    start mysql worked for me (Ubuntu 12.04.4).
    – Tim
    May 12, 2014 at 7:28
  • 3
    sudo /etc/init.d/mysql restart restarted MySQL for me
    – Edward
    Jul 13, 2016 at 20:23
  • 9
    how about sudo systemctl start mysql for Ubuntu 16.04 and up? Feb 2, 2017 at 3:16
  • this still worked for me on ubuntu 18
    – lewis
    Aug 28, 2020 at 7:34
114

Also helps to double check that "mysql" is the correct service name. In my case it wasn't. I kept getting following response: mysql: unrecognized service when running

service mysql status 

Then I checked /etc/init.d and found script named mysqld which listed process name: mysqld and prog=mysqld

So then I did

service mysqld status
service mysqld stop
service mysqld start 

and they all worked fine.

edit: for new versions (MySQL 8) use

service mysql status
service mysql stop
service mysql start 
6
  • 7
    mysql: unrecognized service
    – Green
    Jul 20, 2013 at 1:06
  • 7
    At least on 13.10, you'll need to run it with root privileges. eg, "sudo service mysql status".
    – Bryce
    Mar 4, 2014 at 5:09
  • 14
    For 14.10 I needed to use "mysql" not mysqld Oct 23, 2014 at 1:39
  • 1
    when running without root rights, it'll say unknown job: mysql. a bit confusing.
    – phil294
    Mar 20, 2016 at 15:55
  • 1
    Failed to start mysqld.service: Unit mysqld.service not found.
    – JCarlosR
    Nov 23, 2017 at 23:54
36

For Ubuntu 12.10 to 14.04 (Upstart)

Newer versions of Ubuntu use systemd.

START MYSQL:

sudo start mysql

RESTART MYSQL:

sudo restart mysql # The service must be running

STOP MYSQL:

sudo stop mysql # The service must be running
5
  • 5
    stop: Unknown job: mysql
    – Green
    Jul 20, 2013 at 1:07
  • @Green I just tried this (one year later) and it is still working. This is my result: mysql stop/waiting. You must know that you have to be already running mysql to stop this ;)
    – Lucio
    Jul 24, 2013 at 22:45
  • 1
    Hero, you save my life, that mysqld has been occupied my machine memory for so long. I tried all kinds of methods to remove it, but all failed, until I met your answer.
    – Zen
    Sep 2, 2014 at 13:07
  • 2
    sudo: start: command not found
    – JCarlosR
    Nov 23, 2017 at 23:55
  • 2
    @JCarlos Do apt install upstart
    – Edward
    Dec 28, 2017 at 17:06
14

I got a strange error when I installed mysql-workbench on my Ubuntu machine. After that I tried to start the mysql service using this command:

service mysql start

This showed that the MySQL server was not installed, so I installed it and my problem was solved. The command to install mysql-server is:

sudo apt-get install mysql-server

After successful installation, start the MySQL server:

service mysql start
11

I had the same issue with Ubuntu64 and I fixed by simply not using systemctl but instead this:

sudo service mysql restart

hopefully this command will work for you.

7

After installing MySQL on your system run this command:

service mysql status

If the service is down run:

service mysql start

To stop it:

service mysql stop
1
  • 1
    /etc/init.d/mysql start and service mysql start are exactly the same.
    – storm
    Jul 26, 2017 at 8:55
4

This one should work for the manually built mysql server:

sudo /usr/local/mysql/bin/mysqld_safe --user=mysql
2

On Ubuntu 18.04 the socket is defined in the file /etc/mysql/mysql.conf.d/mysqld.cnf:

 socket     = /var/run/mysqld/mysqld.sock

The directory was not present so I created it manually as below:

sudo mkdir -p /var/run/mysqld

Restart MySQL. That solved it for my case here.

1
  • This was definitely the case with mine! Had to make a directory.
    – Shaze
    Apr 28, 2020 at 13:34
1

use this command to start SQL server as the root user

sudo service mysql restart

OR

sudo /etc/init.d/mysql start

Restart with this command

sudo /etc/init.d/mysql restart

Stop MySQL server with this command

sudo /etc/init.d/mysql stop

Check if MySQL is running:

sudo service mysql status
1

I got stuck with the same issue. I uninstalled and reinstalled with these commands:

sudo apt-get --purge remove mysql-server mysql-common mysql-client
sudo apt update && sudo apt dist-upgrade && sudo apt autoremove 
sudo apt-get install mysql-server mysql-client
0

A little script to cover both cases [server running/server not running]:

#!/bin/bash

service mysql restart
if [ "$?" != "0" ]; then
  service mysql start
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .