Home >Database >Mysql Tutorial >How to install MySQL on Linux and set a password

How to install MySQL on Linux and set a password

PHPz
PHPzOriginal
2023-04-20 10:15:161654browse

MySQL is a popular open source relational database management software used by many web applications, forums, blogs, and social networks. Operating systems like Ubuntu, Debian, CentOS or other Linux-based operating systems include MySQL packages, which make installing MySQL on Linux very easy. However, when installing MySQL, you need to set a password for the root user to protect the security of your data. Next, let us learn how to install MySQL on a Linux system and set a password.

Install MySQL

On Ubuntu, you can install MySQL with the following command:

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

On CentOS or RHEL, you can install MySQL with the following command:

sudo yum update
sudo yum install mysql-server

After the installation is complete, you need to start the MySQL service and ensure that it starts automatically when the system starts. On an Ubuntu system, you can run the following command:

sudo service mysql start
sudo systemctl enable mysql

On a CentOS or RHEL system, you can run the following command:

sudo service mysqld start
sudo systemctl enable mysqld

Set the password of the MySQL root user

After the MySQL server is installed, you need to set a password for the root user. On Ubuntu systems, you can use the following command to set the password:

sudo mysql_secure_installation

This command will prompt you to enter the root user password. If you do not set an initial password, you can directly press Enter. Next, the system will prompt you to enter a new password, confirm password, delete anonymous users, disable root remote login, etc. We only focus on the step of setting the Root user password.

On CentOS or RHEL systems, you can use the following command to set the password after installing MySQL:

sudo mysqladmin -u root password 'newpassword'

This will change the password of the root user to "newpassword", which you can customize as needed New password.

Test MySQL connection

After installing and setting the MySQL password, we need to test whether the MySQL connection is working properly. We can use the following command to test:

mysql -u root -p

This command will prompt you to enter the root user password to connect to the MySQL server. If the password is correct, you will be able to successfully log in to the MySQL server.

Summary

Installing MySQL and setting a password on a Linux system is very simple and requires only a few simple commands. Setting a password can protect your data security to a certain extent. At the same time, it is also recommended that you use a strong password to prevent the system from being attacked by hackers. In future application development, MySQL will be a very useful database management software on which you can create, maintain and process massive amounts of data.

The above is the detailed content of How to install MySQL on Linux and set a password. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn