mysql 初始化脚本

 

脚本须知:

  1. 确认mysql的数据目录,二进制日志目录,中继日志的目录,安装目录的位置

       2. 初始化会对前三个目录执行清空操作,不过清空前该脚本对其进行了压缩打包统一存放在/tmp目录下

       3. 脚本运行完应该查看服务是否启动正常,如果不正常请检查配置文件/etc/my.cnf和相关目录的权限属主问题

       4. 此脚本对需要快速恢复mysql到初始状态的情况比较适宜 比如(主从复制出现问题,已经不能简单的靠2进制日志解决,此时可以用该脚本初始化mysql salve

           服务器 然后用完全备份加2进制重新与主mysql完成同步)

 1 #!/bin/bash
 2 #
 3 
 4 # function
 5 clean_dir(){
 6 cd $1
 7 name=`echo $1 | sed 's@/@@' | sed 's@/@_@g'`
 8 tar -jcf $name.tar.bz2 * &> /dev/null
 9 \mv -f $name.tar.bz2 /tmp/
10 rm -rf *
11 }
12 
13 mysql_init(){
14 $basedir/scripts/mysql_install_db --defaults-file=/etc/my.cnf \
15 --basedir=$basedir \
16 --datadir=$datadir \
17 --user=mysql 
18 if [ $? -eq 0 ];then
19     echo "----------->initiate is finished"
20 else
21     echo "------------>initiate is failed"
22     return 7
23 fi
24 }
25 
26 info_check(){
27     read -p "Please give the MySQL data directory: " datadir
28     if [ ! -d $datadir ];then
29         echo 'directory does not exist !'
30         exit 1
31     fi
32     read -p "Please give a binary file directory: " bindir
33     if [ ! -d $bindir ];then
34         echo 'directory does not exist !'
35         exit 2
36     fi
37     read -p "Please give the relay log directory: " relaydir
38     if [ ! -d $relaydir ];then
39         echo 'directory does not exist !'
40         exit 3
41     fi
42     read -p "Please give the Mysql Install directory: " basedir
43     if [ ! -d $basedir ];then
44         echo 'directory does not exist !'
45         exit 4
46     fi
47     echo -e "\033[31mPlease verify your give Mysql information:\n\
48      MySQL data directory: $datadir\n\
49      MySQL Install directory: $basedir\n\
50      Binary file directory: $bindir\n\
51      Relay log directory: $relaydir\n\033[0m"
52 }
53 
54 # main
55 info_check
56 sleep 1
57 while true;do
58     read -p "Confirm please enter yes, change directory information please input C: " ans
59     if [ $ans == "yes" ];then
60         break
61     elif [ $ans == "c" -o $ans == "C" ];then
62         info_check
63         sleep 1
64     else
65         echo "Option only supports: yes | c"
66         continue
67     fi
68 done
69 echo -e "\033[31mMySQL database initialization......\033[0m"
70 service mysqld stop &> /dev/null || kill 9 `cat /var/run/mysqld/mysqld.pid`
71 dir=`echo  $datadir $bindir $relaydir | xargs -n 1 echo | sort -u`
72 for i in $dir ;do
73     clean_dir $i
74 done
75 # If the MySQL service is started
76 # ps -ef | grep -E "mysqld[[:space:]]+" | awk -F ' ' '{ print $8 }' | xargs dirname | sed -r 's@[^/]+/?$@@'
77 mysql_init $datadir $basedir
78 if [ $? -eq 0 ];then
79     chown -R mysql.mysql {$datadir,$bindir,$relaydir}
80     service mysqld start
81 fi

 

posted @ 2016-07-14 13:50  sed_'s/M18/黑匣子/'  阅读(1146)  评论(0编辑  收藏  举报