关于Ubuntu系统中mysql的用户debian-sys-maint
最近在Ubuntu16上安装nodejs,发生错误如下:
mysql_upgrade: Got error: 1045: Access denied for user ‘debian-sys-maint’@’localhost’ (using password: YES) while connecting to the MySQL server
Do you want to continue? [Y/n] y
Setting up mysql-server-5.7 (5.7.21-0ubuntu0.16.04.1) ...
mysql_upgrade: Got error: 1045: Access denied for user 'debian-sys-maint'@'localhost' (using password: YES) while connecting to the MySQL server
Upgrade process encountered error and will not continue.
mysql_upgrade failed with exit status 11
dpkg: error processing package mysql-server-5.7 (--configure):
subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of mysql-server:
mysql-server depends on mysql-server-5.7; however:
Package mysql-server-5.7 is not configured yet.
dpkg: error processing package mysql-server (--configure):
dependency problems - leaving unconfigured
Errors were encountered while processing:
mysql-server-5.7
mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)
这个mysql用户debian-sys-main是个什么鬼?为什么mysql_upgrade会用这个用户连接,而且为什么又会密码不对?
原来,在MySQL服务器的Debian/Ubuntu发行版下,该软件包依赖于一个特殊的mysql用户,用于名为debian-sys-maint的init脚本。用户的密码在/etc/mysql/debian-my.cnf中
root@iZ2ze8wsk0m120dsi7m0juZ:/# cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = aVKpepDSPym1V0nb
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = aVKpepDSPym1V0nb
socket = /var/run/mysqld/mysqld.sock
默认情况,还用于日志的处理。参考/etc/logrotate.d/mysql-server:
root@iZ2ze8wsk0m120dsi7m0juZ:/# cat /etc/logrotate.d/mysql-server
# - I put everything in one block and added sharedscripts, so that mysql gets
# flush-logs'd only once.
# Else the binary logs would automatically increase by n times every day.
# - The error log is obsolete, messages go to syslog now.
/var/log/mysql.log /var/log/mysql/*log {
daily
rotate 7
missingok
create 640 mysql adm
compress
sharedscripts
postrotate
test -x /usr/bin/mysqladmin || exit 0
# If this fails, check debian.conf!
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
# Really no mysqld or rather a missing debian-sys-maint user?
# If this occurs and is not a error please report a bug.
#if ps cax | grep -q mysqld; then
if killall -q -s0 -umysql mysqld; then
exit 1
fi
else
$MYADMIN flush-logs
fi
endscript
}
并且,在/etc/init.d/mysql脚本中也是使用这个作为默认配置文件:
...................
test -x /usr/bin/mysqld_safe || exit 0
. /lib/lsb/init-functions
SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
CONF=/etc/mysql/my.cnf
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
...................
在mysql-server-5.7.README.Debian有下面的描述:
* MYSQL WON'T START OR STOP?:
=============================
You may never ever delete the special mysql user "debian-sys-maint". This
user together with the credentials in /etc/mysql/debian.cnf are used by the
init scripts to stop the server as they would require knowledge of the mysql
root users password else.
So in most of the times you can fix the situation by making sure that the
debian.cnf file contains the right password, e.g. by setting a new one
(remember to do a "flush privileges" then).
当MySQL首次安装在Ubuntu/Debian上时,随机地为debian-sys-maint用户创建了一个密码,在MySQL中创建用户(在初始安装期间根MySQL用户没有密码,因此能够以root身份登录),并在系统上创建/etc/mysql/debian-my.cnf文件。使用Ubuntu/Debian软件包升级MySQL时,将使用debian-sys-maint用户,密码已存储在/etc/mysql/debian-my.cnf中。如果密码不正确,升级将会成功并出现一些警告,但用户不会自动重新创建。
解决方案:
那么解决方案是什么?那么,有办法来处理它。一种是在使用初始化脚本启动MySQL时忽略错误,并使用具有适当权限的用户使用mysqladmin关闭MYSQL。另一种方法是使用/etc/mysql/debian-my.cnf文件中的信息在MySQL中创建必要的debian-sys-maint用户(或更新用户的密码)。
GRANT ALL PRIVILEGES on *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
flush privileges;
root@iZ2ze8wsk0m120dsi7m0juZ:/# apt-get update
.....................
root@iZ2ze8wsk0m120dsi7m0juZ:/# apt-get install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
nodejs is already the newest version (4.2.6~dfsg-1ubuntu4.1).
0 upgraded, 0 newly installed, 0 to remove and 109 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Setting up mysql-server-5.7 (5.7.21-0ubuntu0.16.04.1) ...
Checking if update is needed.
Checking server version.
Running queries to upgrade MySQL server.
Checking system database.
mysql.columns_priv OK
mysql.db OK
mysql.engine_cost OK
mysql.event OK
mysql.func OK
mysql.general_log OK
mysql.gtid_executed OK
mysql.help_category OK
......................................
Upgrade process completed successfully.
Checking if update is needed.
Setting up mysql-server (5.7.21-0ubuntu0.16.04.1) ...
...................