A Guide to Starting and Restarting MySQL in Ubuntu

Try this guide with our instant dedicated server for as low as 40 Euros

MySQL is one of the most popular open-source relational database management systems, widely used for web applications and other data-driven projects.

Generally, MySQL plays nice with Linux distributions, and managing a MySQL installation and keeping it available requires attention to server management processes. As the common cliché goes, restarting a MySQL server solves some issues that affect the performance of a MySQL server.

In this article, we will guide you through the process of starting and restarting MySQL in Ubuntu, ensuring that you get your database up and running smoothly without any hiccups. Next, we’ll go into the details of stopping the server. Finally, we’ll present solutions to several challenges in managing a MySQL server.

Table Of Contents

  1. How to Start a MySQL Server in Ubuntu
    1. Method #1: Use systemctl
    2. Method #2: Use the service Command
    3. Method #3: Use the MySQL Initialization Script
    4. Method #4: Use the mysqld_safe Command
  2. Stopping MySQL Server on Ubuntu
    1. Method #1: Use systemctl
    2. Method #2: Use the service Command
    3. Method #3: Use the /mysql Script
    4. Method #4: Use the mysqld_safe Command
    5. Method #5: Use the mysqladmin shutdown Command
    6. Method #6: Use the kill Command
    7. Method #7: Use the MySQL Client
  3. Restart the mysql Service
    1. Method #1: Use systemctl
    2. Method #2: Use the service Command
    3. Method #3: Use the script Method
  4. Common MySQL Errors and Their Solutions
    1. Error 1045: Access Denied
    2. Error: Can’t Bind to Address or Port
    3. Error: Configuration File Issues
    4. Error: Insufficient Resources
    5. Error: Can’t Connect to Local MySQL Server Through Socket
  5. Conclusion
  6. FAQs

Let’s start with the prerequisites.

Prerequisites to Working with MySQL Server

Before you go through this tutorial, you should have the following:

  • A machine running a stable Ubuntu version
  • A user account with administrator or sudo rights

If you don’t have MySQL on your machine, install it by entering the following commands in the terminal:

sudo apt-get update
sudo apt-get install mysql-server

Prerequisites to Working with MySQL Server

Now that you have a MySQL installation on your machine, let’s go ahead and start it.

How to Start a MySQL Server in Ubuntu

You can opt for one of the following methods for starting the MySQL server on your Ubuntu machine.

Method #1: Use systemctl

Like most current Linux distributions, Ubuntu uses systemd’s systemctl command to manage service operations, including starting, stopping, and restarting. In our case, we’ll use it to start the MySQL server on Ubuntu. Note that this process works for all Debian-based distros.

Launch the terminal and enter the following command to start the MySQL server:

sudo systemctl start mysql

Note: If you are in an RHEL-based distribution, the process will remain the same with a slight change in the command:

sudo systemctl start mysqld

The command doesn’t provide any status, and thus, you have no idea of the success or failure of the process. But you can verify the status of the MySQL service with the following command:

sudo systemctl status mysql

Method #1: Use systemctl

The output of the command will display MySQL’s current status, indicating whether it’s active.

Also Read: 3 Simple Methods to Delete Duplicate Rows in MySQL Database

Method #2: Use the service Command

Older Linux versions do not use systemd, and thus the previous systemctl command won’t be effective.

If that’s the case with your Ubuntu installation, use the service command to start the MySQL server.

Launch the terminal and enter the following command:

sudo service mysql start

Method #2: Use the service Command

Note that on RHEL-based distributions, the name of the service will be mysqld.

The command will not provide any status, and you need to confirm the MySQL server start by checking the status of the MySQL service with the following command:

sudo service mysql status

Important: You can use this method with the newer distribution versions because of the ongoing support for the service command.

Method #3: Use the MySQL Initialization Script

In some earlier Ubuntu and CentOS versions, admins used the /mysql script to manipulate the state of the MySQL service. The default location of this script was the /etc/init.d directory.

