Linux - MySQL 8.0(二)基本操作:远程连接(Yum、Apt)

19 篇文章 6 订阅

此学习文是基于MySQL 8.0写的
得益于大神朋友的悉心指导解决不少坑,才写出此文,向大神奉上膝盖

前言

参考文章备注
Linux - 在线安装卸载MySQL 8.0(Yum、Apt)前置依赖
MySQL - MySQL 8.0基本操作:用户后置依赖

Yum(Red Hat、CentOS)

一、配置

1. 查看初始化密码

[nangy@nangy-vm ~]$ sudo cat /var/log/mysqld.log | grep password
2019-07-15T00:42:14.636290Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: sx+H1gspzouH

2. 登录MySQL

首次登录用初始化密码

[nangy@nangy-vm ~]$ mysql -u root -p

3. 修改初始化密码

建议修改初始化密码,安全好记
密码一定要大小写字母+数字+符号,如:Root@2019

mysql> alter user 'root'@'localhost' identified with caching_sha2_password by '{new_root_pass}';
  • 身份验证插件类型
    1. mysql_native_password(MySQL 5.7版本)
    2. caching_sha2_password(MySQL 8.0版本)

4. root用户远程设置

# 切换mysql库
mysql> use mysql;
# 更新host,任意地址用 %
mysql> update mysql.user set host='%' where user='root';
# 查询验证一下,plugin是默认加密验证插件
mysql> select host, user, plugin from mysql.user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)

5. 允许root用户从任何主机访问数据库

库名:要远程访问的数据库名称,所有的数据库使用 *
表名:要远程访问的数据库下的表的名称,所有的表使用 *
用户名:要赋给远程访问权限的用户名称
IP地址:可以远程访问的电脑的IP地址,所有的地址使用 %
密码:要赋给远程访问权限的用户对应使用的密码

# 开放远程访问权限
mysql> grant all privileges on *.* to 'root'@'%' with grant option;
# 刷新权限
mysql> flush privileges;
  • 补充:如果报错,不允许使用GRANT创建用户
    重新登陆root再次执行即可!
ERROR 1410 (42000): You are not allowed to create a user with GRANT

6. 创建dbadmin用户

使用Dbadmin@123456密码从任何主机连接到mysql服务器

# 创建新用户,密码永不过期
mysql> create user 'dbadmin'@'%' identified with mysql_native_password by 'Dbadmin@123456' password expire never;
# 开放权限
mysql> grant all privileges on *.* to 'dbadmin'@'%'  with grant option;
# 查询验证一下
mysql> select host, user, plugin from mysql.user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | dbadmin          | mysql_native_password |
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
  • password expire never:口令永不过期
  • with grant option:表示该用户可以将自己拥有的权限授权给别人。

7. 允许myuser用户从指定IP登录

例如:IP为192.168.1.12的主机连接到mysql服务器,并使用mypassword作为密码
下面是个例子,不会执行成功的,需改成自己的用户和密码

mysql> grant {grant_type} on {schema}.{table} to 'myuser'@'192.168.1.12' identified by 'mypassword' with grant option;

Apt(Debain、Ubuntu)

一、配置

Apt的在线安装方式已经设置了root密码
直接使用密码登陆就ok,后续操作与Yum方式一致


坑!

  1. Navicat连接报 10060 "Unknown error" 错误
    在这里插入图片描述
  • 关闭防火墙(生产环境千万别这么玩,应该设置特定端口的入站规则
# Yum
启动: sudo systemctl start firewalld
关闭: sudo systemctl stop firewalld
开机启用: sudo systemctl enable firewalld
开机禁用: sudo systemctl disable firewalld
查看状态: sudo systemctl status firewalld

# Apt
  • 版本兼容性
    原因:MySQL 8.0 更换了默认的密码策略,MySQL 5.8开始将 caching_sha2_password 作为默认的身份验证插件,低版本Navicat不支持而已
    解决办法:登陆用户的加密规则改成旧的 mysql_native_password 就行,或者升级Navicat为最新版本
#修改加密规则,密码永不过期
mysql> alter user 'root'@'%' identified by '{password}' password expire never;
#更新密码(mysql_native_password模式)
mysql> alter user 'root'@'%' identified with mysql_native_password by '{newpassword}';
# 查询验证一下
mysql>  select host, user, plugin from mysql.user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | nan              | mysql_native_password |
| %         | root             | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值