forked from lxcenter/kloxo
/
reset-mysql-root-password.php
56 lines (40 loc) · 1.15 KB
/
reset-mysql-root-password.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
include_once "lib/html/include.php";
$tpath = "/usr/local/lxlabs/kloxo/serverfile";
if (isset($argv[1])) {
$pass = $argv[1];
} else {
$pass = "";
}
$text = <<<EOF
UPDATE mysql.user SET Password=PASSWORD('PASSWORD') WHERE User='USER';
FLUSH PRIVILEGES;
EOF;
$text = str_replace("'USER'", "'root'", $text);
$text = str_replace("'PASSWORD'", "'{$pass}'", $text);
file_put_contents("{$tpath}/reset-mysql-password.sql", $text);
print("Stop MySQL service...\n");
if (isServiceExists('mysqld')) {
exec("service mysqld stop");
} else {
exec("service mysql stop");
}
print("MySQL ROOT password reset...\n");
sleep(10);
system("mysqld_safe --skip-grant-tables --init-file={$tpath}/reset-mysql-password.sql >/dev/null 2>&1 &");
sleep(15);
print("Start MySQL service...\n");
if (isServiceExists('mysqld')) {
exec("service mysqld start");
} else {
exec("service mysql start");
}
exec("'rm' -f {$tpath}/reset-mysql-password.sql");
$conn = new mysqli('localhost', 'root', $pass, 'mysql');
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$conn->close();
$a['mysql']['dbpassword'] = $pass;
slave_save_db("dbadmin", $a);