In the current distro versions, this script is deprecated and replaced with the systemctl command.

So, if you’re on a version that supports the /mysql script, we recommend locating it with the find command. Next, launch the script with the following command:

sudo /etc/init.d/mysql start

Method #3: Use the MySQL Initialization Script

As you can see, on our test machine running a recent Ubuntu version, the command launched systemctl to start the MySQL service.

Method #4: Use the mysqld_safe Command

The mysqld_safe command starts the MySQL server in safe mode, where the script configures relevant environment variables and performs error checks before starting the MySQL server.

When you run the following command in the terminal, it initiates the MySQL server process in the foreground, displaying the process details and any errors.

sudo mysqld_safe

Method #4: Use the mysqld_safe Command

The command displays a success message indicating the success of the launch process. It’s worth mentioning that the command takes a bit longer because it conducts error verification before starting the launch process.

Stopping MySQL Server on Ubuntu

Occasionally, you need to stop the MySQL server for maintenance or security.
We’ll go through several methods of stopping the MySQL server. You’ll see that all the methods of starting the server are represented on the list because the process of stopping the server is usually connected to the method of starting the server.

Method #1: Use systemctl

If you started the MySQL server with the systemctl command, the best way to stop the server is the following command that works on Debian and RHEL-based servers:

sudo systemctl stop mysql

sudo systemctl stop mysqld

Next, verify that the MySQL service has stopped with the following command:

sudo systemctl status mysql

Method #1: Use systemctl

Method #2: Use the service Command

If you are on an older Linux distribution that does not use systemd, you have the option of service command:

sudo service mysql stop

After this, check the status of the MySQL service with the following command:

sudo service mysql status

Method #3: Use the /mysql Script

In earlier versions of popular Linux distributions, admins used init scripts in the /etc/init.d directory to manage services, including MySQL. If you used this method to launch the MySQL server, use the following command to stop the service:

/etc/init.d/mysql stop

Method #4: Use the mysqld_safe Command

If you started the MySQL server via the mysqld_safe script, use the –shutdown argument to stop the server. This command halts the MySQL server by directly terminating the MySQL process.

sudo mysqld_safe --shutdown

Method #4: Use the mysqld_safe Command

During the MySQL server shutdown, the command writes its output to the error.log file.

Important: There is a chance that the shutdown command could fail, or the process could result in data loss before the server stops.

Method #5: Use the mysqladmin shutdown Command

mysqladmin is a command-line administrative tool included with the MySQL server installation. You can stop the MySQL server by running the command with the shutdown parameter:

sudo mysqladmin shutdown

This command force-stops the MySQL server, disregarding active client connections or ongoing transactions. As a result, this command can lead to data loss and corruption.

Method #6: Use the kill Command

The kill command is a general-purpose command for stopping active processes. You need the process ID (PID) for the command. So, first, you need to find the PID for the mysqld process with the following command:

ps aux | grep mysqld

Method #6: Use the kill Command

As you can see, the PID of the mysql process is 8157. Next, use the kill command to stop the mysql service.

sudo kill 8157

Method #7: Use the MySQL Client

The MySQL client is a command-line tool that offers a simple interface for executing SQL commands, including a convenient way of stopping the MySQL server.

First, you need to log in to the MySQL client with the following command:

sudo mysql -u root -p

When prompted, enter the MySQL root password.

Once the client is active, type SHUTDOWN at the prompt to terminate the server and press Enter.

Method #7: Use the MySQL Client

Finally, enter quit to exit to the terminal.

Restart the mysql Service

Once you have shut down the MySQL server, you need to bring it back online by restarting the MySQL server.

We usually recommend the systemctl command for restarting the server. However, if you’re on an older version that doesn’t support systemd, use the service command.

Method #1: Use systemctl

If you used systemctl command to shut down the MySQL server, use the following command to restart the server:

sudo systemctl restart mysql

Method #1: Use systemctl

