1    安装前

请安装好fpm,我的博客中有相关教程,不知道为什么word复制到博客会出现有些空格被省略

2    安装mysql

wget http://distfiles.macports.org/cmake/cmake-2.8.12.tar.gz

tar xf cmake-2.8.12.tar.gz

cd cmake-2.8.12

./configure

gmake

gmake install

cd ..

wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.16.tar.gz

tar xf mysql-5.5.16.tar.gz

yum install ncurses-devel libaio-devel -y

cd mysql-5.5.16

cmake .-DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.16 \

-DMYSQL_DATADIR=/application/mysql-5.5.16/data\

-DMYSQL_UNIX_ADDR=/application/mysql-5.5.16/tmp/mysql.sock\

-DDEFAULT_CHARSET=utf8\

-DDEFAULT_COLLATION=utf8_general_ci\

-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii\

-DENABLED_LOCAL_INFILE=ON\

-DWITH_INNOBASE_STORAGE_ENGINE=1\

-DWITH_FEDERATED_STORAGE_ENGINE=1\

-DWITH_BLACKHOLE_STORAGE_ENGINE=1\

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1\

-DWITHOUT_PARTITION_STORAGE_ENGINE=1\

-DWITH_FAST_MUTEXES=1\

-DWITH_ZLIB=bundled\

-DENABLED_LOCAL_INFILE=1\

-DWITH_READLINE=1\

-DWITH_EMBEDDED_SERVER=1\

-DWITH_DEBUG=0

make

使用 DESTDIR= 选项将 mysql安装到指定的目录 /root/fpm-root

mkdir /root/fpm-root –p

make install DESTDIR=/root/fpm-root

3      配置mysql

3.1  配置文件优化

mkdir /root/fpm-root/application/mysql-5.5.16/default/-p

cp /root/mysql-5.5.16/support-files/my-innodb-heavy-4G.cnf /root/fpm-root/application/mysql-5.5.16/default/my.cnf.default

vim  /root/fpm-root/application/mysql-5.5.16/default/my.cnf

[client]

port            = 3306

socket          = /mnt/mysql_3306/mysql.sock

 

[mysql]

no-auto-rehash

 

[mysqld]

user    = mysql

port    = 3306

socket  = /mnt/mysql_3306/mysql.sock

basedir =/application/mysql

datadir =/mnt/mysql_3306/data

open_files_limit    = 1024

back_log = 600

max_connections= 800

max_connect_errors= 3000

table_cache =614

external-locking= FALSE

max_allowed_packet=8M

sort_buffer_size= 1M

join_buffer_size= 1M

thread_cache_size= 100

thread_concurrency= 2

query_cache_size= 2M

query_cache_limit= 1M

query_cache_min_res_unit= 2k

#default_table_type= InnoDB

thread_stack =192K

#transaction_isolation= READ-COMMITTED

tmp_table_size= 2M

max_heap_table_size= 2M

long_query_time= 1

#log_long_format

#log-error =/mnt/mysql_3306/error.log

#log-slow-queries= /mnt/mysql_3306/slow.log

pid-file =/mnt/mysql_3306/mysql.pid

log-bin =/mnt/mysql_3306/mysql-bin

relay-log =/mnt/mysql_3306/relay-bin

relay-log-info-file= /mnt/mysql_3306/relay-log.info

binlog_cache_size= 1M

max_binlog_cache_size= 1M

max_binlog_size= 2M

expire_logs_days= 7

key_buffer_size= 16M

read_buffer_size= 1M

read_rnd_buffer_size= 1M

bulk_insert_buffer_size= 1M

#myisam_sort_buffer_size= 1M

#myisam_max_sort_file_size= 10G

#myisam_max_extra_sort_file_size= 10G

#myisam_repair_threads= 1

#myisam_recover

 

lower_case_table_names= 1

skip-name-resolve

slave-skip-errors= 1032,1062

replicate-ignore-db=mysql

 

server-id = 1

 

innodb_additional_mem_pool_size= 4M

innodb_buffer_pool_size= 32M

innodb_data_file_path= ibdata1:128M:autoextend

innodb_file_io_threads= 4

innodb_thread_concurrency= 8

innodb_flush_log_at_trx_commit= 2

innodb_log_buffer_size= 2M

innodb_log_file_size= 4M

innodb_log_files_in_group= 3

innodb_max_dirty_pages_pct= 90

innodb_lock_wait_timeout= 120

innodb_file_per_table= 0

[mysqldump]

quick

max_allowed_packet= 2M

 

[mysqld_safe]

log-error=/mnt/mysql_3306/mysql_3306.err

pid-file=/mnt/mysql_3306/mysqld.pid

3.2  启动文件优化

cp /root/mysql-5.5.16/support-files/mysql.server/root/fpm-root/application/mysql-5.5.16/support-files/mysql.server.default

vim /root/fpm-root/application/mysql-5.5.16/support-files/mysql

 #!/bin/sh

################################################

#this scripts is created by chenshifei at2016-10-07

#chenshifei QQ:252509628

