Centos7安装mysql | 青训营笔记

96 阅读2分钟

最近学Linux需要安装mysql8.0但是看别人写的博客总是会出错,下面是我成功安装的步骤

一、先卸载MariaDB

rpm -qa | grep -i mariadb(检查有没有mariadb)

rpm -e  --nodeps  mariadb-libs-5.5.56-2.el7.x86_64(不检查依赖直接卸载)

二、检查是否有mysql残留(最好卸载干净,否则会出问题)

rpm -qa | grep mysql

三、下载mysql库(注意ping一下百度)

wget repo.mysql.com//mysql80-co…

注意安装wget

四、安装mysql库

yum -y install mysql80-community-release-el7-3.noarch.rpm

五、重新获取mysql的GPG(不执行的话下一步yum安装会报错)

rpm --import repo.mysql.com/RPM-GPG-KEY…

六、安装mysql(默认安装mysql8.0)

yum -y install mysql-community-server

七、开启mysql服务

systemctl start mysqld

八、检查mysql运行状态

systemctl status mysqld(如没运行,执行systemctl start mysqld)

九、查看默认密码(非必要操作)

cat /var/log/mysqld.log | grep password

目前mysql8.0已经安装完成,为了更方便地使用mysql,下面是修改密码和开启远程访问的操作。

十、设置mysql免密码登录,以便进行修改密码操作

vim /etc/my.cnf(编辑文件)

没安装vim的话使用vi /etc/my.cnf

按i编辑添加skip-grant-tables

image.png

完成以后按ESC键 输入:wq! 保存退出

这里补充一下wq是保存退出,q是直接退出,后面加!为强制操作

systemctl  restart  mysqld(重启服务让配置生效)

十一、登录mysql(已免密,按任意键登录)

mysql -uroot -p

十二、修改mysql默认密码 use mysql;(切换到mysql库)

SHOW variables LIKE 'validate_password%';(非必要操作,查询表)

set global validate_password.policy=0;(把密码限制调低)

set global validate_password.length=4;(把密码限制字数调低)

flush privileges;(刷新生效)

-- 查询一下安全模式开关

show variables like 'sql_safe_updates';

-- 关闭安全模式

set sql_safe_updates = 0;

-- 关闭安全模式下,才可以执行authentication_string为空

update user set authentication_string='' where user='root';

-- authentication_string空了的情况下,才可以真正修改密码

-- 刷新权限表

flush privileges;

-- 修改密码

alter user 'root'@'%' identified by 'S32*sdf312@';

alter user 'root'@'localhost' identified by 'S32*sdf312@';

-- 上面两个二选一,具体参考下面,一一对应

select user, host from user;

flush privileges;(刷新生效)

十三、设置root账号远程访问

select user,host,plugin from user;(查询表)

update user set host = '%' where user = 'root';(设置root任意地址访问)

update user set plugin = 'mysql_native_password' where user = 'root';(非必要操作,因mysql8.0已改为新插件caching_sha2_password,用navicat12登录会报错,可执行这条命令改回旧插件mysql_native_password即可,或者升级navicat到16版本,16版本已支持caching_sha2_password)

flush privileges;(刷新生效)

exit(退出mysql)

十四、取消免密登录

vim /etc/my.cnf(编辑文件)

把skip-grant-tables删除

wq!保存退出

systemctl  restart  mysqld(重启服务让配置生效)

ok了,关闭防火墙就可以使用了