# rpm -qa|grep mysql
# yum -y install wget
# rpm -qa|grep mariadb
# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
# wget http://mirrors.aliyun.com/repo/Centos-7.repo
# yum clean all --清除yum # yum makecache --清除缓存 # yum repolist --查看仓储包
# cd /tmp/ # rm -rf *
获取最新的包打开MYSQL官网wget https://dev.mysql.com/get/Downloads/
阿里云下载地址:https://mirrors.aliyun.com/mysql/
然后拼接在[wget https://dev.mysql.com/get/Downloads/]后面
wget https://dev.mysql.com/get/Downloads/mysql-community-server-8.0.27-1.el7.x86_64.rpm
本教程提供的yum源包
# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
# sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
执行成功后会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repo 及 mysql-community-source.repo
# cd /etc/yum.repos.d/ # ls -l
# yum install -y mysql-community-server
注意:上面提到过,通过wget获取的MYSQL包不是最新的前提下有可能报错,这是因为MYSQL的GPG升级了,需要重新获取
如果无这个报错可忽略跳过本步骤
报错信息如下: Failing package is: mysql-community-client-8.0.28-1.el7.x86_64 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 # yum install -y mysql-community-server
打开my.cnf文件增加以下 [skip-name-resolve] 语句到末尾(这一步可不做设置)
# vi /etc/my.cnf #跳过名称解析,提高数据库的连接速度 # skip-name-resolve
# systemctl start mysqld --启动mysql服务 # systemctl status mysqld --查看mysql服务状态 # netstat -nlpt | grep mysqld --查看端口
# systemctl enable mysqld # systemctl daemon-reload
# cat /var/log/mysqld.log | grep password
mysql -u<账号> -p<密码> -P<端口号,默认3306> -h<mysqld服务器IP,如果比指定"-h" 则表示本地登陆>
mysql -uroot -p
mysql> alter user 'root'@'localhost' identified by 'Root123..'; --修改密码(强密码) mysql> flush privileges; --刷新,立即生效 mysql> select user,host from mysql.user; --查看数据库中账号信息
弱密码会报错,如图
做这一步前提:必须是修改了默认的随机密码(执行步骤18)
关掉强密码,修改密码长度最短为6位数,修改密码为弱密码
mysql> set global validate_password.policy=0; --关掉强密码认证 mysql> set global validate_password.length=6; --密码长度最短为6位数 mysql> alter user 'root'@'localhost' identified by '123456'; --修改密码(弱密码)
# firewall-cmd --zone=public --add-port=3306/tcp --permanent --开放3306端口 # firewall-cmd --reload --重启防火墙 # firewall-cmd --list-ports --查看开放的端口
做完如上部署,重新登录进到数据库,修改user表中的Host:
# mysql -uroot -p123456 --登录数据库 mysql> show databases; --查数据来库 mysql> use mysql; --使用数据库 mysql> select Host, User from user; --查询表 mysql> update user set Host='%' where User='root'; --修改User表内root用户的Host为% mysql> flush privileges; --刷新
如果创建一个用户,给予单个库权限
mysql> create database test_data; --创建一个数据库 mysql> create user admin@'%' identified by 'Admin@123..'; --创建一个新的用户 admin 密码 Admin@123.. mysql> grant all privileges on test_data.* to admin@'%' with grant option; --授权,给test_data库的所有权限 mysql> flush privileges; --刷新
MySQL5.6版本和8.0版本的的用户授权方式不同
MySQL5.6版本的用户授权
mysql> create user test identified by '123456'; mysql> grant all privileges on *.* to 'admin'@'%'identified by '123456' with grant option; mysql> flush privileges ;
MySQL8.0版本的用户授权
mysql> create user test@'%' identified by '123456'; mysql> grant all privileges on *.* to admin@'%' with grant option; mysql> flush privileges;