Ubuntu16.04安装MySQL

  本篇教程在示例步骤中使用了以下版本的软件。操作时,请您以实际软件版本为准。

  • 操作系统:Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-105-generic x86_64)
  • MySQL 版本:MySQL 5.7.24-0ubuntu0.16.04.1 (Ubuntu)
  • MySQL 可视化工具:Navicat for MySQL 10.1.7-enterprise

一、MySQL 下载与安装

  1. 更新 apt# apt-get update
  2. 这里使用的是 apt 软件包安装:
    • 安装 mysql-server# apt-get install mysql-server(安装的过程中会提示您为 root 用户设置密码、确认密码并按下回车 ok 即可。
  3. 测试 MySQL 是否安装成功:# netstat -tap | grep mysql
    • 打印类似信息为安装成功: tcp 0 0 localhost:mysql *:* LISTEN 6902/mysqld
  4. 使用 root 用户登陆 MySQL:mysql -uroot -p
    • 输入密码:输入刚才安装过程中设置的密码
    • 到这一步你可能会遇到这样的错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    • 打开 cat /etc/mysql/my.cnf 后你会发现该配置文件引入了 !includedir /etc/mysql/conf.d/!includedir /etc/mysql/mysql.conf.d/
    • 打开配置文件 vim /etc/mysql/mysql.conf.d/mysqld.cnf
    • i 键进入编辑模式
    • [mysqld] 后面任意一行添加 skip-grant-tables 用来跳过密码验证的过程
    • 按 Esc 键退出编辑模式,输入 :wq 保存并关闭文件。
  5. 重启 MySQL:service mysql restart

二、修改 MySQL 编码为 utf8

  1. 登陆 MySQL:mysql -uroot -p,并输入登陆密码
  2. 查看 MySQL 编码:s(参见附录1)show variables like '%char%';(参见附录2)
  3. 打开配置文件 vim /etc/mysql/mysql.conf.d/mysqld.cnf
    • i 键进入编辑模式
    • [mysqld]lc-messages-dir = /usr/share/mysql 的后面添加 character_set_server=utf8
    • 按 Esc 键退出编辑模式,输入 :wq 保存并关闭文件。
  4. 打开配置文件 vim /etc/mysql/conf.d/mysql.cnf
    • i 键进入编辑模式
    • [mysql] 后面添加 default-character-set=utf8
    • 按 Esc 键退出编辑模式,输入 :wq 保存并关闭文件。
  5. 重启 MySQL:service mysql restart
  6. 登陆 MySQL 后使用 sshow variables like '%char%'; 查看修改结果。

三、配置 MySQL 远程连接

  注意:在 MySQL 中所执行的语句 / 命令最好都以 ; 结束;遇到使用 utf8 编码的地方则需要使用 utf8,而不是 utf-8

  1. 设置 MySQL 允许远程访问:
    • 首先编辑配置文件:vim /etc/mysql/mysql.conf.d/mysqld.cnf
    • i 键进入编辑模式
    • 注释掉 # bind-address = 127.0.0.1
    • 按 Esc 键退出编辑模式,输入 :wq 保存并关闭文件。
  2. 登陆 MySQL,查看 MySQL 用户:select host,user from mysql.user;
  3. 创建新用户:create user '用户名'@'%' identified by '密码';
  4. 授权远程连接 MySQL 的用户:grant all privileges on *.* to '用户名'@'%' identified by '密码';
    • % 表示所有的电脑都可以连接,也可以设置某个 IP地址 进行连接
    • 更新用户密码:update user set password=password("新密码") where user="root";
    • 更改权限过程中可能会出现的问题:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements,提示密码不符合要求,这应该是安装的时候,您选择了密码强度为 Strong
    • 这里使用更改密码策略:set global 更改密码策略:set global validate_password_policy=0;
    • 设置密码长度:set global validate_password_length=6;
    • 更新密码:set password=Password('123456');
    • 退出 MySQL:quitqexit;
    • 使用新密码重新登陆 MySQL,继续进行授权操作。
  5. 刷新权限表,使配置生效:flush privileges;
  6. 重启 MySQL:service mysql restart
  7. 添加监听端口号 3306iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
    • 查看是否被监听:iptables -L -n
    • 设置防火墙打开 3306 端口:ufw allow 3306
  8. 最重要的一步,登录阿里云 —> 控制台 —> 云服务器ECS —> 网络和安全 —> 安全组,在入方向点击配置规则进行配置,3306 端口是访问服务器 MySQL 的,没有的话就添加规则,端口范围选择 3306/3306,授权策略为 允许,授权协议为 MySQL(3306),授权对象设置为 0.0.0.0/0,允许所有外部 IP 访问。
    • 查看 3306 端口是否被监听:netstat -an | grep 3306
    • 显示 tcp6 0 0 :::3306 :::* LISTEN
  9. 远程连接 MySQL 测试,这里偷懒使用了 Windows 的命令行工具(参见附录3)

四. 参考链接

附录 1

  • 查看 MySQL 版本信息与编码:s
mysql> s
--------------
mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper

Connection id:			3
Current database:	
Current user:			root@localhost
SSL:					Not in use
Current pager:			stdout
Using outfile:			''
Using delimiter:		;
Server version:			5.7.24-0ubuntu0.16.04.1 (Ubuntu)
Protocol version:		10
Connection:				Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:			/var/run/mysqld/mysqld.sock
Uptime:					16 sec

Threads: 1  Questions: 5  Slow queries: 0  Opens: 107  Flush tables: 1  Open tables: 26  Queries per second avg: 0.312
--------------

附录 2

  • 查看 MySQL 编码:show variables like '%char%';
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
  • 设置 MySQL 编码为 utf8
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

附录 3

  • CMD 远程访问 Ubuntu 16.04 下 MySQL 测试:
C:Userswumz>mysql -h 公网IP地址 -u 授权用户 -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.24-0ubuntu0.16.04.1 (Ubuntu)

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>
原文地址:https://www.cnblogs.com/wumz/p/10068917.html