Table of contents
2. Detailed steps for MySQL installation
Download Yum source configuration file
Install Yum source configuration file
Run security scripts (recommended for production environments)
4. Detailed steps for uninstalling MySQL
Delete MySQL data files and configuration files
Uninstall Yum source configuration file
Clean up MySQL users and groups
Clean MySQL log files and temporary files
1. Preface
MySQL5.7 is one of our commonly used Web databases. Installation and uninstallation under Linux may encounter many pitfalls. This article takes the installation and uninstallation of community version MySQL5.7 under Centos7 as an example to record the installation in detail. and the uninstallation process for your reference. If you like it, you can like it and save it, and follow us to not get lost.
operating system | MySQL |
Centos7.9 | MySQL5.7 Community Edition |
Note: Make sure to log in to the system as root user to install
2. Detailed steps for MySQL installation
Update package
yum update -y
Check if MySQL is installed
rpm -qa | grep -i mysql
If you find that it has already been installed, please refer to Part 4 of MySQL Uninstallation.
Download Yum source configuration file
# noarch表示该软件包不依赖特定Linux发行版
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
Install Yum source configuration file
Execute installation command
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
By default, two configuration files will be generated in the /etc/yum.repos.d directory.
Install MySQL server
yum -y install mysql-community-server
The following 5 rpm packages will be installed by default
Note: The installation may fail here, prompting the exception message that Public key for mysql-community-common-5.7.43-1.el7.x86_64.rpm is not installed:
Cause of the problem: MySQL key expires
Solution: Import new key information
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
Re-execute the installation. If the following message appears, the installation is successful.
Start the MySQL server
systemctl start mysqld
View MySQL status
systemctl status mysqld
active (running) means it has been started successfully and is running.
View MySQL version
mysql -V
Results of the
Get temporary password
grep "password" /var/log/mysqld.log
Results of the
Set root password
You can use the following two methods to set the root password when logging in for the first time:
Run security scripts (recommended for production environments)
Execute security scripts to strengthen MySQL security and set a new root password
mysql_secure_installation
Enter temporary root password
Set a new root password,The default security policy is MEDIUM, which requires at least 8 characters, including at least one uppercase and lowercase letter, one special character, and one number, otherwise Report... Failed! Error: Your password does not satisfy the current policy requirements exception.
After entering the required password, you can continue with the following configuration
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
# 移除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
# 禁用远程root登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
# 删除测试数据库及权限
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
# 重新加载授权表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
Modify security policy
Execute login
mysql -uroot -p
Enter the temporary password and enter MySQL
Modify the security policy and use a simple password to log in
# 查看当前安全策略
show variables like 'validate_password%';
# 最低安全策略(0->LOW ,1->MEDIUM, 2->STRONG)
set global validate_password_policy=0;
# 最小密码字符长度是4
set global validate_password_length=4;
# 设置本机root密码
alter user 'root'@'localhost' identified by 'root';
After the setting is successful, enter the quit command and use the modified password to log in again. This method only takes effect temporarily and will become invalid after restarting MySQL. If it needs to take effect permanently, you need to modify /etc/my.conf and restart the MySQL service.
Set up startup
systemctl enable mysqld
Stop MySQL server
systemctl stop mysqld
3. MySQL usage examples
remote access
Log in to the machine, empower and set a password for the root user
# 授权,all表示所有ddl和dml操作权限,*.*表示针对所有数据库所有表,%表示本机和远程主机均能访问
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
# 刷新权限
flush privileges;
Turn off local firewall
systemctl stop firewalld
Executed through the remote host, enter the password to access
mysql -h 192.168.5.11 -uroot -p
result
4. Detailed steps for uninstalling MySQL
Stop MySQL service
Before uninstalling MySQL, you first need to stop the MySQL service
systemctl stop mysqld
Uninstall the MySQL package
yum -y remove mysql-server mysql-client mysql-common mysql-libs
The following results indicate that the uninstallation is complete:
Delete MySQL data files and configuration files
MySQL’s data files and configuration files are usually located at /var/lib/mysql
and /etc/my.cnf,执行删除命令
rm -rf /var/lib/mysql
rm -rf /etc/my.cnf
Uninstall Yum source configuration file
rpm -e mysql57-community-release-el7-11.noarch
Clean up MySQL users and groups
userdel mysql
groupdel mysql
Clean MySQL log files and temporary files
rm -rf /var/log/mysql
rm -rf /tmp/mysql*