Home  >  Article  >  Database  >  解决mysql不能远程连接数据库方法

解决mysql不能远程连接数据库方法

WBOY
WBOYOriginal
2016-06-07 17:51:541158browse

一般的mysql是不建义开启远程访问权限的,这样相对来说安全一些,不过今天我来为需要远程访问权限朋友介绍一下。

1、登陆   

   mysql -u root -p

   查看user表

 代码如下 复制代码

mysql> use mysql;
Database changed
mysql> host,user,password from user;
+--------------+------+-------------------------------------------+
| host         | user | password                                  |
+--------------+------+-------------------------------------------+
| localhost    | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
+--------------+------+-------------------------------------------+
2 rows in set (0.00 sec)

 

   可以看到在user表中已创建的root用户。host字段表示登录的主机,其值可以用IP,也可用主机名,

   (1)有时想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可。

2、实现远程连接(授权法)

   将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。  
   update user set host = ’%’ where user = ’root’;

   将权限改为ALL PRIVILEGES

 代码如下 复制代码

mysql> use mysql;
Database changed
mysql> grant all privileges  on *.* to root@'%' identified by "root";
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user,password from user;
+--------------+------+-------------------------------------------+
| host         | user | password                                  |
+--------------+------+-------------------------------------------+
| localhost    | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| %            | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
+--------------+------+-------------------------------------------+
3 rows in set (0.00 sec)

这样机器就可以以用户名root密码root远程访问该机器上的MySql.

3、实现远程连接(改表法)

 代码如下 复制代码

use mysql;

update user set host = '%' where user = 'root';

这样在远端就可以通过root用户访问Mysql.


另一种方法

mysql默认是不可以通过远程机器访问的,通过下面的配置可以开启远程访问.

  我的mysql环境是ubuntu+mysql5

  1.修改/etc/mysql/my.conf,修改bind-address,指定为本机实际ip地址,你的my.con修改完大概是如下的样子

 

 代码如下 复制代码

  [mysqld]
 

 #
  # * basic settings
  #
  default-character-set=utf8
  default-storage-engine=innodb
  bind-address    = 192.168.1.107
  user            = mysql
  pid-file        = /var/run/mysqld/mysqld.pid
  socket          = /var/run/mysqld/mysqld.sock
  port            = 3306
  basedir         = /usr
  datadir         = /var/lib/mysql
  tmpdir          = /tmp
  language        = /usr/share/mysql/english
  skip-external-locking
  

2.用root登陆mysql执行如下命令

 代码如下 复制代码
 grant all on sonardb.* to sonar@'%' identified by '123456';
  grant all on sonardb.* to sonar@localhost identified by '123456';

  sonardb替换为你想访问的数据库名,sonar是你的想使用的用户名,123456替换为你的密码,这样就开启了远程访问功能.


更多详细内容请查看:http://www.111cn.net/database/mysql/34989.htm


上面的两种方法一种是直接用root用户在cmd模式下操作,一种是在配置文件里面操作,大同小义了吧。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn