10
11
12
13
14
15
[mumu@lelehoo/]$mysql-uroot-p
Enter password:
Welcome tothe MySQL monitor.Commands endwith;or\g.
Your MySQL connection idis3405
Server version:5.6.38-log MySQL Community Server(GPL)
Copyright(c)2000,2017,Oracle and/orits affiliates.All rights reserved.
Oracle isaregistered trademark of Oracle Corporation and/orits
affiliates.Other names may be trademarks of their respective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthe current input statement.
MySQL[(none)]>
进入mysql数据库,并查询host,user,password
MySQL[mysql]>usemysql
Database changed
MySQL[mysql]>select host,user,password from user;
+-----------+------+-------------------------------------------+
|host|user|password|
+-----------+------+-------------------------------------------+
|%|root|*0E28557A303B4D10AF15A0B2182F02EC937D2BDE|
|127.0.0.1|root|*0E28557A303B4D10AF15A0B2182F02EC937D2BDE|
+-----------+------+-------------------------------------------+
可以看到在user表中已创建的root用户。host字段表示登录的主机,其值可以用IP,也可用主机名。
如果查询结果的host中没有你的电脑ip或者’%’,执行下面的
2、实现远程连接(授权法)
将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。
MySQL[mysql]>updateusersethost='%'whereuser='root';
将权限改为ALL PRIVILEGES
MySQL[mysql]>grantallprivilegeson*.*toroot@'%'identified by"数据库密码";
QueryOK,0rowsaffected(0.00sec)
MySQL[mysql]>flushprivileges;
QueryOK,0rowsaffected(0.00sec)
编辑防火墙,增加端口[mumu@lelehoo~]$sudovi/etc/sysconfig/iptables
点击I,切换到插入模式,将如下添加到配置文件中,按esc,输入:wq回车保存
-AINPUT-mstate--state NEW-mtcp-ptcp--dport3306-jACCEPT
重启防火墙[mumu@lelehoo~]$sudo systemctl restart iptables.service
部分机器可能还是无法连接上MySQL,可能是阿里云安全组没有配置,详情可以查看阿里云安全组配置
配置好了,这样机器就可以以用户名和密码远程访问该机器上的MySQL.