Home >Database >Mysql Tutorial >Ubuntu下安装MySql并在外网连接_MySQL

Ubuntu下安装MySql并在外网连接_MySQL

WBOY
WBOYOriginal
2016-06-01 13:01:401307browse

纯新手教程。

1.安装mysql
apt-get install mysql-server mysql-client libmysqlclient15-dev  

安装过程中会提示为数据库root账户设置密码,输入两边密码即可

2、进入

mysql  mysql -uroot -p 

3、重新设置mysql用户root的密码

GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "123456";

4、创建新数据库 proxy

create database proxy;

5、创建用户test 对proxy库有全部操作权限

grant all privileges on proxy.* to test@localhost identified by "testpwd";

6、允许test用户可以从任意机器上登入MySQL

grant all privileges on proxy.* to test@"%" identified by "testpwd"; 

7、退出mysql

exit 

8、mysql安装完成后默认监听的地址是127.0.0.1,端口是3306。可以通过以下命令看到其监听的地址以及端口:

netstat -ntulp

9、如果监听的是127.0.0.1,那么从外网是连不上数据库的。这时可以修改监听的地址为0.0.0.0:

sudo vim /etc/mysql/my.cnf

找到bind-address=127.0.0.1这一行,将127.0.0.1改为0.0.0.0,保存退出

10、重启mysql,使配置生效

service mysql restart 

现在应该就可以在远程用MySql_Front或者phpmyadmin远程连接mysql数据库了。只不过只能用刚授权过的test账户远程登录。root账户默认是禁止远程登录数据库的。可以用以下两种办法来允许root账户远程登录数据库:

(1). 改表法。

可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"

<pre name="code" class="sql">mysql -u root -p
mysql>use mysql;
mysql>update user set host = &#39;%&#39; where user = &#39;root&#39;;
mysql>select host, user from user;

(2).授权法。

例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话:

GRANT ALL PRIVILEGES ON *.* TO &#39;myuser&#39;@&#39;%&#39; IDENTIFIED BY &#39;mypassword&#39; WITH GRANT OPTION;
如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO &#39;myuser&#39;@&#39;192.168.1.3&#39; IDENTIFIED BY &#39;mypassword&#39; WITH GRANT OPTION;

如果考虑到安全性的问题,又想禁止root账户远程登录mysql数据库:

delete from user where user = &#39;root&#39; and host = &#39;%&#39;;
select host, user from user;
flush privileges;
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