Ubuntu下的mysql安装与数据库的远程访问

ubuntu上安装mysql需要的环境包。

1. sudo apt-get install mysql-server

2. sudo apt-get isntall mysql-client

3.  sudo apt-get install libmysqlclient-dev

 

登陆mysql数据库:

mysql -u root -p 

-u 表示选择登陆的用户名, -p 表示登陆的用户密码,上面命令输入回车后会提示输入密码,输入密码就可以登录到mysql。

Ubuntu下的mysql安装与数据库的远程访问

 

  

MySQL允许远程访问的设置

1.注释掉bind-address = 127.0.0.1。

代码如下:
>sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

 

将bind-address = 127.0.0.1注释掉(在行首加#),代码如下:
# 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

 

2.删除匿名用户

登录进数据库:
>mysql -uroot -p123456 (p后面是你的数据库密码)


然后,切换到数据库mysql,代码如下:
 >use mysql;

 >delete from user where user='';

 

3.添加允许远程访问的用户或允许现有用户的远程访问权限。

给root授予在任意主机即ip(%)访问任意数据库的所有权限。 代码如下:

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;


需要指定某个ip或主机访问,可以把%替换为IP或者主机名。

mysql> grant all privileges on *.* to 'root'@'192.168.1.100' identified by '123456' with grant option;

 

如果只是想把某个已存在的用户(例如root)修改成允许远程主机访问,则可以使用如下SQL来完成:
update user set host='%' where user='root' and host='localhost';

 

4.退出数据库

 代码:mysql> exit

 

5.重启数据库(必须)

重启数据库,才能让修改的配置生效。代码如下:

>sudo service mysql restart