#/bin/bash

PWD=`pwd` >>mysql_install.log

#A.建立mysql用户、用户组、home目录

groupadd mysql >>mysql_install.log


useradd -g mysql -d /home/mysql -s /bin/bash mysql -m >>mysql_install.log

#b.安装mysql

mkdir -p /usr/local/mysql >>mysql_install.log

tar -zxvf ./mysql-5.5.24-linux2.6-x86_64.tar.gz >>mysql_install.log

mv ./mysql-5.5.24-linux2.6-x86_64/* /usr/local/mysql/ >>mysql_install.log

chown mysql:mysql /usr/local/mysql >>mysql_install.log

#C.建立data/log相关目录

mkdir -p /data1/mysql/3306/data >>mysql_install.log  

mkdir -p /data1/mysql/3306/data >>mysql_install.log

mkdir -p /data1/mysql/3306/logs >>mysql_install.log

mkdir -p /data1/mysql/3306/logs/binlogs >>mysql_install.log

#D.更改data/log目录所有者

chown -R mysql:mysql /data1/mysql* >>mysql_install.log


#4.初始化默认数据库

#A.将/usr/local/mysql/scripts/mysql_install_db移到/usr/local/mysql/bin/mysql_install_db

mv /usr/local/mysql/scripts/mysql_install_db /usr/local/mysql/bin/mysql_install_db >>mysql_install.log

#根据应用规模选择配置文件

cp -a $PWD/my.cnf /etc/my.cnf >>mysql_install.log

/usr/local/mysql/bin/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data1/mysql/3306/data --user=mysql >>mysql_install.log

#查看/data1/mysql/3306/data下会有mysql和test两个数据库。


#5.启动mysql

su - mysql >>mysql_install.log

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql & >>mysql_install.log

exit

#6.修改密码7zklg@86zKJ


#/usr/local/mysql/bin/mysqladmin -u root password '7zklg@86zKJ'

#/usr/local/mysql/bin/mysqladmin -u root -h 127.0.0.1

#127.0.0.1 password 'new-password

#如果已设置过密码,使用

#mysqladmin -u root password oldpass "newpass"


#8.mysql停止

# mysqladmin -u root shutdown

#其中用户是root,密码是123456



脚本2:通过源码编译

#!/bin/bash


# user define

_mysql_path=/usr/local/mysql

_mysql_data_path=/data/mysql

_mysql_config=/etc/my.cnf

_user=mysql

_mysql_passwd=57DwNRI3pirdSAP

#### end ######


_work_path=$(pwd)

_cmake_soft="cmake-2.8.10.2"

_bison_soft="bison-2.7"

_mysql_soft="mysql-5.5.31"

#_mysql_soft="mysql-5.6.11"


_CHECK_STATS() {

       if [ $? == 0 ];then

               echo "*************************************" >>$_work_path/install.log

               echo "$1 : at $(date +'%F %T') install OK "  >>$_work_path/install.log

       else

echo "install mysql error. please cat $_work_path/install.log "

               echo "*************************************" >>$_work_path/install.log

               echo "$1 : at $(date +'%F %T') install               <<FAIL>> "  >>$_work_path/install.log

exit 1

       fi

}



CHECK_FILE() {


( yum grouplist|grep -B 100 "Available Groups:"|grep "Development Libraries" ) || yum groupinstall "Development Libraries" -y

( yum grouplist|grep -B 100 "Available Groups:"|grep "Development Tools" ) || yum groupinstall "Development Tools" -y


yum install gcc gcc-c++ ncurses-devel make wget -y

[ -s $_cmake_soft.tar.gz ] || wget  http://www.cmake.org/files/v2.8/$_cmake_soft.tar.gz

[ -s $_bison_soft.tar.gz ] || wget  http://mirror.bjtu.edu.cn/gnu/bison/$_bison_soft.tar.gz

[ -s $_mysql_soft.tar.gz ] || wget  http://cdn.mysql.com/Downloads/MySQL-5.5/$_mysql_soft.tar.gz

}


INSTALL_DEPTH() {

cd $_work_path

tar -zxf $_cmake_soft.tar.gz

cd ${_cmake_soft}

./bootstrap

make && make install

_CHECK_STATS $_cmake_soft


cd $_work_path

tar -zxf $_bison_soft.tar.gz

cd ${_bison_soft}

./configure

make && make install

_CHECK_STATS $_bison_soft

}



INSTALL_MYSQL() {


cd $_work_path

tar -zxf $_mysql_soft.tar.gz

$(id mysql > /dev/null 2>&1) || useradd -M -r -s /sbin/nologin mysql

cd ${_mysql_soft}

cmake -DCMAKE_INSTALL_PREFIX=$_mysql_path  \

-DMYSQL_DATADIR=$_mysql_data_path \

-DSYSCONFDIR=$(dirname $_mysql_config) \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DMYSQL_TCP_PORT=3306 \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_EXTRA_CHARSETS=all \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DWITH_READLINE=1 \

-DWITH_ZLIB=system \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_DEBUG=0


_CHECK_STATS mysql_config

make  && make install

_CHECK_STATS mysql_make


}


CONFIG_MYSQL() {

cd ${_mysql_soft}

[ ! -d  $_mysql_data_path ] && mkdir -p $_mysql_data_path


( id $_user >/dev/null 2>&1 ) || useradd -M -r -s /sbin/nologin $_user

chown -R $_user:$_user $_mysql_data_path

chown -R $_user:$_user $_mysql_path

echo  "$_mysql_path/lib"  >>/etc/ld.so.conf.d/mysql.conf && ldconfig

ln -s $_mysql_path/bin/* /usr/local/bin/


cp -a support-files/mysql.server /etc/init.d/mysqld && chmod a+x /etc/init.d/mysqld

sed -i "/^basedir=/s#\(^basedir=\).*#\1$_mysql_path#g" /etc/init.d/mysqld

sed -i "/^datadir=/s#\(^datadir=\).*#\1$_mysql_data_path#g" /etc/init.d/mysqld


chkconfig --add mysqld

chkconfig  mysqld on


config=1

if [ $config -eq 1 ] ;then

[ -f $_mysql_config] && mv $_mysql_config{,.bak$(date +%F)}


cat > $_mysql_config <<END_OF

[client]

port                    = 3306

socket                  = /tmp/mysql.sock

default-character-set = utf8


[mysqld]

port                    = 3306

socket                  = /tmp/mysql.sock

character-set-server    = utf8

basedir                 = ${_mysql_path}

datadir                 = ${_mysql_data_path}

general_log_file        = ${_mysql_data_path}/mysql.log

pid-file                = ${_mysql_data_path}/mysqld.pid

log-error               = ${_mysql_data_path}/mysql.err

user                    = mysql

default-storage-engine= MyISAM


#FEDERATED


key_buffer_size         = 256M

max_allowed_packet      = 16M

sort_buffer_size        = 1M

read_buffer_size        = 1M

join-buffer-size= 1M

read_rnd_buffer_size    = 4M

thread-cache-size       = 300


# show status like '%Qcache_hits%';

query-cache-size        = 512M

query-cache-limit = 2M

query-cache-min-res-unit = 2k

transaction-isolation= REPEATABLE-READ

thread-stack= 192K

tmp-table-size= 256M

max-heap-table-size= 256M

long-query-time= 3


#

skip-name-resolve

skip-symbolic-links

open-files-limit        = 65535



# show variables like  '%table%cache%';

table-cache= 614

# show global status like '%open%tables%';

# Open_tables / Opened_tables >= 0.85

# Open_tables / table_cache <= 0.95

table-open-cache= 600

back-log                = 600


# connection

max-connections         = 6000

max-connect-errors      = 5000

max-user-connections    = 5000


skip-external-locking


# time

connect-timeout= 10

interactive-timeout= 120

wait-timeout= 120


# myisam engine

myisam_sort_buffer_size = 64M

myisam-sort-buffer-size = 128M

myisam-max-sort-file-size = 10G

myisam-repair-threads= 2

myisam-recover-options


# Try number of CPU's*2 for thread_concurrency

thread_concurrency = 4


server-id               = 1

log-bin                 = mysql-bin

expire-logs-days = 30

binlog-cache-size= 4M

binlog-format= MIXED

max-binlog-cache-size= 8M

max-binlog-size= 1G


# Uncomment the following if you are using InnoDB tables

#innodb_data_home_dir            = ${_mysql_data_path}

#innodb_data_file_path           = ibdata1:10M:autoextend

#innodb_log_group_home_dir       = ${_mysql_data_path}

# You can set .._buffer_pool_size up to 50 - 80 %

# of RAM but beware of setting memory usage too high

#innodb_buffer_pool_size = 256M

#innodb_additional_mem_pool_size = 20M

# Set .._log_file_size to 25 % of buffer pool size

#innodb_log_file_size            = 64M

#innodb_log_buffer_size          = 8M

#innodb-log-files-in-group       = 3

#innodb_flush_log_at_trx_commit  = 2

#innodb_lock_wait_timeout        = 50

#innodb-file-io-threads          = 4

#innodb-additional-mem-pool-size = 16M

#innodb-max-dirty-pages-pct      = 90

#innodb-lock-wait-timeout        = 120

#innodb-file-per-table           = FALSE


[mysqldump]

quick

max_allowed_packet      = 16M


[mysql]

no-auto-rehash


[myisamchk]

key_buffer_size         = 128M

sort_buffer_size        = 128M

read_buffer             = 2M

write_buffer            = 2M


[mysqlhotcopy]

interactive-timeout


[safe_mysqld]

err-log= ${_mysql_data_path}/mysqld.log


END_OF


else

cp $_mysql_path/support-files/my-default.cnf /etc/my.cnf

fi


$_mysql_path/scripts/mysql_install_db --user=$_user --defaults-file=$_mysql_config --basedir=$_mysql_path --datadir=$_mysql_data_path  

/etc/init.d/mysqld start

$_mysql_path/bin/mysqladmin -u root password "$_mysql_passwd"

/etc/init.d/mysqld restart

$_mysql_path/bin/mysql -uroot -p$_mysql_passwd -e "delete from mysql.user where Password=' '"

_CHECK_STATS mysql_configure


}



TCMalloc() {


[ -s gperftools-2.0.tar.gz ] || wget -c https:///files/gperftools-2.0.tar.gz

[ -s libunwind-1.1.tar.gz ] || wget -c http://ftp.twaren.net/Unix/NonGNU//libunwind/libunwind-1.1.tar.gz



# 64bit need,but 32bits not need.

cd $_work_path

tar zxf libunwind-1.1.tar.gz

cd libunwind-1.1

./configure --enable-shared

make && make install

_CHECK_STATS libunwind-1.1


echo "/usr/local/lib" >/etc/ld.so.conf.d/usr_local_lib.conf


cd $_work_path

tar zxf  gperftools-2.0.tar.gz

cd gperftools-2.0

./configure --enable-shared --enable-frame-pointers

make && make install

_CHECK_STATS gperftools-2.0

/sbin/ldconfig


# set mysql

cp $_mysql_path/bin/mysqld_safe{,.default}

sed -i '/^# executing mysqld_safe/a\export LD_PRELOAD=/usr/local/lib/' $_mysql_path/bin/mysqld_safe

/etc/init.d/mysqld restart


}



if [ `id -u` -ne 0 ];then

echo "install mysql need root"

exit 1

else

CHECK_FILE

INSTALL_DEPTH

INSTALL_MYSQL

CONFIG_MYSQL

TCMalloc

[ $? -eq 0 ] && echo " $(date +"%F %T")  @@@@@@@ MYSQL COMPLETE @@@@@@ " >> $_work_path/install.log

sleep 5

fi