Ubuntu18.04下安装MySQL5.7(支持win10-wsl环境)
注意: 本文操作环境为win10系统wsl下的Ubuntu18.04,对于原生的Ubuntu18.04同样适用。MySQL默认版本为5.7,其他版本不适用。
安装步骤
1.更新源:
sudo apt update
2.安装mysql:
sudo apt install mysql-server
wsl下使用上述命令安装就直接安装上去了,没有设置密码的地方,这时候无论怎么登陆,都无法登录上去。
chenyc@DESKTOP-Q5J25HR:~$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
chenyc@DESKTOP-Q5J25HR:~$
设置root用户密码:
chenyc@DESKTOP-Q5J25HR:~$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
发现无法设置,看报错信息,说的是不能连接到mysqld.sock套接字,猜想是mysql服务没有开启。
开启mysql服务:
chenyc@DESKTOP-Q5J25HR:~$ sudo service mysql start
* Starting MySQL database server mysqld
No directory, logging in with HOME=/ [ OK ]
重新设置密码:
使用sudo mysql_secure_installation命令,有几个地方需要用户确认。
Press y|Y for Yes, any other key for No:y
选y,前面提示大致的意思是:默认使用空的密码连接,该种连接方式可以作为测试使用,但是不安全,问是否要重新设置密码。
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
这里是让选择密码强度,从前面提示可以知道,有LOW,MEDIUM,STRONG三种强度可选,我们选择0即可。
New password:
Re-enter new password:
这个没什么好说的,让设置密码,并确认新密码。
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
问是否移除匿名用户,匿名用户留着也没什么用,可以移除掉,选择y。
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
是否禁止root用户远程登录,在没有设置其他用户之前,只能通过root用户登录,所以不能禁止,选择n。
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n
是否移除测试数据库,选择n。
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : n
是否重新载入特权表,最好不动它,选否。
下面是设置密码的全过程。
chenyc@DESKTOP-Q5J25HR:~$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 25
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : n
... skipping.
All done!
以上设置完成后,使用刚刚设置的root用户密码,再次连接数据库,成功连接上。
chenyc@DESKTOP-Q5J25HR:~$ sudo 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.29-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, 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>
编码设置
查看编码:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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默认有很多是latin1的编码格式,对汉字的支持不好,因此需要改成utf8编码格式。
修改mysql数据库编码的步骤:
1.停止mysql服务:
chenyc@DESKTOP-Q5J25HR:~$ /etc/init.d/mysql stop
* Stopping MySQL database server mysqld
cat: /var/run/mysqld/mysqld.pid: Permission denied
[fail]
发现没有权限,加上sudo再去执行:
chenyc@DESKTOP-Q5J25HR:~$ sudo /etc/init.d/mysql stop
* Stopping MySQL database server mysqld [ OK ]
服务停止成功。
2.修改配置文件:
进入配置文件目录:
cd /etc/mysql/mysql.conf.d
修改之前先备份:
sudo cp mysqld.cnf mysqld.cnf.2
接下来修改配置文件,执行如下命令:
sudo vim mysqld.cnf
修改两个地方:
# 修改处1:添加以下2行
[client]
default-character-set=utf8
[mysqld]
# 修改处2:添加以下3行
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
修改完成后,重启数据库:
chenyc@DESKTOP-Q5J25HR:~$ sudo service mysql start
* Starting MySQL database server mysqld [ OK ]
再次登录,发现登不上了:
chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
使用sudo cat /etc/mysql/debian.cnf查看debian-sys-maint密码,发现密码是下面这个玩意:
chenyc@DESKTOP-Q5J25HR:~$ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = LMCuPijw9kX5cRsS
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = LMCuPijw9kX5cRsS
socket = /var/run/mysqld/mysqld.sock
因此,可以使用debian-sys-maint用户先登录上去,修改密码:
chenyc@DESKTOP-Q5J25HR:~$ mysql -udebian-sys-maint -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, 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>
使用上面的password,成功登录。
修改密码,执行如下语句:
UPDATE mysql.user SET authentication_string=PASSWORD('你的密码'), PLUGIN='mysql_native_password'
WHERE USER='root';
执行时发现报错:
mysql> UPDATE mysql.user SET authentication_string=PASSWORD('cyc2010'), PLUGIN='mysql_native_password' WHERE USER='root';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
说不符合安全规范,估计是之前设置安全级别的那地方的问题,需要重新设置一下:
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
validate_password_length(密码长度)参数默认为8,修改为1:
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
再次修改密码成功。
mysql> UPDATE mysql.user SET authentication_string=PASSWORD('cyc2010'), PLUGIN='mysql_native_password' WHERE USER='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
授权远程登录:
mysql> grant all privileges on *.* to 'root'@'%' identified by 'cyc2010' with grant option;
Query OK, 0 rows affected, 1 warning (0.01 sec)
设置完成后,再次重启服务:
chenyc@DESKTOP-Q5J25HR:~$ sudo /etc/init.d/mysql restart
* Stopping MySQL database server mysqld [ OK ] * Starting MySQL database server mysqld No directory, logging in with HOME=/ [ OK ]
使用root用户登录:
chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, 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>
成功登录,查看编码格式:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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)
发现都变成了utf8,说明设置成功了。
系统重启后失效问题
重启wsl-Linux子系统:
//WSL-Ubuntu18.04 LTS 重启方法
//以管理员权限运行cmd
>net stop LxssManager //停止
>net start LxssManager //启动
重启系统后,再次登录,发现又登不上了:
chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
看报错信息,应该是mysql服务没有启动,启动服务:
chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ sudo service mysql start
[sudo] password for chenyc:
* Starting MySQL database server mysqld No directory, logging in with HOME=/
[ OK ]
再次登录,发现登陆成功。
chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ mysql -u root -p Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, 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>
Ubuntu18.04下安装MySQL5.7(支持win10-wsl环境)的更多相关文章
- Ubuntu18.04下安装mysql5.7超详细步骤
1.首先执行下面三条命令: #安装mysql服务 sudo apt-get install mysql-server #安装客户端 sudo apt install mysql-client #安装依 ...
- Ubuntu18.04下安装搜狗输入法
Ubuntu18.04下安装搜狗输入法 第一步:安装 fcitx输入框架 sudo apt-get install fcitx 第二步:在官网下载 Linux 版本搜狗输入法 https://piny ...
- Ubuntu18.04下安装MySQL
Ubuntu上安装MySQL非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client ...
- Ubuntu18.04下安装Sublime Text3并解决不能输入中文
Ubuntu18.04下安装Sublime Text3并解决不能输入中文! 废话不多说,直接按顺序执行下面命令开始安装! wget -qO - https://download.sublimetext ...
- Ubuntu18.04下安装Sublime Text3!
这几天安装了Ubuntu18.04,然后在里面安装Sublime Text3,结果各种问题!各种BUG!试了网上各种办法!尼玛!都是坑爹的啊! 最后还是楼主自己解决了…… 废话不多说,直接按顺序执行下 ...
- ubuntu18.04下安装mysql后无法用mysqlworkbench访问
问题描述:我在ubuntu18.04下执行以下命令安装mysql时遇到了mysqlworkbench无法连接root用户的问题.ubuntu18.04下默认安装mysql时是5.7版本的,但是5.7版 ...
- Win10 WSL Ubuntu18.04 编译安装MySQL5.7
---恢复内容开始--- 在win10 商店中选择 ubuntu18.04 下载地址 http://dev.mysql.com/downloads/mysql/ wget https://cdn.my ...
- win10子系统Ubuntu18.04下安装图形界面
前提:windows 10 已经安装WSL(windows subsystem for linux),并能正确运行Bash. 要想使用Linux的图形用户界面通常有两种方法,一种是使用X-Window ...
- Ubuntu 16.04下安装MySQL5.7
原文链接:https://www.linuxidc.com/Linux/2017-06/144805.htm ps:ubuntu14.04下默认安装的是MySQL5.5 首先执行下面三条命令: sud ...
随机推荐
- 你的胃能Hold住未来的食物吗?
如果你是一名美食客,那么一定会发现现在越来越多的食物已经发生了翻天覆地的变化,很多食物正在以我们未知的形式出现在生活中,其中最大的莫过于分子美食.你想过吗?当食物发生改变的时候,你的胃是否能够Ho ...
- python 初学者
明确学习目标,不急于求成 当下是一个喧嚣.浮躁的时代.我们总是被生活中大量涌现的热点所吸引,几乎没有深度阅读和思考的时间和机会.我始终认为,学习是需要沉下心来慢慢钻研的,是长 期的:同时,学习不应该被 ...
- 致敬——C语言
2016年4月26日21:00 OJ考试平台关闭,C语言这门课程也就随之结束了. 回顾学习历程,坦诚的讲学习期间也努力过也颓废过,但从来没有绝对的放弃过.由于种种客观原因与主观原因导致没有给 ...
- git指令-删除
git指令-删除 添加一个新文件test.txt到Git并且提交: $ git add test.txt $ git commit -m "add test.txt" [maste ...
- Apple App签名机制
概览 数字签名 签名机制与验证过程 操作流程 数字签名 摘要算法 将任意长度文本通过一个算法得到一个固定长度的文本. 源文本不同,计算结果必然不同 无法从结果反推源 例如,MD5和SHA算法 非对称加 ...
- python安装pip (windows64)
1.前提条件是先安装了easy_install(easy_install安装教程http://www.cnblogs.com/IT-Crowd/articles/6528469.html) 2.在ea ...
- 【Amaple教程】6. 路由配置
在 第1节<启动路由> 章节中为了能让单页应用顺利跑起来,我们提前介绍了简单的路由配置方法.我们已了解路由配置的目的是指定不同的url下对应的 模块节点(也叫做模块容器)内应该显示哪个模块 ...
- JS面试准备二
1.常用的字符串方法 1. indexOf:查找字符串某一项的初始位置2. slice:截取字符串(包含起始位置,不包含结束位置) 不会根据参数大小,交换参数位置 如果出现-1按倒数第一个数,如果出现 ...
- ZYNQ入门实例——定时器中断与程序固化
一.前言 APU系统中CPU以串行执行代码的方式完成操作,软件方式很难做到精准计时,因此调用内部定时器硬件完成计时是更好的选择.本文以定时器中断方式控制LED周期性闪烁为例学习私有定时器的使用.同时学 ...
- Layabox 世界坐标和屏幕坐标互转
最近在入坑Layabox,花了几天时间做世界坐标和屏幕坐标的互转,由于Layabox没有现成的代码所以只能自己手动写,大概就是模仿unity里面的ScreenToWorldPoint和WorldToS ...