注意: 本文操作环境为win10系统wsl下的Ubuntu18.04,对于原生的Ubuntu18.04同样适用。MySQL默认版本为5.7,其他版本不适用。
安装步骤
1.更新源:
sudo apt update
2.安装mysql:
sudo apt install mysql-server
wsl下使用上述命令安装就直接安装上去了,没有设置密码的地方,这时候无论怎么登陆,都无法登录上去。
1chenyc@DESKTOP-Q5J25HR:~$ mysql 2ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 3chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p 4Enter password: 5ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 6chenyc@DESKTOP-Q5J25HR:~$
设置root用户密码:
1chenyc@DESKTOP-Q5J25HR:~$ sudo mysql_secure_installation 2 3Securing the MySQL server deployment. 4 5Enter password for user root: 6Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
发现无法设置,看报错信息,说的是不能连接到mysqld.sock套接字,猜想是mysql服务没有开启。
开启mysql服务:
1chenyc@DESKTOP-Q5J25HR:~$ sudo service mysql start 2 * Starting MySQL database server mysqld 3No 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即可。
1New password: 2 3Re-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
是否重新载入特权表,最好不动它,选否。
下面是设置密码的全过程。
1chenyc@DESKTOP-Q5J25HR:~$ sudo mysql_secure_installation 2 3Securing the MySQL server deployment. 4 5Connecting to MySQL using a blank password. 6 7VALIDATE PASSWORD PLUGIN can be used to test passwords 8and improve security. It checks the strength of password 9and allows the users to set only those passwords which are 10secure enough. Would you like to setup VALIDATE PASSWORD plugin? 11 12Press y|Y for Yes, any other key for No: y 13 14There are three levels of password validation policy: 15 16LOW Length >= 8 17MEDIUM Length >= 8, numeric, mixed case, and special characters 18STRONG Length >= 8, numeric, mixed case, special characters and dictionary file 19 20Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 21Please set the password for root here. 22 23New password: 24 25Re-enter new password: 26 27Estimated strength of the password: 25 28Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y 29By default, a MySQL installation has an anonymous user, 30allowing anyone to log into MySQL without having to have 31a user account created for them. This is intended only for 32testing, and to make the installation go a bit smoother. 33You should remove them before moving into a production 34environment. 35 36Remove anonymous users? (Press y|Y for Yes, any other key for No) : y 37Success. 38 39 40Normally, root should only be allowed to connect from 41'localhost'. This ensures that someone cannot guess at 42the root password from the network. 43 44Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n 45 46 ... skipping. 47By default, MySQL comes with a database named 'test' that 48anyone can access. This is also intended only for testing, 49and should be removed before moving into a production 50environment. 51 52 53Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n 54 55 ... skipping. 56Reloading the privilege tables will ensure that all changes 57made so far will take effect immediately. 58 59Reload privilege tables now? (Press y|Y for Yes, any other key for No) : n 60 61 ... skipping. 62All done!
以上设置完成后,使用刚刚设置的root用户密码,再次连接数据库,成功连接上。
1chenyc@DESKTOP-Q5J25HR:~$ sudo mysql -u root -p 2Enter password: 3Welcome to the MySQL monitor. Commands end with ; or \g. 4Your MySQL connection id is 6 5Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu) 6 7Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 8 9Oracle is a registered trademark of Oracle Corporation and/or its 10affiliates. Other names may be trademarks of their respective 11owners. 12 13Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 14 15mysql>
编码设置
查看编码:
1mysql> show variables like 'character%'; 2+--------------------------+----------------------------+ 3| Variable_name | Value | 4+--------------------------+----------------------------+ 5| character_set_client | utf8 | 6| character_set_connection | utf8 | 7| character_set_database | latin1 | 8| character_set_filesystem | binary | 9| character_set_results | utf8 | 10| character_set_server | latin1 | 11| character_set_system | utf8 | 12| character_sets_dir | /usr/share/mysql/charsets/ | 13+--------------------------+----------------------------+ 148 rows in set (0.00 sec)
可以看到,刚刚安装好的mysql默认有很多是latin1的编码格式,对汉字的支持不好,因此需要改成utf8编码格式。
修改mysql数据库编码的步骤:
1.停止mysql服务:
1chenyc@DESKTOP-Q5J25HR:~$ /etc/init.d/mysql stop 2 * Stopping MySQL database server mysqld 3cat: /var/run/mysqld/mysqld.pid: Permission denied 4 [fail]
发现没有权限,加上sudo再去执行:
1chenyc@DESKTOP-Q5J25HR:~$ sudo /etc/init.d/mysql stop 2 * 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# 修改处1:添加以下2行 2[client] 3default-character-set=utf8 4 5 6[mysqld] 7# 修改处2:添加以下3行 8default-storage-engine=INNODB 9character-set-server=utf8 10collation-server=utf8_general_ci
修改完成后,重启数据库:
1chenyc@DESKTOP-Q5J25HR:~$ sudo service mysql start 2 * Starting MySQL database server mysqld [ OK ]
再次登录,发现登不上了:
1chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p 2Enter password: 3ERROR 1698 (28000): Access denied for user 'root'@'localhost'
使用sudo cat /etc/mysql/debian.cnf查看debian-sys-maint密码,发现密码是下面这个玩意:
1chenyc@DESKTOP-Q5J25HR:~$ sudo cat /etc/mysql/debian.cnf 2# Automatically generated for Debian scripts. DO NOT TOUCH! 3[client] 4host = localhost 5user = debian-sys-maint 6password = LMCuPijw9kX5cRsS 7socket = /var/run/mysqld/mysqld.sock 8[mysql_upgrade] 9host = localhost 10user = debian-sys-maint 11password = LMCuPijw9kX5cRsS 12socket = /var/run/mysqld/mysqld.sock
因此,可以使用debian-sys-maint用户先登录上去,修改密码:
1chenyc@DESKTOP-Q5J25HR:~$ mysql -udebian-sys-maint -p 2Enter password: 3Welcome to the MySQL monitor. Commands end with ; or \g. 4Your MySQL connection id is 18 5Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu) 6 7Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 8 9Oracle is a registered trademark of Oracle Corporation and/or its 10affiliates. Other names may be trademarks of their respective 11owners. 12 13Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 14 15mysql>
使用上面的password,成功登录。
修改密码,执行如下语句:
1UPDATE mysql.user SET authentication_string=PASSWORD('你的密码'), PLUGIN='mysql_native_password' 2 WHERE USER='root';
执行时发现报错:
1mysql> UPDATE mysql.user SET authentication_string=PASSWORD('cyc2010'), PLUGIN='mysql_native_password' WHERE USER='root'; 2ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
说不符合安全规范,估计是之前设置安全级别的那地方的问题,需要重新设置一下:
1mysql> set global validate_password_policy=0; 2Query OK, 0 rows affected (0.00 sec)
validate_password_length(密码长度)参数默认为8,修改为1:
1mysql> set global validate_password_length=1; 2Query OK, 0 rows affected (0.00 sec)
再次修改密码成功。
1mysql> UPDATE mysql.user SET authentication_string=PASSWORD('cyc2010'), PLUGIN='mysql_native_password' WHERE USER='root'; 2Query OK, 1 row affected, 1 warning (0.00 sec) 3Rows matched: 1 Changed: 1 Warnings: 1
授权远程登录:
1mysql> grant all privileges on *.* to 'root'@'%' identified by 'cyc2010' with grant option; 2Query OK, 0 rows affected, 1 warning (0.01 sec)
设置完成后,再次重启服务:
1chenyc@DESKTOP-Q5J25HR:~$ sudo /etc/init.d/mysql restart 2 * Stopping MySQL database server mysqld [ OK ] * Starting MySQL database server mysqld No directory, logging in with HOME=/ [ OK ]
使用root用户登录:
1chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p 2Enter password: 3Welcome to the MySQL monitor. Commands end with ; or \g. 4Your MySQL connection id is 4 5Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu) 6 7Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 8 9Oracle is a registered trademark of Oracle Corporation and/or its 10affiliates. Other names may be trademarks of their respective 11owners. 12 13Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 14 15mysql>
成功登录,查看编码格式:
1mysql> show variables like 'character%'; 2+--------------------------+----------------------------+ 3| Variable_name | Value | 4+--------------------------+----------------------------+ 5| character_set_client | utf8 | 6| character_set_connection | utf8 | 7| character_set_database | utf8 | 8| character_set_filesystem | binary | 9| character_set_results | utf8 | 10| character_set_server | utf8 | 11| character_set_system | utf8 | 12| character_sets_dir | /usr/share/mysql/charsets/ | 13+--------------------------+----------------------------+ 148 rows in set (0.00 sec)
发现都变成了utf8,说明设置成功了。
系统重启后失效问题
重启wsl-Linux子系统:
1//WSL-Ubuntu18.04 LTS 重启方法 2//以管理员权限运行cmd 3>net stop LxssManager //停止 4>net start LxssManager //启动
重启系统后,再次登录,发现又登不上了:
1chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ mysql -u root -p 2Enter password: 3ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
看报错信息,应该是mysql服务没有启动,启动服务:
1chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ sudo service mysql start 2[sudo] password for chenyc: 3 * Starting MySQL database server mysqld No directory, logging in with HOME=/ 4 [ OK ]
再次登录,发现登陆成功。
1chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ mysql -u root -p Enter password: 2Welcome to the MySQL monitor. Commands end with ; or \g. 3Your MySQL connection id is 4 4Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu) 5 6Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 7 8Oracle is a registered trademark of Oracle Corporation and/or its 9affiliates. Other names may be trademarks of their respective 10owners. 11 12Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14mysql>