Linux下MySQL(8.0.18)的安装
一、mysql8的安装包
下载方式一:百度网盘
链接:https://pan.baidu.com/s/1ZqMtOfwaImmQkhW7pH3Bzw
提取码:tgjd
下载方式二:官网
https://downloads.mysql.com/archives/community/
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar
二、MySQL环境清理/卸载
1、停止MySQL进程
# systemctl status mysqld
如果出现active (running),就使用命令#systemctl stop mysqld,停止即可
2、删除已经安装了的MySQL安装包
# rpm -qa|grep mysql
查询到已安装的MySQL后,执行以下命令依次卸载(若没有查询到,则说明没有安装Mysql库,可直接跳过此步)
rpm -ev mysql-community-common-8.0.18-1.el7.x86_64 --nodeps rpm -ev mysql-community-libs-8.0.18-1.el7.x86_64 --nodeps rpm -ev mysql-community-client-8.0.18-1.el7.x86_64 --nodeps rpm -ev mysql-community-server-8.0.18-1.el7.x86_64 --nodeps
3、删除MySQL的配置文件(MySQL卸载不会自动删除配置文件,需自行清理)
[root@zhizuo mysql]# find / -name mysql /usr/local/mysql /usr/lib64/mysql /var/lib/mysql /var/lib/mysql/mysql [root@zhizuo mysql]# rm -rf /usr/local/mysql [root@zhizuo mysql]# rm -rf /usr/lib64/mysql/ [root@zhizuo mysql]# rm -rf /var/lib/mysql/
4、删除mysql全局配置文件
rm -rf /etc/my.cnf
至此,又可以愉快的玩耍了~
三、安装(我这里将软件放到了/opt目录下)
1、解压安装包
[root@zhizuo opt]# tar -xvf mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar
2、移动安装包
[root@zhizuo opt]# mkdir /usr/local/mysql
[root@zhizuo opt]# mv ./*.rpm /usr/local/mysql/
[root@zhizuo opt]# cd /usr/local/mysql/
3、安装:直接粘贴即可
rpm -ivh mysql-community-common-8.0.18-1.el7.x86_64.rpm --force --nodeps rpm -ivh mysql-community-libs-8.0.18-1.el7.x86_64.rpm --force --nodeps rpm -ivh mysql-community-client-8.0.18-1.el7.x86_64.rpm --force --nodeps rpm -ivh mysql-community-server-8.0.18-1.el7.x86_64.rpm --force --nodeps rpm -ivh mysql-community-common-8.0.18-1.el7.x86_64.rpm --force --nodeps rpm -ivh mysql-community-libs-8.0.18-1.el7.x86_64.rpm --force --nodeps rpm -ivh mysql-community-client-8.0.18-1.el7.x86_64.rpm --force --nodeps rpm -ivh mysql-community-server-8.0.18-1.el7.x86_64.rpm --force --nodeps
4、查看已安装的安装包:
[root@zhizuo mysql]# rpm -qa|grep mysql
mysql-community-common-8.0.18-1.el7.x86_64
mysql-community-client-8.0.18-1.el7.x86_64
mysql-community-libs-8.0.18-1.el7.x86_64
mysql-community-server-8.0.18-1.el7.x86_64
四、初始化
1、初始化
注意:如果想要添加不区分大小写配置,这里必须先初始化不区分大小写(建议加上),后面在配置文件(my.cnf)中也要加,只要启动一次了再加就没用了。
[root@zhizuo mysql]# mysqld --lower-case-table-names=1 --initialize
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
如果遇到这步报错了,我们安装下libaio服务:yum install -y libaio
然后再执行一下:[root@zhizuo mysql]# mysqld --lower-case-table-names=1 --initialize
2、修改配置文件(添加不区分大小写配置):
vim /etc/my.cnf
lower-case-table-names=1
3、授权
[root@zhizuo mysql]# chown mysql:mysql /var/lib/mysql -R
4、开启mysql服务
[root@zhizuo mysql]# systemctl start mysqld
[root@zhizuo mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-06-11 01:27:06 CST; 6s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 1575 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 1599 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─1599 /usr/sbin/mysqld
Jun 11 01:27:05 zhizuo systemd[1]: Starting MySQL Server...
Jun 11 01:27:06 zhizuo systemd[1]: Started MySQL Server.
5、查看MySQL的root用户初始密码
[root@zhizuo mysql]# cat /var/log/mysqld.log | grep password
2022-06-10T17:18:56.887826Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: YafwAftzh7?i
四、修改密码以及开启远程访问
1、登录进去mysql [root@zhixi sbin]# mysql -u root -p ……这边输入默认密码,进行登录 2、修改mysql的密码 mysql> ALTER USER "root"@"localhost" IDENTIFIED BY "自己要设置的密码"; Query OK, 0 rows affected (0.01 sec) 3、使操作立即生效 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) 4、进入mysql目录,开放远程访问方面的权限 mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set host = '%' where user ='root'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 5、使操作立即生效 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> exit
五、(可选)授予ROOT用户SYSTEM_USER权限
在mysql中使用root用户对普通用户进行授权的时候,出现了:ERROR 1227 (42000): Access denied;
原因是MySQL8版本中新增了一个system_user帐户类型,由于root用户没有SYSTEM_USER权限,导致错误出现。
grant system_user on *.* to 'root'; FLUSH PRIVILEGES;
六、开启端口与重启防火墙
这一步主要是让程序能够远程连接,当然不考虑安全问题的话,可以关闭防火墙,这样就不需要再开放端口了:systemctl stop firewalld
1、开启防火墙 [root@zhizuo mysql]# systemctl restart firewalld 2、开放mysql对外访问的端口 [root@zhizuo mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent 3、查看端口是否被使用 [root@zhizuo mysql]# netstat -ntpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1132/sshd tcp6 0 0 :::3306 :::* LISTEN 1599/mysqld tcp6 0 0 :::33060 :::* LISTEN 1599/mysqld
连接成功: