问题:
使用apt-get来安装mysql,安装好之后发现安装的是 MariaDB,如下,无需密码既可以登录了。即使使用mysqladmin设置好密码,用密码登录可以,不用密码登录也可以
root@ubuntu:/etc/mysql# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 35 Server version: 10.0.31-MariaDB-0ubuntu0.16.04.2 Ubuntu 16.04 Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
排查思路:
第一看看my.conf有没有skip-grant-tables,跳过密码验证
过滤了下没有
看看my.cnf里面是不是把密码写进去了,查找了相关.cnf文件去看了看也没有
root@ubuntu:~# find / -name "*.cnf" /usr/share/ssl-cert/ssleay.cnf /usr/share/dovecot/dovecot-openssl.cnf /usr/lib/ssl/openssl.cnf /etc/ssl/openssl.cnf /etc/alternatives/my.cnf /etc/mysql/my.cnf /etc/mysql/mariadb.cnf /etc/mysql/conf.d/mysqldump.cnf /etc/mysql/conf.d/mysql.cnf /etc/mysql/mariadb.conf.d/50-mysqld_safe.cnf /etc/mysql/mariadb.conf.d/50-mysql-clients.cnf /etc/mysql/mariadb.conf.d/50-client.cnf /etc/mysql/mariadb.conf.d/50-server.cnf /etc/mysql/debian.cnf /var/lib/dpkg/alternatives/my.cnf root@ubuntu:~#
不过有个小发现:
vim /etc/mysql/debian.cnf # Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = root password = socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = root password = socket = /var/run/mysqld/mysqld.sock basedir = /usr
看了说明是以上由脚本生成,不要改动,虽然这样写,我也去改了下,加上密码,重启还是不行
最后的最后,去搜索了很久,终于有发现了,是用户插件问题。
第一我去跟安装正常的mysql来比较下,如下
看到这里应该发现问题了,按照正常的修改就行了
sudo service mysql stop sudo mysqld_safe --skip-grant-tables
进去mysql执行如下命令:
MariaDB [(none)]> UPDATE mysql.user SET authentication_string = PASSWORD('mypassword'), plugin = 'mysql_native_password' WHERE User = 'root' AND Host = 'localhost'; MariaDB [(none)]> FLUSH PRIVILEGES;
验证:
先kill掉mysql kill -9 pid
启动:
sudo service mysql start
最后验证下:需要密码了
root@ubuntu:~# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) root@ubuntu:~#