新安装的 MySQL 只有一个 root 用户,默认不开启远程连接。下面以 Ubuntu 系统下的 MySQL 为例,说明如何远程连接 MySQL 数据库。

  • 登录 root 用户 mysql -uroot -pyongdu
  • 查看 mysql 数据库中的所有表
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hr                 |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
  • 切换到 mysql 数据库
  • 查看有哪些表
mysql> use 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> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)
  • 查看root用户的配置
mysql> select host,user from user where user = 'root';
+-----------+------+
| host      | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1       | root |
| localhost | root |
| myubuntu  | root |
+-----------+------+
4 rows in set (0.00 sec)
  • 发现 root 用户配置的 host 信息是 localhost 或者 127.0.0.1,所以在本机可以通过 localhost 或者 127.0.0.1 连接到 mysql 数据库
mysql> quit
Bye
ubuntu@MyUbuntu:~$ mysql -uroot -pyongdu -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 46
  • 其中-h代表指定的 ip 主机,用本机的 ip 尝试连接 mysql -uroot -pyongdu -h 192.168.43.246 发现无法登陆,这是因为 root 只允许 localhost 和 127.0.0.1 进行登陆
ubuntu@MyUbuntu:~$ mysql -uroot -pyongdu -h 192.168.43.246
ERROR 1045 (28000): Access denied for user 'root'@'192.168.43.246' (using password: YES)
  • 想要进行远程连接,可以考虑增加一个用户 GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'admin' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'admin' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • 这条指令是增加一个 admin 用户,密码为 admin,连接的主机设置为“%”,代表任意的 ip 可以连接,同时给 admin 赋权进行所有操作
  • 执行 flush privileges;,使上述设置生效
  • 再次查看 user 表,按 admin 查询
mysql> select host,user from user where user = 'admin';
+------+-------+
| host | user  |
+------+-------+
| %    | admin |
+------+-------+
1 row in set (0.00 sec)
  • 发现 admin 用户创建成功,且 host 对应的是“%”,这样便可以尝试远程连接,有些情况下可能还会连接失败。有的主机还有对 mysql 启动时的设置:/etc/mysql/myf bind-address = 127.0.0.1
    在这个配置文件里也绑定了 ip,可用命令 sudo find / -name myf 查找到该文件,将 bind-address 这一行注释掉,或者改为 0.0.0.0
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
#
# * Fine Tuning
  • 然后重启 mysql 服务 sudo service mysql restart
  • 使用 admin 用户进行远程连接
ubuntu@MyUbuntu:~$ mysql -h 192.168.43.246 -uadmin -padmin
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
  • 成功登陆,下面在 windows 下用 Navicat 工具连接到 msql 数据库,虚拟机和主机需联网
  • 测试成功后点击确定
  • 连接完成,此时便可用图形工具操作数据库了

更多推荐

如何远程连接MySQL数据库