编译安装:


创建MySQL专用账户和用户组:

groupaddmysql

useradd-g mysql mysql

设置用户操作系统资源的限制,在/etc/security/limits.conf最后添加

mysql      soft  nproc      2047

mysql      hardnproc      16384

mysql      soft  nofile      1024

mysql      hardnofile      65536

 

执行cmake . -LAH查看MySQL编译所有支持的参数,或者通过http://dev/mysql.com/doc/refman/5.6/en/source-configuration-options.html查看官方技术文档

 

安装cmake:

yuminstall cmake

 

出现问题:Generic Linux (Architecture Independent), Compressed TAR Archive

解决:yum -y install ncurses-devel
出现问题:CMAKE_C_COMPILER not set, after EnableLanguage
解决:yum install gcc-c++


rm -f CMakeCache.txt

重新编译安装

 

解压源码包:tar -zvfx mysql-5.6.28.tar.gz

进入解压目录,执行cmake编译并安装:

cdmysql-5.6.28

cmake. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_COLLATION=utf8_general_ci-DDEFAULT_CHARSET=utf8 -DENABLED_LOCAL_INFILE=ON-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1-DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DCOMPILATION_COMMENT='JSSfor mysqltest' -DWITH_READLINE=ON -DSYSCONFDIR=/data/mysqldata/3306-DMYSQL_UNIX_ADDR=/data/mysqldata/3306/mysql.sock

 make && make install

 

修改软件所在目录拥有者为mysql

chown-R mysql:mysql /usr/local/mysql

修改mysql用户环境变量,在/home/mysql/.bash_profile添加

# exportLANG=zh_CN.GB18030

exportPATH=/usr/local/mysql/bin:$PATH

 

 

 

管理MySQL数据库服务

MySQL数据库服务:MySQL软件、MySQL实例和MySQL数据库几部分的总称

MySQL实例:指MySQL进程以及该进程持有的内存资源

MySQL数据库:MySQLdata目录或MySQLdatabase文件

mysql数据库:mysql这个DB

mysql:连接MySQL数据库的命令行方式交互工具mysql

 

创建数据库文件默认路径:

mkdir -p/data/mysqldata/{3306/{data,tmp,binlog},backup,scripts}

chown -R mysql:mysql /data/mysqldata

chown -R mysql:mysql /usr/local/mysql

 

 

 

添加MySQL配置文件/data/mysqldata/3306/my.cnf如下:

[client]

port = 3306

socket = /data/mysqldata/3306/mysql.sock

 

# The MySQL server

[mysqld]

port   = 3306

user   = mysql

socket = /data/mysqldata/3306/mysql.sock

pid-file        = /data/mysqldata/3306/mysql.pid

basedir = /usr/local/mysql

datadir = /data/mysqldata/3306/data

tmpdir = /data/mysqldata/3306/tmp

open_files_limit       = 10240

explicit_defaults_for_timestamp

sql_mode        =NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

# Buffer

max_allowed_packet      = 256M

max_heap_table_size     = 256M

net_buffer_length   = 8K

sort_buffer_size        = 2M

join_buffer_size        = 4M

read_buffer_size        = 2M

read_rnd_buffer_size    = 16M

 

# Log

log-bin =/data/mysqldata/3306/binlog/mysql-bin

binlog_cache_size       = 32m

max_binlog_cache_size   = 512m

max_binlog_size = 512m

binlog_format   = mixed

log_output      = FILE

log-error       = ../mysql-error.log

slow_query_log  = 1

slow_query_log_file     = ../slow_query.log

general_log     = 0

general_log_file        = ../general_query.log

expire-logs-days        = 14

 

# InnoDB

innodb_data_file_path   = ibdata1:2048M:autoextend

innodb_log_file_size    = 256M

innodb_log_files_in_group       = 3

innodb_buffer_pool_size = 1024M

 

[mysql]

no-auto-rehash

prompt = (\u@\h) [\d]\_

default-character-set   = gbk

 

初始化MySQL数据库:

/usr/local/mysql/scripts/mysql_install_db--datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql

 

启动MySQL服务:

$ /usr/local/mysql/bin/mysqld_safe--defaults-file=/data/mysqldata/3306/my.cnf &

 

连接数据库:

$ /usr/local/mysql/bin/mysql

 

 

检查MySQL服务是否启动:

netstat -tnl | grep 3306

 

 

 

保留一条允许root用户从本地连接的记录外,删除全部其他用户:

delete from mysql.user where (user,host)not in (select 'root','localhost');

修改管理员用户:

update mysql.user setuser='system',password=password('password');

 

truncate table mysql.db;

生效所有操作:

flush privileges;

 

 

MySQL服务管理配置

创建中间定义文件,提高脚本的复用性:

$ vi/data/mysqldata/scripts/mysql_env.ini

# set env

MYSQL_USER=system

MYSQL_PASS=’password’

 

# check parameter

if [ $# -ne 1 ]

then

       HOST_PORT=3306

else

       HOST_PORT=$1

fi

 

修改该文件在操作系统层的权限:

$ chmod 600/data/mysqldata/scripts/mysql_env.ini

 

创建mysql_db_startup.sh脚本,用于启动MySQL服务:

$ vi/data/mysqldata/scripts/mysql_db_startup.sh

#!/bin/sh

 

source/data/mysqldata/scripts/mysql_env.ini

 

echo "Stratup MySQL Service:local_"${HOST_PORT}

/usr/local/mysql/bin/mysqld_safe--defaults-file=/data/mysqldata/${HOST_PORT}/my.cnf &

 

创建mysql_db_shutdown.sh脚本,用于关闭MySQL服务:

$ vi /data/mysqldata/scripts/mysql_db_shutdown.sh

#!/bin/sh

 

source/data/mysqldata/scripts/mysql_env.ini

 

echo "Shutdown MySQL Service:localhost_"${HOST_PORT}

/usr/local/mysqldata/bin/mysqladmin-u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sockshutdown

 

创建mysqlplus.sh脚本文件用于快速登录:

$ vi/data/mysqldata/scripts/mysqlplus.sh

         

#!/bin/sh

 

source/data/mysqldata/scripts/mysql_env.ini

 

echo "Login MySQL Service:localhost_"${HOST_PORT}

/usr/local/mysql/bin/mysql-u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sock $2

 

授权以上脚本执行权限:

$ chmod +x /data/mysqldata/scripts/*.sh

编辑mysql用户的环境变量,将上述路径加入到PATH中:

$ echo "exportPATH=/data/mysqldata/scripts:$PATH">>~/.bash_profile

$ source ~/.bash_profile

 

编辑/etc/rc.local使MySQL开机启动,在最后添加:

sudo -i -u mysql/data/mysqldata/scripts/mysql_db_startupsh 3306 >/home/mysql/mysql_db_startup.log 2>&1

 

visudoDefaultsrequiretty注释掉,避免sudo: sorry,you myst have a ttyto run sudo