Linux(CentOS 7.6) Install MySQL 5.7.x

A good memory is not as good as a bad pen. Record the process of installing MySQl.

Before installing MySql, if MariaDB is also installed...
Newbies don't know how to make them coexist. The method I chose is to uninstall MariaDB.

1. Uninstall Mariadb (optional)

If you have installed MariaDB before and want to uninstall it.

卸载语句
	rpm -qa | grep mariadb 查询是否安装了MariaDB
	yum remove mariadb 卸载mariadb,同时也卸载了mariadb-server
 	yum list installed | grep mariadb 发现在安装mariadb时作为依赖项的mariadb-libs没有被删除。
 	yum remove mariadb-libs 将其卸载
 	rm -rf /etc/my.cnf
	rm -rf $(find / -name mysql) 删除所有包含mysql的文件(夹)
 	reboot

Insert image description here
Insert image description here
Insert image description here

2. Install MySQL

First create a folder to store mysql.

语句如下:
	cd // 返回根目录
	ls 查看目录下的所有文件
	cd usr 进入usr文件夹
	mkdir tools 创建一个文件夹,名字叫做tools
	cd tools 进入tools文件夹
	yum list installed | grep mysql 查看系统中是否已安装MySQL 服务,一般情况下不存在
	yum -y remove mysql-libs.x86_64 如果已安装则删除MySQL 及其依赖的包

Insert image description here
Insert image description here
download

语句
	wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm 下载
	ls 查看目录,可以看到已经下载好了
	rpm -ivh mysql57-community-release-el7-8.noarch.rpm 安装

Insert image description here
If, like me, you have downloaded many times, you will get a lot of files like this, just delete them.

Insert image description here
If you execute the installation statement, like me, this prompt appears.
Execute such a statement: sudo yum remove mysql57-community-release-el7-8.noarch

Insert image description here
Insert image description here
Insert image description here
Enter the etc folder, find yum.repos.d, and install MySql.

语句:
	cd etc 进入etc文件夹
	cd yum.repos.d 找到它
	ls 查看文件
	yum install mysql-server 安装MySql

Insert image description here

If the following problems occur during the installation of mysql

Insert image description here

This is a version problem. First import the correct key URL and then install the MySQL service. (2022 represents the year)

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

Insert image description here

Check the MySql version, start the MySql service, and check the service status

语句:
	mysql -V 查看版本
	service mysqld start 或者 systemctl start mysqld.service 启动服务
	service mysqld status 或 systemctl status mysqld.service 查看服务状态
	grep “password” /var/log/mysqld.log 获取临时密码
	mysql -u root -p 登陆MySql

Insert image description here

Insert image description here
Use a temporary password to change the password after entering MySql.
It should be noted that the generated password can only be used for the root account and is temporary. You need to change the password immediately after logging in. No operations can be performed without changing the password. After changing the password Restart MySQL to take effect.

However, if you are like me and accidentally exit the database without successfully changing the password, and you can't get in, you can proceed to the third step. ↓

3. Change MySQL password

Modify the configuration file and directly skip the database password verification.
Enter the etc folder and open the my.cnf file. Add skip-grant-tables at the end.
After saving the modification, restart the MySql service. At this time, you can log in to MySql without a password.

语句:
	cd etc 进入etc文件夹
	vim my.cnf 打开配置文件,添加内容
	service mysqld restart 重启MySql服务
	mysql -uroot -p 登陆MySql

Insert image description here

在打开的my.cnf文件中,添加以下语句:
	skip-grant-tables
	
关于vim的一些操作
vim是文本编辑器
创建文件/打开文件 : vim [文件名]
进入插入模式:
	vim打开文件后是普通模式,需要进入插入模式才能进行文本编辑.
	使用i键可以进入插入模式.
保存:
	在插入模式下不能保存文件,需要先回到普通模式,按下esc回到普通模式.
	在普通模式下输入:w再按下回车即可保存文件.
退出:
	在插入模式不能退出,需要先回到普通模式.
	在普通模式下输入:q再按下回车即可退出.
	也可以直接使用:wq同时执行保存和退出.

Insert image description here
Insert image description here
Insert image description here
Insert image description here

In MySql, use sql statements to change passwords.

语句:
	use mysql;
	update user set password=password(“你要设置的密码”) where user=‘root’; 修改密码
	update user set authentication_string = password('你要设置的密码') where user='root' ;(版本5.7及以上的修改密码语句)
	quit; 退出

Insert image description here
If, like me, you have the following error. (mysql version is 5.7 and above)
change the password change statement to the following:
update user set authentication_string = password('The password you want to set') where user='root ' \gAfter
Insert image description here
Insert image description here
the modification is successful, you can go back and modify the configuration file my.cnf, delete the skip password verification skip-grant-tables, save and exit.
Then you can use the modified password to log in to MySql.


After logging in, when I want to create a database, ↓ appears

Insert image description here

You have to change the password again.
Change the password statement: alter user user() identified by 'Admin2022!';
This password cannot be too simple.

Insert image description here

4. Modify the default port of MySQL (optional)

语句:
netstat -tlpn | grep mysql 查看端口号
cd /etc 到etc文件夹
vim my.cnf  编辑配置文件my.cnf,在里面修改端口(port:5426(已有port改端口号,没有加port))
service mysqld stop  关闭mysql服务
service mysqld restart 重启mysql服务
netstat -tlpn | grep mysql 再次查看端口号

Insert image description here
Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/tenju/article/details/134720383