Remember to check the MySQL service status with the following command:

sudo systemctl status mysql

Method #2: Use the service Command

Use the following command to restart the MySQL server:

sudo service mysql restart

Once the command finishes, check the status of the MySQL service to make sure it is running correctly:

sudo service mysql status

Method #3: Use the script Method

Although newer Ubuntu versions and other distros now use systemd, some distributions still support the old scripts to check MySQL server status for compatibility reasons.

If you stopped your server with a /mysql script, use the following command to restart it:

/etc/init.d/mysql restart

Do you know that restarting the MySQL service is a critical step in creating a MySQL database? Read more about this critical activity in our comprehensive tutorial on creating a database in MySQL from the command-line.

Also Read: How To Rename a Database in MySQL: 3 Easy Methods

Common MySQL Errors and Their Solutions

Now that you know how to start and restart a MySQL server in Ubuntu, let’s look at some common error messages you may encounter when working with the MySQL database service.

Error 1045: Access Denied

Solution: Start MySQL with ‘sudo’ to ensure you have the necessary permissions.

Error: Can’t Bind to Address or Port

Solution: Check for port conflicts or incorrect IP bindings with other services. Change the current MySQL port if needed.

Error: Configuration File Issues

Solution: Review the MySQL configuration file (my.cnf) for errors, typos, or missing settings.

Error Insufficient Resources

Solution: Ensure the server has enough memory and disk space for MySQL to run. Free up space if necessary.

Error: Can’t Connect to Local MySQL Server Through Socket

Solution: Verify the MySQL socket file’s location and permissions. It should match the details in the configuration.

If you encounter an error, the recommended course of action is to carefully examine error messages and logs to identify the problem and then apply the appropriate solution.

Conclusion

Understanding how to start and restart MySQL in Ubuntu is a fundamental skill for database management. By understanding the commands, permissions, and configurations involved, you can ensure a smooth and efficient MySQL experience.

If you’re looking for reliable hosting infrastructure to run your MySQL server, consider RedSwitches bare metal hosting services. With our robust infrastructure and support, you can enjoy a seamless MySQL experience, backed by a reliable support team.

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What’s the default method to start MySQL in Ubuntu?

The default way to start MySQL in Ubuntu is by using the following command:

sudo service mysql start

Q. Are there alternative methods to starting and restarting MySQL?

Yes, you can also use the systemctl command:

sudo systemctl start mysql

To restart MySQL:

sudo systemctl restart mysql

Q. How do I check the version of MySQL server on Ubuntu?
To check the version of MySQL Server on Ubuntu, use the following command in the terminal:
mysql --version

Q. How do I restart the MySQL server using XAMPP on Ubuntu?
To restart the MySQL server using XAMPP on Ubuntu, open the XAMPP Control Panel, go to the Manage Servers tab, and click the Restart button next to the MySQL server.

Q. Can I restart the MySQL server using the service command?

Yes, you can restart the MySQL server using the service command in Ubuntu. Open a terminal and run the following command to restart the MySQL service:

sudo service mysql restart

Q. How do I connect to the MySQL server on Ubuntu?

A. To connect to the MySQL server on Ubuntu, run the following command to connect to the MySQL server:
mysql -u root -p

Q. Where can I find the MySQL server version number in Ubuntu?

To find the MySQL server version number on Ubuntu, run the following command;

mysql -u root -p

Once connected, run the following SQL query:
mysql> SELECT VERSION();

Q. How do I set the root password for the MySQL server in Ubuntu?

To set the root password for the MySQL server in Ubuntu, run the following command to start the script:
sudo mysql_secure_installation

Follow the on-screen prompts to configure the MySQL server, including setting the root password.

Q. How do I check the status of the MySQL server in Ubuntu?

To check the status of the MySQL server on Ubuntu, run the following command to check the MySQL service status:
sudo systemctl status mysql

Try this guide with our instant dedicated server for as low as 40 Euros