MySQL破解root账户密码,及多实例自动部署

破解Mysql管理员账户密码

1.进入配置文件/etc/my.conf,添加skip-grant-table:

[root@server ~]# vim /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

skip-grant-tables

2.重启服务

[root@server ~]# service mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 

3.更新mysql.user表,设置user=root,并且host=localhost的记录中authentication_string的密码

mysql> update mysql.user set authentication_string = password('tjp123') where User = 'root' and Host = 'localhost';
Query OK, 1 row affected, 1 warning (0.36 sec)
Rows matched: 1  Changed: 1  Warnings: 1


4.删除配置文件中的skip-grant-tables重启服务
5.重启服务

[root@server ~]# service mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 

验证

[root@server ~]# mysql -uroot -ptjp123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)

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> 

MySQL多实例部署

手动版

创建各实例数据存放的目录

[root@server data]# mkdir -p /opt/data/{3306,3307,3308}
[root@server data]# ls
3306            ib_logfile1       mysql_bin.000016    server.err
3307            ibtmp1            mysql_bin.000017    sys
3308            mysql             mysql_bin.000018    tjp

初始化各实例

[root@server ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3306
[root@server ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3307
[root@server ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3308
截取密码
_KhT(H5rR;Cm
kqzAwRO<=0qf
R%>Uehoh=80(

安装perl

[root@server ~]# yum -y install perl

配置配置文件/etc/my.cnf

[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin

[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log

[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log-error=/var/log/3307.log

[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/mysql3308.sock
pid-file = /opt/data/3308/mysql_3308.pid
log-error=/var/log/3308.log

启动各实例

[root@server ~]# mysqld_multi start 3306
Wide character in print at /usr/local/mysql/bin/mysqld_multi line 672.
[root@server ~]# mysqld_multi start 3307
[root@server ~]# mysqld_multi start 3308

[root@server data]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port       Peer Address:Port   
LISTEN   0        80                     *:3306                  *:*      
LISTEN   0        80                     *:3307                  *:*      
LISTEN   0        50                  [::]:139                [::]:*      
LISTEN   0        80                     *:3308                  *:*    

自动版

[root@localhost mysql]# tree
.
├── soft
│   └── mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
└── tang.sh

[root@localhost mysql]# cat tang.sh 
#!/bin/bash 
# auto install mysql
datadir=/data
basedir=/usr/local
pkg_name=mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
unpack_dir=$basedir/$(echo $pkg_name | awk -F'.tar' '{print $1}')
port=3306 
PATH=$basedir/mysql/bin:$PATH

yum -y -q install tar  perl ncurses-compat-libs &>/dev/null

read -p "输入要创建的实例个数:" count
read -s -p "输入要为数据库设置的密码" passwd


#判断有没有mysql用户.扔掉输出;没有就创建用户,
id mysql &> /dev/null || /usr/sbin/useradd -r -M -s /sbin/nolgin mysql 
#判断这个路径是否不存在,不存在就创建
[ ! -d $basedir ] && mkdir -p $basedir 
#判断没有一个mysql安装包的目录
if [ ! -d $unpack_dir ];then
#解压安装包到指定目录
   echo "正在解压$pkg_name 至 $basedir 目录...."
tar xf soft/$pkg_name -C $basedir
fi 
#软链接取出解压后的文件名
ln -s $unpack_dir $basedir/mysql &>/dev/null
chown -R mysql.mysql $basedir/mysql
echo "export PATH=$basedir/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh

if [ $count -gt 1 ];then
cat > /etc/my.cnf <<EOF
[mysqld_multi]
mysqld = $basedir/mysql/bin/mysqld_safe
mysqladmin = $basedir/mysql/bin/mysqladmin

EOF
fi


for i in $(seq $count);do
    if [ $i -ne 1 ];then
	    let port++
    fi
    mkdir -p $datadir/$port &>/dev/null
    chown -R mysql.mysql $datadir

    myinit=$(ls $datadir/$port|wc -l)
    if [ $myinit -eq 0 ];then
	    echo " 正在初始化$port "
    $basedir/mysql/bin/mysqld --initialize-insecure --datadir=/data/$port --user=mysql --explicit_defaults_for_timestamp &> /dev/null
    fi
#单实例
if [ $count -eq 1 ];then
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $basedir/mysql
datadir = $datadir/$port
socket = /tmp/mysql.sock
port = 3306
pid-file = $datadir/$port/mysql.pid
user = mysql
skip-name-resolve
EOF
#脚本
   \cp -a $basedir/mysql/support-files/mysql.server /etc/init.d/mysqld
   sed -ri "s#^(basedir=).*#\1$basedir/mysql#g" /etc/init.d/mysqld
   sed -ri "s#^(datadir=).*#\1$basedir/$port#g" /etc/init.d/mysqld
#启动服务 
service mysqld start 
#设置密码(套接字)
$basedir/mysql/bin/mysql -uroot -e " set password = password('$passwd');"
else    
#多实例追加
cat >> /etc/my.cnf <<EOF

[mysqld$port]
datadir = $datadir/$port
port = $port
socket = /tmp/mysql$port.sock
pid-file = $datadir/$port/mysql_$port.pid
log-error = /var/log/$port.log
EOF
      $basedir/mysql/bin/mysqld_multi start $port
  sleep 3
      $basedir/mysql/bin/mysql -uroot -S /tmp/mysql$port.sock -e "set password = password('$passwd');"
     fi
done

运行结果

[root@localhost mysql]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port       Peer Address:Port   
LISTEN   0        128              0.0.0.0:22              0.0.0.0:*      
LISTEN   0        128                 [::]:22                 [::]:*      
[root@localhost mysql]# bash tang.sh 
输入要创建的实例个数:3
输入要为数据库设置的密码 正在初始化3306 
 正在初始化3307 
 正在初始化3308 
[root@localhost mysql]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port       Peer Address:Port   
LISTEN   0        128              0.0.0.0:22              0.0.0.0:*      
LISTEN   0        80                     *:3306                  *:*      
LISTEN   0        80                     *:3307                  *:*      
LISTEN   0        80                     *:3308                  *:*      
LISTEN   0        128                 [::]:22                 [::]:* 
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页