一
、目录规划
MySQL 的目录包括 base 目录和 data 目录,base 目录存放软件文件,每个版本的软件单
独一个目录,例如/usr/local/mysql/mysql5.6.19。data 目录存放数据文件,以端口号区分
每个实例,例如我们需要创建一个端口号为 3306 的实例,它的目录就是
/data/mysqldata/3306/{data,binlog,tmp}。 注:一个版本的软件只需要安装一次,即可创
建多个端口号不同的实例(通过初始化命令创建)。
base 目录:
/usr/local/mysql/mysql5.6.19
/usr/local/mysql/mysql5.6.21
/usr/local/mysql/mysql5.6.27
/usr/local/mysql/mysql5.7.11
/usr/local/mysql/mysql5.6.13
data 目录
/data/mysqldata/{3306/{data,binlog,tmp},backup,scripts}
/data/mysqldata/{3307/{data,binlog,tmp},backup,scripts}
/data/mysqldata/{3308/{data,binlog,tmp},backup,scripts}
二、MySQL5.6 安装方法
2.1 查看系统中是否安装 mysql(root 用户)
[root@localhost ~]# rpm -qa|grep mysql
结果显示已安装
mysql-libs-5.1.52-1.el6_0.1.x86_64
卸载已安装的 mysql:
[root@localhost ~]# rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64
2.2 检查是否依赖包已安装,使用以下命令:(root 用户)
rpm -qa make gcc-c++ cmake bison-devel ncurses-devel 查看系统有没安装相应的包
不存在的依赖包需要安装,使用源安装:
yum -y install make gcc-c++ cmake bison-devel ncurses-devel
2.3 创建用户和组(root 用户)
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -g mysql mysql
[root@localhost ~]# passwd mysql
[root@localhost ~]# vi /etc/security/limits.conf
mysql soft nproc 2047
mysql hard nproc 16384
mysql soft nofile 1024
mysql hard nofile 65536
2.4 解压安装包(root 用户)
解压安装包到当前目录:
[root@localhost ~]# tar -zxvf mysql-5.6.12.tar.gz
得到 mysql-5.6.12
2.5 新建 mysql 所需要的目录(root 用户)
新建 mysql 数据目录
[root@localhost ~]# mkdir -p /data/mysqldata/{3306/{data,tmp,binlog},backup,scripts}
[root@localhost ~]# chown -R mysql:mysql /data/mysqldata
2.6 编译安装 mysql(root 用户)
[root@localhost ~]# cd mysql-5.6.12
[root@localhost ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/mysql5.6.12
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-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='mysql of drizzle'
-DWITH_READLINE=ON
-DSYSCONFDIR=/data/mysqldata/3306
-DMYSQL_UNIX_ADDR=/data/mysqldata/3306/mysql.sock
[root@localhost ~]# make && make install
(注:重新运行配置,需要删除 CMakeCache.txt 文件,make 失 败,执行 make clean)
2.7 修改权限和环境变量(root 用户)
chown -R mysql:mysql /usr/local/mysql/mysql5.6.12
vi /home/mysql/.bash_profile
export PATH=/usr/local/mysql/mysql5.6.12/bin:$PATH
2.8 初始化数据库(mysql 用户)
2.8.1 配置参数文件
[root@localhost ~]# su - mysql
[mysql@localhost ~]$ vi /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/mysql5.7.11
datadir = /data/mysqldata/3306/data
tmpdir = /data/mysqldata/3306/tmp
character_set_server = utf8
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode = ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
skip-name-resolve
server_id = 1
lower_case_table_names = 1
max_connections = 300
log_bin_trust_function_creators = 1
autocommit = 0
event_scheduler = 1
# Buffer
max_allowed_packet = 32M
max_heap_table_size = 32M
net_buffer_length = 8K
sort_buffer_size = 512K
join_buffer_size = 256K
read_buffer_size = 512K
read_rnd_buffer_size = 256K
key_buffer_size = 16M
key_cache_block_size = 4K
myisam_sort_buffer_size = 512K
# Log
log-bin = /data/mysqldata/3306/binlog/master-bin
log-bin-index = /data/mysqldata/3306/binlog/master-bin.index
binlog_cache_size = 4m
max_binlog_cache_size = 8m
max_binlog_size = 256m
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 = ibdata01.df:1024M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size =512M
innodb_flush_method = O_DIRECT
[mysql]
no-auto-rehash
prompt = (u@h)[d]>\_
default-character-set = utf8
2.8.2 初始化数据库
[mysql@localhost ~]$ cd /usr/local/mysql/mysql5.6.12
[mysql@localhost ~]$ /usr/local/mysql/mysql5.6.12/scripts/mysql_install_db --datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql/mysql5.6.12
2.8.3 启动数据库
[mysql@localhost ~]$ /usr/local/mysql/mysql5.6.12/bin/mysqld_safe --defaults- file=/data/mysqldata/3306/my.cnf &
2.8.4 配置数据库 [mysql@localhost ~]$ mysql
(root@localhost)[(none)]> select user, host from mysql.user;
(root@localhost)[(none)]> delete from mysql.user where host = '::1';
(root@localhost)[(none)]> delete from mysql.user where user = '';
(root@localhost)[(none)]> update mysql.user set password=password('abc123456');
(root@localhost)[(none)]> flush privileges;
(root@localhost)[(none)]> exit;
2.8.5 配置管理脚本
2.8.5.1 创建公共脚本
[mysql@localhost ~]$ vi /data/mysqldata/scripts/mysql_env.ini
# set env
# check parameter
MYSQL_USER=root
if [ $# -ne 1 ]
then
HOST_PORT=3306
else
HOST_PORT=$1
fi
2.8.5.2 创建启动脚本
[mysql@localhost ~]$ vi /data/mysqldata/scripts/startup_mysql.sh
#!/bin/sh
source /data/mysqldata/scripts/mysql_env.ini
echo "Startup MySql Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/${HOST_PORT}/my.cnf &
2.8.5.3 创建关闭脚本
[mysql@localhost ~]$ vi /data/mysqldata/scripts/shutdown_mysql.sh
#!/bin/sh
source /data/mysqldata/scripts/mysql_env.ini
echo "Shutdown MySql Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysqladmin -uroot -p -S
/data/mysqldata/${HOST_PORT}/mysql.sock shutdown
2.8.5.4 创建登录脚本
[mysql@localhost ~]$ 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 -uroot –p -S /data/mysqldata/${HOST_PORT}/mysql.sock
[mysql@localhost ~]$ chmod u+x /data/mysqldata/scripts/*.sh
2.8.5.5 配置环境变量
[mysql@localhost ~]$ vi ~/.bash_profile
export PATH=/data/mysqldata/scripts:/usr/local/mysql/mysql5.6.12/bin:$PATH
三、MySQL5.7 安装方法
3.1 安装依赖包(root 用户)
yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml
libgcrypt libtool bison
3.2 安装 boost(root 用户)
先清理掉就的 boost
rpm -qa boost*
yum -y remove boost-*
安装 boost1.59.0(与 mysql-5.7.11 对应的版本)
wget -c
http://liquidtelecom.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.
gz(也可以自己下载下来,解压后放在/usr/local/boost)
mv boost_1_59_0 /usr/local/boost
3.3 创建用户 mysql(root 用户)
groupadd mysql;
useradd -g mysql mysql
passwd mysql
3.4 创建数据库安装所需目录(root 用户)
mkdir -p /data/mysqldata/{3306/{data,tmp,binlog},backup,scripts}
chown -R mysql:mysql mysqldata
3.5 设置 mysql 操作系统资源(root 用户)
vi /etc/security/limits.conf
mysql soft nproc 2047
mysql hard nproc 16384
mysql soft nofile 1024
mysql hard nofile 65536
3.6 解压编译安装(root 用户)
tar zxvf mysql-5.7.11.tar.gz
cd mysql-5.7.11
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/mysql5.7.11
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-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
-DSYSCONFDIR=/data/mysqldata/3306
-DMYSQL_UNIX_ADDR=/data/mysqldata/3306/mysql.sock
-DDOWNLOAD_BOOST=1
-DWITH_BOOST=/usr/local/boost
若编译错误删除 CMakeCache.txt,然后重新编译;make 失败后执行 make clean 进
行清理。
make && make install --注意 make 是内存使用量会超过 2G
注意事项:
如安装失败重新编译时,需要清除旧的对象文件和缓存信息。
make clean
rm -f CMakeCache.txt
rm -rf /etc/my.cnf
3.7 修改权限和环境变量(root 用户)
chown -R mysql:mysql /usr/local/mysql/mysql5.7.11
vi /home/mysql/.bash_profile
export PATH=/usr/local/mysql/mysql5.7.11/bin:$PATH
3.8 配置参数文件(mysql 用户)
su - mysql
vi /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/mysql5.7.11
datadir = /data/mysqldata/3306/data
tmpdir = /data/mysqldata/3306/tmp
character_set_server = utf8
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode = ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_ FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
skip-name-resolve
server_id = 1
lower_case_table_names = 1
max_connections = 300
log_bin_trust_function_creators = 1
autocommit = 0
event_scheduler = 1
# Buffer
max_allowed_packet = 32M
max_heap_table_size = 32M
net_buffer_length = 8K
sort_buffer_size = 512K
join_buffer_size = 256K
read_buffer_size = 512K
read_rnd_buffer_size = 256K
key_buffer_size = 16M
key_cache_block_size = 4K
myisam_sort_buffer_size = 512K
# Log
log-bin = /data/mysqldata/3306/binlog/master-bin
log-bin-index = /data/mysqldata/3306/binlog/master-bin.index
binlog_cache_size = 4m
max_binlog_cache_size = 8m
max_binlog_size = 256m
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 = ibdata01.df:1024M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size =512M
innodb_flush_method = O_DIRECT
[mysql]
no-auto-rehash
prompt = (u@h)[d]>\_
default-character-set = utf8
3.9 初始化数据库(mysql 用户)
cd /usr/local/mysql/mysql5.7.11
bin/mysqld --defaults-file=/data/mysqldata/3306/my.cnf --initialize
从 mysql 错误日志文件查看最后一行生成随机密码。
cd /data/mysqldata/3306/
tail -1 mysql-error.log
3.10 启动服务(mysql 用户)
cd /usr/local/mysql/mysql5.7.11/bin
./mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
3.11 配置用户(mysql 用户)
mysql -uroot -p --输入刚才生成的密码
alter user 'root'@'localhost' identified by 'abc123456';
select user, host from mysql.user;
或者
update mysql.user set authentication_string=password('abc123456') where user='root'
and host = 'localhost';
或者
set password = password('abc123456');
flush privileges;
3.12 启动脚本
3.12.1 创建公共脚本
[mysql@localhost ~]$ vi /data/mysqldata/scripts/mysql_env.ini
# set env
MYSQL_USER=root
# check parameter
if [ $# -ne 1 ]
then
HOST_PORT=3306
else
HOST_PORT=$1
fi
3.12.2 创建启动脚本
[mysql@localhost ~]$ vi /data/mysqldata/scripts/startup_mysql.sh
#!/bin/sh
source /data/mysqldata/scripts/mysql_env.ini
echo "Startup MySql Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysqld_safe file=/data/mysqldata/${HOST_PORT}/my.cnf &
3.12.3 创建关闭脚本
[mysql@localhost ~]$ vi /data/mysqldata/scripts/shutdown_mysql.sh
#!/bin/sh
source /data/mysqldata/scripts/mysql_env.ini
echo "Shutdown MySql Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p -S /data/mysqldata/${HOST_PORT}/mysql.sock shutdown
3.12.4 创建登录脚本
[mysql@localhost ~]$ 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 -S /data/mysqldata/${HOST_PORT}/mysql.sock
[mysql@localhost ~]$ chmod u+x /data/mysqldata/scripts/*.sh
3.12.5 配置环境变量
[mysql@localhost ~]$ vi ~/.bash_profile
export PATH=/data/mysqldata/scripts:/usr/local/mysql/mysql5.7.11/bin:$PATH