前言: MySQL二进制包,定死了basedir为/usr/local/mysql/,但是很多人喜欢自定义目录,比如我就喜欢放/opt/app/mysql 数据目录喜欢自定义为/data/mydata/,以前必须把basedir必须做个软链接到/usr/local/mysql,本人有轻微的强迫症,就不想这么做,下面就来看实现过程。
1创建组,创建用户
groupadd -g3306 mysql
useradd -u3306 -M -s /sbin/nologin mysql
2.解压二进制包,创建数据存放目录
mkdir -p /opt/app/
mkdir -p /data/mydata
cp mysql-5.6.20-linux-glibc2.5-x86_64.tar.gz/opt/app
cd /opt/app
tar xvf mysql-5.6.20-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.20-linux-glibc2.5-x86_64 mysql
chown -Rmysql.mysql /opt/app/mysql /data/mydata
3.定义环境变量
vim /etc/profile.d/mysql.sh
export MYSQL_HOME=/opt/app/mysql
export PATH=$MYSQL_HOME/bin:$PATH
su -
4.准备启动脚本和添加服务,确保启动脚本是755权限
cp -rpf support-files/mysql.server /etc/init.d/mysql
ll /etc/init.d/mysql
-rwxr-xr-x 1 mysql mysql 10880 07-19 00:24/etc/init.d/mysql
5.准备配置文件,我喜欢放basedir目录,你也可以放/etc/my.cnf
vim/opt/app/mysql/my.cnf
[client]
socket=/opt/app/mysql/mysql.socket
[mysqld]
basedir = /opt/app/mysql
datadir = /data/mydata
port = 3306
user = mysql
server_id = 11
socket = /opt/app/mysql/mysql.socket
skip-name-resolve
skip_external_locking = 1
explicit_defaults_for_timestamp
log-error = /opt/app/mysql/mysql-error.log
slow_query_log = 1
slow_query_log_file =/opt/app/mysql/mysql-slow.log
character_set_server = gbk
innodb_file_per_table = 1
innodb_data_home_dir = /data/mydata
innodb_data_file_path =ibdata1:1024M:autoextend
innodb_buffer_pool_size = 1024M
innodb_additional_mem_pool = 32M
innodb_log_buffer_size = 32M
innodb_flush_log_at_trx_commit = 2
innodb_open_files = 10000
innodb_thread_concurrency = 32
innodb_read_io_threads = 8
innodb_write_io_threads =8
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_log_group_home_dir = /opt/app/mysql
innodb_flush_method = O_DIRECT
innodb_io_capacity = 2000
innodb_undo_tablespaces = 2
innodb_undo_logs = 128
innodb_undo_directory = /opt/app/mysql
max_connections = 5000
max_connect_errors = 100000
read_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = on
query_cache_size = 64M
tmp_table_size = 512M
max_heap_table_size = 512M
join_buffer_size = 2M
open_files_limit= 65535
thread_cache_size= 128
thread_concurrency= 12
back_log= 65535
max_allowed_packet = 64M
wait_timeout=2147483
key_buffer_size= 512M
bulk_insert_buffer_size = 16M
[myisamchk]
key_buffer_size= 64M
read_buffer_size = 2M
sort_buffer_size = 2M
write_buffer_size = 2M
[mysqld_safe]
pid-file=/opt/app/mysql/mysqld.pid
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
6.初始化MySQL
./mysql_install_db --basedir=/opt/app/mysql--datadir=/data/mydata --defaults-file=/opt/app/mysql/my.cnf
7.检查能否读取到配置文件
mysqld --verbose --help | grep cnf
2014-09-12 11:20:21 13784 [Warning] Using unique option prefix innodb_additional_mem_pool instead of innodb-additional-mem-pool-size is deprecated and will be removed in a future release. Please use the full name instead.
2014-09-12 11:20:21 13784 [Note] Plugin ‘FEDERATED‘ is disabled.
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf /opt/app/mysql/my.cnf ~/.my.cnf
my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default
2014-09-12 11:20:21 13784 [Note] Binlog end
2014-09-12 11:20:21 13784 [Note] Shutting down plugin ‘CSV‘
2014-09-12 11:20:21 13784 [Note] Shutting down plugin ‘MyISAM
8.最重要的一步来了,启动MySQL,如果不针对启动脚本修改,不做软连接,是无法启动的
[[email protected]]# /etc/init.d/mysql start
/etc/init.d/mysql:line 256: my_print_defaults: command not found
/etc/init.d/mysql:line 276: cd: /usr/local/mysql: 没有那个文件或目录
StartingMySQLCouldn‘t find MySQL server (/usr/local/mysql/[失败]sqld_safe)
[[email protected]]# vim /etc/init.d/mysql
#################截取了一段
basedir=
datadir=
# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don‘t wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900
# Lock directory for RedHat / SuSE.
lockdir=‘/var/lock/subsys‘
lock_file_path="$lockdir/mysql"
# The following variables are only set for letting mysql.server find things.
# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/usr/local/mysql/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
#####################################
启动脚本写死了目录
就算你给了这basedir datadir 两个变量,还是一样启动不起来
分析一下启动脚本,可以发现问题
sh -x/etc/init.d/mysql start
[[email protected] scripts]# sh -x /etc/init.d/mysql start
+ basedir=
+ /opt/app/mysql
/etc/init.d/mysql: line 46: /opt/app/mysql: is a directory
+ datadir=
+ /data/mydata
/etc/init.d/mysql: line 48: /data/mydata: is a directory
+ service_startup_timeout=900
+ lockdir=/var/lock/subsys
+ lock_file_path=/var/lock/subsys/mysql
+ mysqld_pid_file_path=
+ test -z ‘‘
+ basedir=/usr/local/mysql
+ bindir=/usr/local/mysql/bin
+ test -z ‘‘
+ datadir=/usr/local/mysql/data
+ sbindir=/usr/local/mysql/bin
+ libexecdir=/usr/local/mysql/bin
+ datadir_set=
+ lsb_functions=/lib/lsb/init-functions
+ test -f /lib/lsb/init-functions
+ . /lib/lsb/init-functions
+ PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/mysql/bin
+ export PATH
+ mode=start
+ ‘[‘ 1 -ge 1 ‘]‘
+ shift
+ other_args=
+ case `echo "testing\c"`,`echo -n testing` in
++ echo ‘testing\c‘
++ echo -n testing
+ echo_n=-n
+ echo_c=
+ test -x ./bin/my_print_defaults
+ test -x /usr/local/mysql/bin/my_print_defaults
+ test -x /usr/local/mysql/bin/mysql_print_defaults
+ conf=/etc/my.cnf
+ print_defaults=
+ test -r /etc/my.cnf
+ test -z ‘‘
+ print_defaults=my_print_defaults
+ extra_args=
+ test -r /usr/local/mysql/my.cnf
+ test -r /usr/local/mysql/data/my.cnf
++ my_print_defaults mysqld server mysql_server mysql.server
/etc/init.d/mysql: line 257: my_print_defaults: command not found
+ parse_server_arguments
+ test -z ‘‘
++ hostname
+ mysqld_pid_file_path=/usr/local/mysql/data/i-718-40065-VM.pid
+ case "$mode" in
+ cd /usr/local/mysql
/etc/init.d/mysql: line 277: cd: /usr/local/mysql: 没有那个文件或目录
+ echo -n ‘Starting MySQL‘
Starting MySQL+ test -x /usr/local/mysql/bin/mysqld_safe
+ log_failure_msg ‘Couldn‘\‘‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)‘
+ /etc/redhat-lsb/lsb_log_message failure ‘Couldn‘\‘‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)‘
Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_saf[失败]
SHELL脚本我是比较渣渣,为什么刚才给了一个变量,他还认不到了,找到PATH
居然认不到我就给他,因为刚才我已经定义了MySQL变量,所以这边我就写$PATH
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH
修改成
PATH="$PATH:/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH
再次启动一下
9:检查一下,启动成功
[[email protected] scripts]# netstat-lntp | grep 3306
tcp 0 0 :::3306 :::* LISTEN 13699/mysqld
[[email protected] scripts]#