微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ubuntu上搭建mysql服务器及外部访问

安装MysqL服务器和客户端

liukang@liukang-virtual-machine:~$ sudo apt-get install MysqL-server MysqL-client

判断MysqL是否安装成功:
启动MysqL服务

liukang@liukang-virtual-machine:~$ sudo service MysqL restart

命令行登录MysqL:

liukang@liukang-virtual-machine:~$ MysqL -u root -p  
Enter password:

使用命令show global variables like ‘port’;查看端口号

MysqL> show global variables like 'port';  
+---------------+-------+ 
| Variable_name | Value |  
+---------------+-------+ 
| port | 3306 |  
+---------------+-------+ 
1 row in set (0.00 sec)

修改端口,编辑/etc/MysqL/my.cnf,注意该端口未被使用,保存退出

liukang@liukang-virtual-machine:~$ sudo vi /etc/MysqL/my.cnf  
[MysqLd]  
port=3606  
datadir=/var/lib/MysqL  
socket=/var/lib/MysqL/MysqL.sock  
user=MysqL  
# disabling symbolic-links is recommended to prevent assorted security risks 
symbolic-links=0  

[MysqLd_safe]  
log-error=/var/log/MysqLd.log  
pid-file=/var/run/MysqLd/MysqLd.pid  

"my.cnf" 11L,261C written  

liukang@liukang-virtual-machine:~$

重新启动MysqL

liukang@liukang-virtual-machine:~$ /etc/init.d/MysqLd restart

安装完成后使用Navicat连接一下,但是报错了:
2003 can’t connect to MysqL server on ‘192.168.1.4’ (10038)

本机用“ MysqL -u root -p”都可访问,但外部却无法访问?:
/etc/MysqL/my.cnf中的 bind-address = 127.0.0.1注释掉即可

liukang@liukang-virtual-machine:~$ sudo vi /etc/MysqL/my.cnf   

# Instead of skip-networking the default is Now to listen only on  
# localhost which is more compatible and is not less secure.  
# bind-address           = 127.0.0.1

修改配置记得重启MysqL

再次用Navicat连接一下,还是报错:
1130 - Host ‘192.168.1.2’ is nor allowed to connect to this MysqL server
不被允许?那就是访问权限的问题了

liukang@liukang-virtual-machine:~$ MysqL -u root -p
Enter password: 
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 40
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

copyright (c) 2000,2017,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> select host,user,password from user;
ERROR 1046 (3D000): No database selected
MysqL> use MysqL;
Database changed
MysqL> select host,password from user; +-------------------------+------------------+-------------------------------------------+
| host | user | password | +-------------------------+------------------+-------------------------------------------+
| localhost               | root             | *AEDD34275BE2CCF550FF1257B2C822B57DB858F0 | | liukang-virtual-machine | root | *AEDD34275BE2CCF550FF1257B2C822B57DB858F0 |
| 127.0.0.1               | root             | *AEDD34275BE2CCF550FF1257B2C822B57DB858F0 | | ::1 | root | *AEDD34275BE2CCF550FF1257B2C822B57DB858F0 |
| localhost | debian-sys-maint | *AFC871473228030333589B66C3085F7741545136 | +-------------------------+------------------+-------------------------------------------+
5 rows in set (0.00 sec)

MysqL> update user set host = "%" where host = "liukang-virtual-machine" and user = "root";
ERROR 2006 (HY000): MysqL server has gone away
No connection. Trying to reconnect...
Connection id:    41
Current database: MysqL

Query OK,1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MysqL> flush privileges;
Query OK,0 rows affected (0.00 sec)

MysqL> select host,password from user; +-----------+------------------+-------------------------------------------+
| host | user | password | +-----------+------------------+-------------------------------------------+
| localhost | root             | *AEDD34275BE2CCF550FF1257B2C822B57DB858F0 | | % | root | *AEDD34275BE2CCF550FF1257B2C822B57DB858F0 |
| 127.0.0.1 | root             | *AEDD34275BE2CCF550FF1257B2C822B57DB858F0 | | ::1 | root | *AEDD34275BE2CCF550FF1257B2C822B57DB858F0 |
| localhost | debian-sys-maint | *AFC871473228030333589B66C3085F7741545136 | +-----------+------------------+-------------------------------------------+
5 rows in set (0.00 sec)

MysqL>

这样在另一台电脑上使用Navicat就可以成功连接至MysqL数据库服务器了

原文地址:https://www.jb51.cc/ubuntu/352653.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