In RedHat, install MySQL8 in RPM Bundle mode. It is recommended that you must use the RPM Bndle version to install it, which is all-inclusive.
Official website download: https://dev.mysql.com/downloads/mysql/
1. Uninstalling mariadb will conflict with the MySQL installation.
rpm -qa | grep mariadb Check whether there is mariadb
If there is, remove it yum -y remove mariadb-libs.x86_64
2. Check if there is numactl
rpm -qa | grep numa
If you do not have the numactl library, or it is not a 64-bit version, you need to install it. There are many online tutorials, so you can find it by yourself.
3. Unzip tar -xvf mysql-8.0.13-1.el7.x86_64.rpm-bundle.tar
Install 6 packages that must be installed and need to be installed in order.
rpm -ivh mysql-community-common-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.32-1.el7.x86_64.rpm
4. Start mysql
Start the service: service mysqld start
View status: service mysqld status
5. Log in to MySQL8
Check the temporary password grep 'password' /var/log/mysqld.log
2023-02-11T10:58:32.259446Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: >5wo7dI(atEg
Log in using temporary password:
mysql -u root -p and press enter to enter the temporary password
6. Change new password
alter USER 'root'@'localhost' IDENTIFIED BY 'root user's password';
7. Set up remote login for MySQL root user
use mysql;
select host, user from user;
If the host is localhost, execute update user set host = "%" where user='root';
Refresh takes effect flush privileges;
Solve the problem that some clients cannot connect, such as dbvisualizer reporting
Unable to load authentication plugin 'caching_sha2_password
alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root user's password';
Refresh takes effect flush privileges;
8. Change the time zone. Generally, there is no need to modify it, just in case.
View the current time.
select now() from dual;
If it is found that it is inconsistent with the current system time, it needs to be modified according to the actual situation.
set persist time_zone='-5:00';
flush privileges;
View modified time zone and current time
show variables like '%time_zone%';
select now() from dual;
9.考虑到以后可能有空间大小限制,变更一下数据目录
当前目录:/var/lib/mysql
目标目录:/opt/mysql
#先做个备份,有备无患
cd /var/lib
mkdir mysqlbak
cp -a -R /var/lib/mysql/* /var/lib/mysqlbak
#转移mysql目录到/opt
mv /var/lib/mysql /opt/
chown -R mysql:mysql /opt/mysql
#修改my.cnf
vim /etc/my.cnf
此时,启动mysql服务正常,但是 mysql -u root -p 登录会报错:
Can 't connect to local MySQL server through socket '/var/lib/mysql.sock '(2)
分析了一下,提示说无法通过socket文件/var/lib/mysql/mysql.sock连接到mysql服务器,
也就是对于mysql程序来说,尽管我把socket文件从/var/lib/mysql/mysql.sock转移到了
/opt/mysql/mysql.sock,在my.cnf里指定了socket文件位置,
但是对于mysql还是会从默认的安装目录/var/lib/mysql/里找这个mysql.sock文件,
找不着,就不知道从哪里启动了。所以,做一个软连接,让mysql能访问到移动后的 mysql.sock。
#在 /var/lib 目录,创建一个空的mysql
mkdir mysql
chown mysql:mysql mysql
#做一个软连接
ln –s /opt/mysql/mysql.sock /var/lib/mysql/
ll /var/lib/mysql
这一步很关键,网上很多做这个软连接的做法,但是都是错误的,在这一步试了好长时间才找对办法。
做完软连接后,启动 mysql 服务,能正常登录了。
10.开放防火墙端口,否则不能远程访问
#防火墙命令
启动防火墙:systemctl start firewalld
关闭防火墙:systemctl stop firewalld
重启防火墙:systemctl restart firewalld
查看防火墙:systemctl status firewalld
查看已开启的端口:firewall-cmd --list-ports
#添加端口3306
firewall-cmd --zone=public --add-port=3306/tcp --permanent
systemctl restart firewalld
firewall-cmd --reload
firewall-cmd --list-port
#关闭指定端口
firewall-cmd --zone=public --remove-port=3306/tcp --permanent
systemctl restart firewalld
firewall-cmd --reload
firewall-cmd --list-port
#查看端口被哪一个进程占用
netstat -lnpt |grep 3306