Article directory
I. Introduction
MySQL is a portable database that can run on almost all current operating systems. This article focuses on how to install MySQL on Centos7. During the installation process, it is recommended to always run as root user to reduce the cost of operation.
2. Uninstall unnecessary environments
In your Centos7 system, MySQL or mariadb (a branch of MySQL) may have been pre-installed and run as a daemon process in the background. We must first stop these services before we can delete the original installation package.
- Use to
ps ajx
display whether mysqld exists (the same is true for mariada, and I won’t repeat it later)
- Use
systemctl stop
the command to stop the mysql service
If the system does not have mysql, this step is skipped
3. Delete the default installation package
- Use
rpm -qa | grep mysql
the command to display whether the mysql package is installed in the system
- If there is, use
rpm -qa | grep mysql | xargs yum -y remove
the command to delete all installation packages. The function of the xargs instruction is to "feed" the previous data to the following instructions one by one, so as to achieve the purpose of batch operation. Note-y
Cannot be omitted, otherwise errors will occur during batch processing - After deleting, check again to confirm that it has been completely deleted.
If the mysql installation package cannot be found in the system, skip this step directly
4. Obtain the official source of mysql
[Purpose] : Finally, you can use yum install
the command to install with one click
[Problem] : There may be no mysql source in the default yum source in the cloud server
[Solution] : First, manually pull the mysql yum
source from the official website (note that it is not to download mysql)
- First get the official yum source of mysql > yum source link
- Right-click to view the source code of the web page to see more file details
- Pay attention to version issues when installing
-
Choose
el
the version with the words in the name.el
Refers to Enterprise Linux , which is the abbreviation of centos
-
Select the version with 57 in the name , 57 means the version of mysql is 5.7, otherwise the download is the latest version of mysql
-
It is best to install the mysql version that is consistent with your own system , otherwise there may be software compatibility issues. If there is no suitable version, our principle is to choose a mysql version newer than the system version , which is generally forward compatible
-
Use
cat /etc/redhat-release
the command to view the current centos version
-
Select the version mysql that matches the operating system. Everything in the picture below will do
-
- Use
rz
the command to upload the locally downloaded mysql source to the cloud server environment
- Use
rpm -ivh
the command to install the rpm package
. So far we have completed the installation of the official source. Check the yum source again and find two more items. The essence of installation under Linux is to copy files to a specific path.
Use the following command to check whether the yum source of mysql is working properly: yum list | grep mysql
. If you can filter out mysql-related packages, it means that the yum source is working properly
Five, install mysql service
We have solved the problem of the mysql yum source before, and now we can use the yum command to install it with one click yum install -y mysql-community-server
. Although we seem to have only installed mysql-community-server, yum will automatically handle dependencies, and mysql clients, components, libraries, etc. will be automatically installed.
- Common Mistakes: Key Expiration Problems
Use the following command to reactivate the key:rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
- Verify that the installation was successful
6. Set password-free login
- You can log in without password by adding this sentence in the mysql configuration file:
skip-grant-tables
. At this point we do not need a password to log in to mysql. If the service has been started before, then use the commandsystemctl restart mysqld
to restart mysql, and the configuration information will take effect. - Use the command
systemctl start mysqld
command to start the mysql server (note that it is not mysql) - Use the command to log in
mysql -uroot -p
as the root user