mysql

</div>Ubuntu也是坑,总是变来变去。原来从16.04版升级到17.10版时,域名解析服务换了,直接升级域名解析用不了???,由原来的resolvconf服务变成了systemd-resolved服务。

现在新版的18.04安装mysql也变了,安装过程没有要求设置root密码了,直接静默安装了,安装好后直接可以匿名登录?。直接按常规的改密码的方法改完了还是有个匿名登录,简直了。

所以我之前一个文章《怎么样用wordpress搭建一个站点之四:安装wordpress》里面关于mysql安装的部分需要修改修改了?

上google搜了搜,看到了这篇文章《How to reset root MySQL password on Ubuntu 18.04 Bionic Beaver Linux》,尝试里面的方法,终于解决了。

这篇文章讲到了两种方法,第一种放我试过没效果,第二种方法才搞定。我记录下来,以便以后能够用到:

方法一:通过mysql_secure_installation重设密码

很简单只需执行“mysql_secure_installation”命令安装它的提示往下走就能完成重设密码(虽然我试过没用?)

$ sudo mysql_secure_installation
....
Please set the password for root here.

New password: 
Re-enter new password:

方法二:使用–skip-grant-tables参数重设密码

稍微麻烦一点,如果用方法一失败了可以试试这个方法。

首先我们要停止mysql服务:

$ sudo service mysql stop

接下来我们创建一个路径 /var/run/mysqld 用来存放mysql进程的socket文件:

$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld

然后我们可以带上–skip-grant-tables手动启动mysql进程:

$ sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
[1] 2708

现在检查一个mysql是不是正常运行:

$ jobs
[1]+  Running     sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &

如果之前的步骤没问题,我们现在就可以用mysql命令不用用户名密码直接登录到数据库:

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20-1ubuntu1 (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>

现在mysql客户端的会话中,首先我们先flush privileges:

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

接下来,我们开始重设密码。下面的命令会把密码重设为“www.azio.me”:

mysql> USE mysql; 
Database changed
mysql> UPDATE user SET authentication_string=PASSWORD("www.azio.me") WHERE User='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql> UPDATE user SET plugin="mysql_native_password" WHERE User='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

接着退出mysql会话:

mysql> quit                                                                                                                                                                                    
Bye

优雅地(原文:Gracefully?)终止当前mysql服务进程:

$ sudo pkill mysqld                                                                                                                                                        
$ jobs                                                                                                                                                                     
[1]+  Done       sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking

最后启动mysql数据库服务器就行啦:

$ sudo service mysql start

如果配置没问题的话,你现在就可以使用用户名密码登录mysql数据库啦:

$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 107
Server version: 5.7.22-0ubuntu18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, 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>

最后祝你成功(手动滑稽