MySQL is a very common relational database in Linux. For developers and system administrators, installing and configuring MySQL is one of the most important tasks in their daily work. This article will introduce how to install MySQL on the Linux operating system.
1. Install MySQL
To install MySQL on Linux, you can use the operating system's own package manager. Various Linux operating systems have different package managers. This article will cover the two most popular package managers - yum and apt-get.
Use yum to install MySQL:
sudo yum -y install mysql-server
sudo systemctl start mysqld
Install MySQL using apt-get:
sudo apt-get install mysql-server
sudo systemctl start mysql
2. Configure MySQL
After the MySQL installation is completed, some configurations are required. Below we will introduce how to configure MySQL.
By default, the MySQL root user does not have a password, so you need to set a password for the root user first to protect the database.
sudo mysql_secure_installation
This command will prompt you to set the password for the root user. After following the instructions and setting a new password, you will be asked several security questions, such as removing anonymous users, disabling root user remote access, etc. In order to enhance the security of MySQL, it is recommended that you answer "yes" to both.
You can use the following command to log in to MySQL.
sudo mysql -u root -p
This command will prompt you to enter the password of the root user. After confirmation, you can log in to MySQL.
In MySQL, it is not recommended to use the root user in the application. Instead, a new user should be created for the application. Here are the commands on how to create a new user.
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Among them, newuser is the username of the new user, and localhost is the host where the new user is located Name or IP address, password is the password of the new user.
After creating a new user, you need to authorize it. For example, the following command will grant the new user all operation permissions on all databases.
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
If you want to authorize new users to operate only on specific databases or tables, then A database or table needs to be specified.
After you complete all MySQL operations, you can enter the following command to exit MySQL.
exit
3. Conclusion
Installing MySQL on a Linux system may seem difficult, but in fact, as long as you follow the instructions, it It's very easy. Whether you are a developer or a system administrator, it is very important to learn how to install and configure MySQL well. It is recommended that you understand the basic knowledge and syntax of MySQL before using it so that you can use it better.
The above is the detailed content of Install mysql under linux. For more information, please follow other related articles on the PHP Chinese website!