#blog:http://chenshifei.blog.51cto.com

################################################

 

#init

port=3306

mysql_user="root"

mysql_pwd="chenshifei"

CmdPath="/application/mysql/bin"

mysql_sock="/mnt/mysql_${port}/mysql.sock"

#startup function

function_start_mysql()

{

    if [ !-e "$mysql_sock" ];then

      printf "Starting MySQL...\n"

     /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/mnt/mysql_${port}/my.cnf 2>&1 > /dev/null &

    else

      printf "MySQL is running...\n"

      exit

    fi

}

 

#stop function

function_stop_mysql()

{

    if [ !-e "$mysql_sock" ];then

      printf "MySQL is stopped...\n"

       exit

    else

      printf "Stoping MySQL...\n"

       #read "input mysql_${} password: " mysql_pwd

      ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S/mnt/mysql_${port}/mysql.sock shutdown

   fi

}

 

#restart function

function_restart_mysql()

{

    printf "Restarting MySQL...\n"

   function_stop_mysql

    sleep 2

   function_start_mysql

}

 

case $1 in

start)

   function_start_mysql

;;

stop)

   function_stop_mysql

;;

restart)

   function_restart_mysql

;;

*)

    printf "Usage: /mnt/mysql_${port}/mysql {start|stop|restart}\n"

esac

4      编写脚本

实现安装前、安装后、删除前、删除后的需求

mkdir /root/fpm-scripts

vim /root/fpm-scripts/before-install.sh

/usr/bin/id mysql &>/dev/null

if [ $? -ne 0 ] ; then

   /usr/sbin/useradd -M -s /sbin/nologin mysql

fi

vim /root/fpm-scripts/after-install.sh

/bin/ln -s /application/mysql-5.5.16//application/mysql

echo 'exportPATH=/application/mysql/bin:$PATH'>>/etc/profile

. /etc/profile

export PATH=/application/mysql/bin:$PATH

/bin/mkdir /mnt/mysql_3306/data/ -p

/bin/cp/application/mysql-5.5.16/support-files/mysql /mnt/mysql_3306/mysql

/bin/cp/application/mysql-5.5.16/default/my.cnf   /mnt/mysql_3306/

/bin/find /mnt/mysql_3306/ -type f -name"mysql"|xargs /bin/chmod 700

/bin/chown -R mysql.mysql /mnt/mysql_3306/

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/mnt/mysql_3306/data/ --user=mysql

/bin/chown -R mysql.mysql /mnt/mysql_3306/data

vim /root/fpm-scripts/before-remove.sh

sed -ir '/export PATH=\/application\/mysql\/bin:\$PATH/d' /etc/profile

PID=($(ps -ef |grep mysql|awk '{print $2}'))

if [  ! -z$PID  ] ; then

     for iin $PID

     do

        kill $i &>/dev/null

     done

fi

vim /root/fpm-scripts/after-remove.sh

/bin/rm -f /application/mysql

/bin/rm -rf /application/mysql-5.5.16

5      生成rpm

创建存放包的目录/root/fpm-rpm

mkdir /root/fpm-rpm

打包:

fpm -f -t rpm -n mysql -v 5.5.16 --iteration el6--epoch 1  -s dir -C /root/fpm-root -p/root/fpm-rpm --license 'GPL' -d 'ncurses-devel' -d 'libaio-devel' --before-install/root/fpm-scripts/before-install.sh --after-install/root/fpm-scripts/after-install.sh --before-remove/root/fpm-scripts/before-remove.sh --after-remove/root/fpm-scripts/after-remove.sh --description 'MySQL Server 5.5.16' --url'http://www.mysql.com' -m 'Mr.chen'

测试安装:

cd /root/fpm-rpm/

yum localinstall mysql-5.5.16-el6.x86_64.rpm–y

不知道为什么需要手动执行,可能这个环境变量还是需要换成软连接来访问才好

. /etc/profile

/mnt/mysql_3306/mysql start

mysql -S /mnt/mysql_3306/mysql.sock

6    卸载

出现error:"mysql-5.5.16-el6" specifies multiple packages:的卸载办法:

rpm -e --allmatches  mysql-5.5.16-el6.x86_64

 

7      多实例复制

mkdir /mnt/mysql_3307/

\cp  /mnt/mysql_3306/my.cnf /mnt/mysql_3307/

\cp  /mnt/mysql_3306/mysql /mnt/mysql_3307/

sed -i s#3306#3307#g'' /mnt/mysql_3307/my.cnf

sed -i s#server-id = 1#server-id = 2#g'' /mnt/mysql_3307/my.cnf

sed -i s#3306#3307#g'' /mnt/mysql_3307/mysql

chown -R mysql.mysql /mnt/mysql_3307

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/mnt/mysql_3307/data/ --user=mysql

/bin/chown -R mysql.mysql /mnt/mysql_3307/data

chmod -R 700 /mnt/mysql_3307/mysql

启动

/mnt/mysql_3307/mysql start

连接

mysql -S /mnt/mysql_3307/mysql.sock