Archive for 十二月 2010

mysql默认登录账户

前几天,一个测试的哥们来找我,问我mysql登录,如果不输入用户名,那么默认使用的那个账户登录,我一想,不就是当前你登录的账户吗?用whoami就可以得到。
但是,我错了!
我们测试了这样一个场景
场景1:先登录root账户,然后su – mysql,此时,mysql登录进去以后一看,还是以root账户登录mysql的
场景2:如果我们直接以mysql登录这台主机,输入mysql登录,提示是默认用mysql账户登录MySQL数据库的。
这样的话,我们怀疑su – mysql 把一些环境变量带到mysql账户中。但是检查了env和set输出的内容,却没有发现异常。
没辙了,请教传奇人物我们的系统管理员:库哥。检查来检查去,最后库哥通过strace为我们释疑了。
(可能我自己好好看看MySQL的源代码也可以看到原因,但是偷懒了,值得批评!)
原因如下:mysql获得默认账户是这样获得的,它首先readlink /proc/self/fd/0获得登录账户的pts,
类似于
[mysql@PLATQA153004 ~]$ readlink /proc/self/fd/0
/dev/pts/5
然后通过/var/run/utmp来获得对应的用户,/var/run/utmp不是纯文本文件,但是你用root登录主机和mysql登录主机可以明显看到对应的用户名是不一样的。
那么也就是说,mysql是以你登录主机的pts账户作为默认账户的,su 等操作对它都没有影响。
摘录几个man介绍如下:
/proc/self
This  directory  refers to the process accessing the /proc filesystem, and is identical to the /proc directory named by the process ID of the
same process.
/proc/[number]/fd
This  is  a subdirectory containing one entry for each file which the process has open, named by its file descriptor, and which is a symbolic
link to the actual file.  Thus, 0 is standard input, 1 standard output, 2 standard error, etc.
我们在linux上面经常用的0,1,2就在这里了,哈哈
In a multithreaded process, the contents of this directory are not available if the main thread has already terminated (typically by  calling
pthread_exit(3)).
Programs  that will take a filename, but will not take the standard input, and which write to a file, but will not send their output to stan-
dard output, can be effectively foiled this way, assuming that -i is the flag designating an input file and -o is  the  flag  designating  an
output file:
foobar -i /proc/self/fd/0 -o /proc/self/fd/1 …
and you have a working filter.
/proc/self/fd/N  is  approximately  the  same  as /dev/fd/N in some UNIX and UNIX-like systems.  Most Linux MAKEDEV scripts symbolically link
/dev/fd to /proc/self/fd, in fact.
The  utmp  file  allows  one  to  discover information about who is currently using the system.  There may be more users currently using the system,
because not all programs use utmp logging.