设置 Linux 服务器中 MySQL 允许远程访问

开启 MySQL 远程访问权限: 在linux系统上登陆mysql服务。

-- root 是用户名
[root@localhost ~]# mysql -u root -p
Enter password: --  输入密码

创建远程连接 MySQL 的用户:

-- 创建用户、密码及权限范围 第一个 roo t为用户名 @后为适用的主机,‘%’表示所有电脑都可以访问连接,第二个 root 为密码
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.0.2' IDENTIFIED BY 'root' WITH GRANT OPTION;                 
Query OK, 0 rows affected (1.57 sec)

-- 立即生效
mysql> flush privileges;
Query OK, 0 rows affected (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> select host,user from user;
  +-----------+------------------+
  | host      | user             |
  +-----------+------------------+
  | %         | lys              |
  | %         | mcAdmin          |
  | %         | root             |
  | %         | zcbox            |
  | localhost | debian-sys-maint |
  | localhost | mysql.session    |
  | localhost | mysql.sys        |
  | localhost | phpmyadmin       |
  +-----------+------------------+
  8 rows in set (0.00 sec)

  -- 更改用户权限:

  mysql> update user set host = 'localhost' where user = 'mcAdmin';
  Query OK, 1 row affected (0.00 sec)
  Rows matched: 1 Changed: 1 Warnings: 0

  -- 删除用户:  

  mysql> delete from user where user = 'mcAdmin';
  Query OK, 1 row affected (0.00 sec)

  

-- 立即生效
 mysql> flush privileges;
 Query OK, 0 rows affected (0.00 sec)
可见user实际上是一个表,对其操作与普通的mysql操作没有什么不同。

此时旧版的mysql,就已经让数据库可以外部访问了

但在mysql5.7中,上面操作可能仍然不能让你外部访问本地服务器数据库,因为5.7中MySQL的配置文件里默认也是仅允许本地访问
所以还要修改MySQL的配置文件
一般来说在路径/etc/mysql/mysql.conf.d/mysqld.cnf下
通过vi/vim/gedit等编辑器打开此文件

注释掉 bind-address          = 127.0.0.1
然后 sudo service mysql restart 重启mysql

搞定!

查看端口: 

扫描二维码关注公众号,回复: 6808944 查看本文章
mysql> show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.01 sec)

貌似还有可能是3306端口的问题,配置文件中也有看到port = 3306 这一行,但是没影响就先不管了;
遇到了再解决

猜你喜欢

转载自www.cnblogs.com/wangtong111/p/11201734.html