目标:远程连接虚拟机中的MySQL
效果图如下
1. VBox设置好端口转发
具体步骤请看 VM VirtualBox 网络地址转换(NAT)使用详解
2. MySQL授权
如果这时我们就去远程连接MySQL数据库,会提示
必须先授权,授权方法有多种,这里只介绍其中的一种
mysql> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
这时远程登录,仍然失败
C:UsersAdministrator>mysql -h 192.168.56.1 -P 33060 -u root -p Enter password: **** ERROR 2013 (HY000): Lost connection to MySQL server at 'waiting for initial communication packet', s ystem error: 0
3. 关闭防火墙
本地环境要关闭防火墙
虚拟机也要关闭防火墙
Linux关于启动和关闭防火墙的命令:
1) 重启后生效
开启: chkconfig iptables on
关闭: chkconfig iptables off
2) 即时生效,重启后失效
开启: service iptables start
关闭: service iptables stop
关闭防火墙之后,远程登录就成功了
C:UsersAdministrator>mysql -h 192.168.56.1 -P 33060 -u root -p Enter password: **** Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 6 Server version: 5.5.21-log Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>