#!/bin/bash # 使用方法: ./mysql.sh "主机名/IP" "端口" "用户名" "密码" > "输出文件" if [ $# == 4 ] then MYSQL_HOST=$1 MYSQL_PORT=$2 MYSQL_USER=$3 MYSQL_PWD=$4 elif [ $# == 3 ] then MYSQL_HOST=$1 MYSQL_PORT=3306 MYSQL_USER=$2 MYSQL_PWD=$3 else echo "Usage:" echo " $0 hostname port username password > output_file"; echo " $0 hostname username password > output_file"; exit; fi #检查进程权限 echo "[>]Running User:" ps -aux|grep mysql MYSQL_DATA=`echo "select VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME='DATADIR';"|mysql --disable-pager -N -h $MYSQL_HOST -u $MYSQL_USER --password=$MYSQL_PWD` echo "[>]DataPath: $MYSQL_DATA" ls -al $MYSQL_DATA MYSQL_BASE=`echo "select VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME='BASEDIR';"|mysql --disable-pager -N -h $MYSQL_HOST -u $MYSQL_USER --password=$MYSQL_PWD` echo ">>BasePath: $MYSQL_BASE" if [ $MYSQL_BASE = '/usr/' ] then rpm -qa|grep mysql|grep server|xargs rpm -ql|xargs ls -al else ls -al $MYSQL_BASE fi mysql -t -h $MYSQL_HOST -u $MYSQL_USER --password=$MYSQL_PWD <<EOF -- 获取MYSQL版本信息 select @@version; -- 获取当前的用户 select user(); -- 获取日志设置 show variables like 'log%'; -- 获取SSL设置 show variables like '%ssl%'; -- 获取用户列表,ssl设置和连接的限制设置 select Host,User,password,ssl_type,ssl_cipher,x509_issuer,x509_subject,max_questions,max_updates,max_connections,max_user_connections from mysql.user; -- 获取相同用户和相同密码的数量 select 'common',count(distinct concat_ws('@',user,host)) as user,count(distinct password) as pwd from mysql.user; -- 获取密码策略 show variables like 'validate_password%'; -- 超时查询 show global variables like '%timeout'; -- 获取文件权限开启状态 select VARIABLE_NAME,VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME='LOCAL_INFILE'; -- show_compatibility_56打开 --set global show_compatibility_56=on; -- 确保只有管理员账号有所有数据库的访问权限 SELECT user, host FROM mysql.user WHERE (Select_priv = 'Y') OR (Insert_priv = 'Y') OR (Update_priv = 'Y') OR (Delete_priv = 'Y') OR (Create_priv = 'Y') OR (Drop_priv = 'Y'); -- 口令复杂度 show variables like '%password%'; -- 空口令用户 SELECT User,host FROM mysql.user WHERE authentication_string=''; -- 匿名账户 SELECT user,host FROM mysql.user WHERE user = ''; 确保用户不允许所有ip访问 SELECT user, host FROM mysql.user WHERE host = '%'; -- 获取初始化连接执行语句 select VARIABLE_NAME,VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME='INIT_CONNECT'; EOF echo "select concat('show grants for \`',User,'\`@\`',Host,'\`;') from mysql.user;"|mysql --disable-pager -N -h $MYSQL_HOST -u $MYSQL_USER --password=$MYSQL_PWD|mysql -t -h $MYSQL_HOST -u $MYSQL_USER --password=$MYSQL_PWD