1.登陆mysql:mysql -u root -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec) mysql> use mysql; # 切换到mysql数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> select host,user from user; # 查询user表
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
5 rows in set (0.00 sec)
如果上述查询结果,root用户对应的host不为%,则修改用户权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root密码' WITH GRANT OPTION; # 修改权限
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
或创建用户
mysql>insert into user (host,user,password) values('122.114.155.163','admin',password('ygpassword')); mysql>FLUSH PRIVILEGES; mysql>GRANT ALL PRIVILEGES ON *.* TO 'admin'@'122.114.155.163'IDENTIFIED BY 'ygpassword' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES;
完成。(如果连接不成功,继续执行下面操作)
2.阿里云的ECS需要手动添加安全组规则
3.关闭防火墙(关闭防火墙,就可以外部访问了。不受端口限制。生产环境,最好开启防火墙,开启部分端口。)
1.永久有效
开启: chkconfig iptables on
关闭: chkconfig iptables off
2.即刻生效
开启: service iptables start
关闭: service iptables stop
3.开启部分端口
vim /etc/sysconfig/iptables
添加想要开启的相关端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
4.重启防火墙服务
service iptables restart
方法4:(腾讯云)
配置腾讯云服务器的安全组开放3306端口