
阿里云ECS(ubuntu16.04.4)安装MySQL并设置远程访问(以下以root用户操作)
更新源
apt-get update
安装mysql服务器
apt-get install mysql-server
apt-get install mysql-client
apt-get install libmysqlclient-dev
查看是否安装成功(安装之前也可以使用此命令查看是否有安装MySQL)
netstat -tap | grep mysql
重启服务
/etc/init.d/mysql restart
登录(假设用户名和密码都为root)
mysql -uroot -proot
正常情况按照以上操作,应该可以进入mysql了。
远程访问MySQL,阿里云默认只能本地连接,需要以下操作支持远程访问
数据库安装后
查看数据库
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
+——————–+
4 rows in set (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 -ADatabase changed
查看host,user
mysql> select distinct host,user from user;
+———–+——————+
| host | user |
+———–+——————+
| localhost | root |
| localhost | debian-sys-maint |
| localhost | mysql.session |
| localhost | mysql.sys |
+———–+——————+
4 rows in set (0.01 sec)更改host以支持远程访问
mysql> update user set host=’%’ where user=’root’ and host=’localhost’;
刷新权限,使配置生效
mysql> flush privileges;
一般情况下,到这里就可以远程访问了,但是由于MySQL是安装在阿里云上的,阿里云默认没有开启3306端口,所以需要到阿里云控制台安全组规则配置规则,开启3306端口,默认情况下只有后五条,开启后会多出第一条,如下图
