2

MySQL 的安装方式一般分为三种,二进制版本、编译版本、RPM 包。比较常见的是二进制版本安装,方便简单,相对于编译安装,如果不是追求极致性能,使用起来差别不大。本次教程以二进制版本为例,系统为 centos6.8,MySQL 版本为5.7.20。

下载安装包

先去官网下载二进制安装包,进入官网下载页面 mysql下载页面,点击下载 5.7.20 二进制 64 位版本

图片描述

下载完成,把安装包移到目标 Linux 下

环境准备

1 安装依赖

yum install libaio numactl -y

2 创建用户以及用户组

groupadd mysql
useradd -m -r -g mysql mysql

3 配置 ulimit

我们一般使用 mysql 用户启动数据库,如果不设置文件打开数和进程数,后期会有问题

打开 /etc/security/limits.conf 文件,在文件末尾写上

mysql     soft    nproc    65536
mysql     hard    nproc    65536
mysql     soft    nofile   65536
mysql     hard    nofile   65536

打开 /etc/security/limits.d/90-nproc.conf 把参数调整为

*          soft    nproc     65536
root       soft    nproc     unlimited
*          soft    nofile    65536
root       soft    nofile    unlimited

4 在 hosts 文件加上本机解析

如果不设置 hosts 本机解析,偶然会出现一些诡异的问题,所以还是加上比较好

host_ip=$(ifconfig | grep inet | grep cast | awk '{print $2}' | awk -F: '{print $NF}' | head -1)
echo "$host_ip `hostname`" >> /etc/hosts

5 开启时间同步

时间同步对于数据库来说非常重要,请保证所有数据库节点时间是一致的

yum install -y ntp
service ntpd start

6 关闭 iptable

数据库一般不允许通外网,如果对防火墙不是很熟悉,可以关掉; 如果网络环境不够安全,可以开启

service iptables stop
chkconfig iptables off

7 关闭 selinux

selinux 可能会造成无法写入数据,如果对它不熟悉,建议关掉

查看 selinux 状态

getenforce

临时关闭

setenforce 0

永久关闭

打开 /etc/sysconfig/selinux

SELINUX=enforcing 改为 SELINUX=disabled

安装

这次安装数据目录为 /data/mysql_db/,日志目录为 /data/mysql_log/,实例名为 mysql_test,启动用户为 mysql

1 解压文件

把二进制文件解压出来,放到 /user/local/ 目录下

tar -xvzf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql57

如果想要全局访问到 mysql 客户端,可以把 /usr/local/mysql57/bin 加入系统路径,我这里采取直接创建软连接的方式

ln -s  /usr/local/mysql57/bin/mysql /usr/bin/mysql
ln -s  /usr/local/mysql57/bin/mysqlbinlog /usr/bin/mysqlbinlog

2 创建数据和日志目录

mkdir -p /data/mysql_db/mysql_test
mkdir -p /data/mysql_log/mysql_test

3 初始化目录

/usr/local/mysql57/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test --user=mysql
/usr/local/mysql57/bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test

正常输出

[root@mysql-test ~]# /usr/local/mysql57/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test --user=mysql
2018-06-03T05:19:31.469841Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-06-03T05:19:32.525091Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-06-03T05:19:32.751099Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-06-03T05:19:32.861647Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b2d883be-66ed-11e8-bd72-90b11c1a653a.
2018-06-03T05:19:32.877177Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-06-03T05:19:32.877705Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@mysql-test ~]# /usr/local/mysql57/bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test
Generating a 2048 bit RSA private key
............................................................................+++
....+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
......................................+++
...........................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
..............................................................................+++
..................................................+++
writing new private key to 'client-key.pem'
-----

4 生成配置文件

一台机器上有可能会存在多个 MySQL 实例,我们可以把 my.cnf 文件放到对应实例的数据目录下,由于 MySQL 会受到默认配置文件的影响,所以我们要把 /etc/my.cnf 移走, 然后创建新的 my.cnf 文件

rm -f /etc/my.cnf
vim /data/mysql_db/mysql_test/my.cnf

本次安装参考配置,具体参数结合服务器情况调整

# my.cnf
[client]
port = 3306
default-character-set =utf8
socket = /data/mysql_db/mysql_test/mysql.sock

[mysqld]
datadir = /data/mysql_db/mysql_test
basedir = /usr/local/mysql57
tmpdir = /tmp
socket = /data/mysql_db/mysql_test/mysql.sock
pid-file = /data/mysql_db/mysql_test/mysql.pid
skip-external-locking = 1
skip-name-resolve = 1
port = 3306
server_id = 1

default-storage-engine = InnoDB
character-set-server = utf8
default_password_lifetime=0

#### log ####
binlog_cache_size = 16M
log_bin = /data/mysql_log/mysql_test/mysql-bin
log_bin_index = /data/mysql_log/mysql_test/mysql-bin.index
binlog_format = row
expire_logs_days = 15

relay_log_recovery=ON
relay_log=/data/mysql_log/mysql_test/mysql-relay-bin
relay_log_index=/data/mysql_log/mysql_test/mysql-relay-bin.index

log_error = /data/mysql_log/mysql_test/mysql-error.log
log_queries_not_using_indexes = /data/mysql_log/mysql_test/nouseindex.log

slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql_log/mysql_test/mysql-slow.log

#### innodb ####
innodb_buffer_pool_size = 4G
innodb_buffer_pool_instances = 8
innodb_log_group_home_dir = /data/mysql_log/mysql_test/
innodb_undo_directory = /data/mysql_log/mysql_test/
innodb_undo_logs = 128
innodb_flush_neighbors = 1
innodb_log_file_size =  1G
innodb_file_per_table = on 

bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1

log_timestamps=system

[mysqldump]
quick
max_allowed_packet = 64M

[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 32M
read_buffer = 16M
write_buffer = 16M

5 生成启动文件

cp /usr/local/mysql57/support-files/mysql.server /etc/init.d/mysqld-test
vim /etc/init.d/mysqld-test

basedir=
datadir=
lock_file_path="$lockdir/mysql"
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &

改为, 手动指定配置文件和数据目录

basedir=/usr/local/mysql57
datadir=/data/mysql_db/mysql_test
default_file=/data/mysql_db/mysql_test/my.cnf
lock_file_path="$lockdir/mysql-test"
$bindir/mysqld_safe  --defaults-file="$default_file" --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

把数据目录的所有者改为 mysql

chown mysql.mysql /etc/init.d/mysqld-test 
chown mysql.mysql -R /data/mysql*

启动实例

们以 mysql 用户来启动

su - mysql
[mysql@chengqm ~]$ /etc/init.d/mysqld-test start
Starting MySQL.. SUCCESS!

进入 mysql 客户端

mysql -S /data/mysql_db/mysql_test/mysql.sock

到此,MySQL 已经成功安装并启动, 后续可以修改密码. 如果有报错,查看错误日志,根据具体报错信息进行修改。如果报错日志里面什么也没有,可能是配置文件有问题,或者读到了默认配置。


程淇铭
4.2k 声望1.7k 粉丝

今晚请吃饭,随便坐