2020-02-14 centos8安装mysql8及远程访问

1.下载及安装mysql

cd /usr/local/src
wget -i -c https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm
(新版本地址:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-community-server-8.0.19-1.el8.x86_64.rpm

  • yum安装mysql源,自动

yum -y install mysql80-community-release-el8-1.noarch.rpm

image.png
  • 检查安装情况


    image.png
  • yum安装mysql服务器,自动

yum -y install mysql-server

image.png
  • 安装完成后,配置文件在/etc/my.cnf

2.mysql 数据库配置

  • 启动mysql

systemctl start mysqld.service

  • 查看运行状态

systemctl status mysqld.service

image.png

这样代表启动成功

  • 找到mysql的默认密码,然后登陆,修改密码(必做)

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

image.png

可以看到mysql8默认密码为空,所以直接登陆修改密码

  • 直接登陆修改密码

mysql -u root

image.png

alter user 'root'@'localhost' identified by 'mysql';

image.png

flush privileges;
exit;

3. 允许远程连接

3.1在操作系统层面操作,检查3306端口是否开放

  • 在远程开发端,用telnet访问:


    image.png

    说明防火墙把3306端口屏蔽了,centos8不在使用iptables,默认使用firewall作为防火墙,查看一下firewall的状态:

systemctl status firewalld

image.png
  • 进入firewall的配置目录,把3306端口加入白名单:

cd /etc/firewalld/zones
vi public.xml

image.png

或者直接使用命令加白名单:

firewall-cmd --add-port=3306/tcp --permanent

  • 重载防火墙

firewall-cmd --reload

3.2在mysql中设置,允许其他ip对mysql的访问

划重点:新版的mysql中,把创建用户权限记录和授权分开了,不能同时做,以前用以下命令做可以,但是新版mysql报错(起码mysql8会报错)
grant all on . to root@'%' identified by 'mysql' with grant option; *

mysql> GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'mysql' WITH GRANT OPTION;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'mysql' WITH GRANT OPTION' at line 1

正确做法如下:

1)创建所有ip的root权限记录
create user 'root'@'%' identified by 'mysql';
2)然后授权(grant 权限列表 on 数据库 to '用户名'@'访问主机' ;(修改权限时在后面加with grant option))
grant all privileges on *.* to 'root'@'%' with grant option;
3)刷新权限(mysql8貌似不需要这一步也可以)
flush privileges

image.png

客户端访问效果:


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。