安装 MySQL
~# sudo apt-get update
~# apt-get install mysql-server
~# apt-get install mysql-client
安装成功后启动
~# service mysql start
查看一下端口,确认mysql正常启动
~# netstat -tnl|grep 3306
数据库启动后进行初始化(都选 YES)
~# mysql_secure_installation
远程连接
打开阿里云控制台,找到自己的 ECS 实例,添加安全组规则:
20171122-103130.png授权用户
root@iZhp3c4t17pu8f04lck5odZ:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.20-0ubuntu0.16.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> GRANT ALL PRIVILEGES ON *.* TO root IDENTIFIED BY "密码";
// 然后输入,更新数据库
mysql> FLUSH PRIVILEGES;
// 你也可以授权一个叫 TestUser(自定义)的账户,并授予它远程连接的权力,命令如下:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'TestUser'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
root@iZhp3c4t17pu8f04lck5odZ:~#
尝试登录一下
$ mysql -h xx.xx.xx.xx -P 3306 -u root -p密码
ERROR 2003 (HY000): Can't connect to MySQL server on 'xx.xx.xx.xx' (60)
这是因为MySQL
没有开放远程连接,需要修改配置文件:
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
注释掉其中的bind-address = 127.0.0.1
:
重启数据库
~# service mysql restart
OK!
网友评论