Press ESC to close

MySQL Installation and Management on Ubuntu

Contents

MySQL Installation

Ubuntu operating system is one of the most preferred Linux distributions, especially in server environments. Thanks to its powerful and flexible structure, it can work compatible with many database management systems.

In this guide, we will provide step-by-step information on how to install and manage MySQL database server on Ubuntu. Whether you are a beginner or an experienced system administrator, you will feel better equipped to install and manage MySQL with the explanations in this article series.

The first step to install MySQL is to open the terminal and run the following command:

				
					sudo apt update -y
				
			

This command will update the package lists and prepare your system for the latest versions. Next, to install MySQL packages:

				
					sudo apt install mysql-server -y
				
			

You can use the command. During installation, you may be asked to set a password for the root user. Make sure you choose a secure password.

When the installation is complete, to check if the MySQL service is active:

				
					sudo systemctl status mysql.service
				
			

You can use the command. If the service is not active, to start it:

				
					sudo systemctl start mysql.service
				
			

run the command.

After installing the MySQL server, it is recommended to adjust some configuration settings for security and ease of use. To adjust these settings, you can run the following command in the terminal:

				
					sudo mysql_secure_installation
				
			

This script offers a number of security measures, such as strengthening the root password, removing anonymous users, disabling remote root user login, and deleting the test database. You will be guided at every step and you will be able to make adjustments according to your preferences.

Creating MySQL User and Database

You can use the MySQL command line to create a database with a new user in MySQL and assign permissions to this user. First, log into MySQL:

				
					sudo mysql -u root -p
				
			

Then to create a new user:

				
					CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'strong_password';
				
			

Use the command. Here, replace the ‘user_name’ and ‘strong_password’ sections with the username and password you specified.

To create a new database:

				
					CREATE DATABASE database_name;
				
			

To assign database permission to the user:

				
					GRANT ALL PRIVILEGES ON database_name.* TO 'user_name'@'localhost';
				
			

Precautions you need to take to ensure MySQL security include password policies, network security, and regular backups. For example, using strong passwords and changing passwords regularly are important steps for security. Additionally, allowing access to your database only from trusted networks and deleting unnecessary MySQL users are also ways to increase security.

MySQL Configuration Settings

Once you have successfully installed the MySQL server, you will need to make some basic configuration settings for performance and security. In addition to ensuring MySQL runs efficiently, these settings will also increase the security of your database. Now, let’s examine step by step how to configure MySQL configuration settings.

First, we will edit the MySQL configuration file. This file is usually located at /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf. You can open the configuration file with the following command via terminal:

				
					sudo apt-get install -y nano
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
				
			
The important settings you need to make in the configuration file are:
  • bind-address: Determines from which IP address the MySQL server can be accessed. It is usually set to 127.0.0.1 for security reasons to only allow access from the local machine.
  • max_connections: Determines the maximum number of clients that can connect to the server at the same time. You can increase or decrease this value according to your needs.
  • character-set-server and collation-server: Determines the database’s character set and comparison order. It is usually set to utf8 and utf8_general_ci .
Below is a sample table of some basic settings you need to make in the configuration file:
Setting Description Sample Value
bind-address Specifies from which IP address the MySQL server can be accessed. 127.0.0.1
max_connections Specifies the maximum number of clients that can connect to the server at the same time. 151
character-set-server Specifies the character set of the database. utf8
collation-server Specifies the comparison order of the database. utf8_general_ci
After making the settings, be sure to save and close the configuration file. You will need to restart the MySQL server for the changes to take effect. You can restart the server using the following command:
				
					sudo systemctl restart mysql
				
			

Setting your configuration settings correctly will ensure the healthy and secure operation of your MySQL server. Remember, these settings are only recommended to get started. You may need to make more advanced configurations depending on the needs of your system.

MySQL Security Precautions

Securing your MySQL database server is the first step in protecting against cyber attacks. In this section, we will discuss the security measures you can implement for your MySQL server.

First, you need to set the password for the root user. Changing the default password assigned during installation prevents possible unauthorized access. You can use the following command for this:

				
					sudo mysql_secure_installation

				
			

Second, remove anonymous users. Anonymous users can access your database without authentication. To remove these users, enter the following commands in the MySQL command line:

				
					DELETE FROM mysql.user WHERE User'';
FLUSH PRIVILEGES;
				
			
As a third step, disable remote root access. Restricting the root user’s remote access increases security. To do this, use the following SQL command:
				
					UPDATE mysql.user SET Host'localhost' WHERE User'root';
FLUSH PRIVILEGES;
				
			
Fourth, remove unnecessary default databases. Default databases, such as the test database, can create security vulnerabilities. To delete these databases, use these commands:
				
					DROP DATABASE test;
FLUSH PRIVILEGES;
				
			
Step five, tighten database permissions. Ensure users have only the necessary permissions. For example, to grant a user permission to create a database, you can use the following command:
				
					GRANT CREATE ON *.* TO 'kullanici_adi'@'localhost';

				
			

Sixth, back up your database regularly. In case of possible data loss, you can quickly restore your data. Various tools and commands can be used for backup.

Step seven, keep your database server up to date. Close known vulnerabilities by applying security patches and updates regularly.

Finally, implement strong password policies and ensure user passwords are changed regularly. Make sure that passwords are complex and difficult to guess.

Besides the steps mentioned above, it is important to consider additional security measures such as firewall rules, encrypted connections, etc. MySQL security is an ongoing process and should be reviewed regularly.

Leave a Reply

Your email address will not be published. Required fields are marked *