千金良方:MySQL性能优化金字塔法则
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

1.2 初始化安装

1.2.1 下载二进制安装文件

使用wget下载MySQL 5.6.35二进制安装文件并存放在/root目录下。

[root@localhost ~]# cd /root
[root@localhost ~]# wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-\x86_64.tar.gz
[root@localhost ~]# ll mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
-rw-r--r--1 root root 314581668 2  12 23:04 mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

1.2.2 创建mysql用户

先创建mysql组,再创建mysql用户并加入mysql组中。

[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd mysql -r -g mysql
# 验证用户组和用户
[root@localhost ~]# id mysql
uid=500(mysql)gid=500(mysql)=500(mysql)

1.2.3 创建程序、数据存放目录

按照如下路径规范创建MySQL的程序、数据存放路径。

[root@localhost ~]# mkdir /home/mysql/{program, data, conf} -p
[root@localhost~]#mkdir/home/mysql/data/mysqldata1/{mydata, sock, tmpdir, log, innodb_ts, \innodb_log, undo, slowlog, binlog, relaylog} -p
# 查看创建目录结果
[root@localhost ~]# tree /home/mysql/  #如果没有这个命令,就使用yum install tree -y 安装
/home/mysql/
├—— conf
├—— data
|   └—— mysqldata1
|       ├—— binlog
|       ├—— innodb_log
|       ├—— innodb_ts
|       ├—— log
|       ├—— mydata
|       ├—— slowlog
|       ├—— sock
|       ├—— tmpdir
|       └—— undo
|       └—— relaylog
└—— program
13 directories, 0 files

1.2.4 解压缩二进制安装文件并设置目录权限

把二进制安装文件解压缩到/home/mysql/program目录下,并修改程序、数据存放路径宿主、属组为mysql,使MySQL用户对这些目录和文件有完全访问权限。

[root@localhost ~]# cd /root
[root@localhost ~]# tar xf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz -C/home/mysql/\program/
[root@localhost ~]# chown mysql.mysql /home/mysql -R
# 查看datadir关键目录的权限是否正确
[root@localhost ~]# ll /home/mysql/data/mysqldata1/
总用量 36
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 binlog
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 innodb_log
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 innodb_ts
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 log
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 mydata
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 slowlog
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 sock
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 tmpdir
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 undo
drwxr-xr-x 2 mysql mysql 4096 2  12 23:07 relaylog

1.2.5 软链接程序路径,并设置MySQL命令环境变量

把/home/mysql/program/mysql-5.6.35-linux-glibc2.5-x86_64路径软链接到MySQL默认的程序访问路径/usr/local/mysql下,并把/usr/local/mysql/bin/添加到系统环境变量中,以便使用mysql相关命令时不需要输入绝对路径。

[root@localhost ~]# ln -s\
/home/mysql/program/mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
# 查看basedir关键程序目录是否可用
[root@localhost ~]# ll /usr/local/mysql/
总用量 68
drwxr-xr-x  2 mysql mysql  4096 2  12 23:05 bin
-rw-r--r--  1 mysql mysql 17987 11 28 21:36 COPYING
drwxr-xr-x  3 mysql mysql  4096 2  12 23:04 data
drwxr-xr-x  2 mysql mysql  4096 2  12 23:05 docs
drwxr-xr-x  3 mysql mysql  4096 2  12 23:05 include
drwxr-xr-x  3 mysql mysql  4096 2  12 23:04 lib
drwxr-xr-x  4 mysql mysql  4096 2  12 23:05 man
drwxr-xr-x 10 mysql mysql  4096 2  12 23:05 mysql-test
-rw-r--r--  1 mysql mysql  2496 11 28 21:36 README
drwxr-xr-x  2 mysql mysql  4096 2  12 23:04 scripts
drwxr-xr-x 28 mysql mysql  4096 2  12 23:04 share
drwxr-xr-x  4 mysql mysql  4096 2  12 23:05 sql-bench
drwxr-xr-x  2 mysql mysql  4096 2  12 23:04 support-files
[root@localhost ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@localhost ~]# echo 'export PATH=$PATH:/usr/local/mysql/bin/' >> /etc/profile
# 查看环境变量配置是否成功添加到/etc/profile文件中
[root@localhost ~]# tail -1 /etc/profile
export PATH=$PATH:/usr/local/mysql/bin/

1.2.6 配置my.cnf文件参数

把样例配置文件复制到/home/mysql/conf目录下,并设置好相关路径系统参数:socket、pid-file、datadir、tmpdir、log-error、slow_query_log_file、log-bin、relay-log、innodb_data_home_dir、innodb_log_group_home_dir、innodb_undo_directory。

[root@localhost ~]# cp -ar /usr/local/mysql/support-files/my-default.cnf /home/mysql/\conf/my.cnf
[root@localhost ~]# ln -s /home/mysql/conf/my.cnf  /etc/my.cnf
# my.cnf配置文件内容如下
[root@localhost ~]# cat /home/mysql/conf/my.cnf
[client]
socket=/home/mysql/data/mysqldata1/sock/mysql.sock # sock文件所在路径
[mysqld]
user=mysql
basedir = /usr/local/mysql
socket=/home/mysql/data/mysqldata1/sock/mysql.sock # sock文件所在路径
pid-file=/home/mysql/data/mysqldata1/sock/mysql.pid # pid文件所在路径
datadir=/home/mysql/data/mysqldata1/mydata # 数据文件路径
tmpdir=/home/mysql/data/mysqldata1/tmpdir # 存放临时文件的路径
log-error=/home/mysql/data/mysqldata1/log/error.log
slow_query_log
slow_query_log_file=/home/mysql/data/mysqldata1/slowlog/slow-query.log
log-bin=/home/mysql/data/mysqldata1/binlog/mysql-bin
relay-log=/home/mysql/data/mysqldata1/relaylog/mysql-relay-bin
innodb_data_home_dir = /home/mysql/data/mysqldata1/innodb_ts
innodb_log_group_home_dir = /home/mysql/data/mysqldata1/innodb_log
innodb_undo_directory = /home/mysql/data/mysqldata1/undo/

1.2.7 初始化MySQL

使用mysql_install_db命令初始化MySQL数据字典库、ibdata1、log_file*等文件。

[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# ./scripts/mysql_install_db --defaults-file=/home/ mysql/conf/my.\cnf--user=mysql
Installing MySQL system tables...2017-02-12 23:25:41 0[Warning]TIMESTAMP with implicit DEFAULT value is deprecated.Please use--explicit_defaults_for_timestamp server option(see documentation for more details).
2017-02-12 23:25:41 0 [Note] Ignoring --secure-file-priv value as server is running with--bootstrap.
2017-02-12 23:25:41 0 [Note] ./bin/mysqld(mysqld 5.6.35-log)starting as process 8297 ...
OK
Filling help tables...2017-02-12 23:25:44 0[Warning]TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option(see documentation for more details).
2017-02-12 23:25:44 0 [Note] Ignoring --secure-file-priv value as server is running with--bootstrap.
2017-02-12 23:25:44 0 [Note] ./bin/mysqld(mysqld 5.6.35-log)starting as process 8319 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
  /usr/local/mysql/bin/mysqladmin -u root password 'new-password'
  /usr/local/mysql/bin/mysqladmin   -u   root   -h   localhost.localdomain   password 'new-password'
... 此处省略后续部分输出
# 查看关键目录在初始化之后是否有正确的数据文件和目录、权限
[root@localhost mysql]# ll /home/mysql/data/mysqldata1/{mydata, innodb_log, innodb_ts}/
/home/mysql/data/mysqldata1/innodb_log/:
总用量 98304
-rw-rw----1 mysql mysql 50331648 2  12 23:25 ib_logfile0
-rw-rw----1 mysql mysql 50331648 2  12 23:25 ib_logfile1
/home/mysql/data/mysqldata1/innodb_ts/:
总用量 12288
-rw-rw----1 mysql mysql 12582912 2  12 23:25 ibdata1
/home/mysql/data/mysqldata1/mydata/:
总用量 12
drwx------2 mysql mysql 4096 2  12 23:25 mysql
drwx------2 mysql mysql 4096 2  12 23:25 performance_schema
drwx------2 mysql mysql 4096 2  12 23:25 test

提示:在MySQL 5.7版本中删除了mysql_install_db脚本,直接使用bin/mysqld命令进程初始化,初始化有两个选项。

● --initialize:使用该选项初始化时会在错误日志中写一个随机的root密码,初始化完成之后在错误日志中搜索password,紧跟其后的一串字符串就是这个随机密码(例如,“A temporary password is generated for root@localhost: XRER<:les9p? ”这段文字中的粗体字部分就是随机密码),初始化完成并启动mysqld之后,初次登录需要使用这个随机密码。

● --initialize-insecure:使用该选项初始化时不会产生随机密码,而是像MySQL 5.7之前的版本一样,初始化完成之后,第一次登录数据库使用空的root密码。

示例:

[root@localhost mysql]# mysqld --defaults-file=/etc/my.cnf --initialize
[root@localhost mysql]# mysqld --defaults-file=/etc/my.cnf --initialize-insecure

1.2.8 启动MySQL

将mysql.server文件复制到/etc/init.d/目录下,命名为mysqld程序,并使用这个脚本启动和停止MySQL。

[root@localhost mysql]# cp -ar /usr/local/mysql/support-files/mysql.server /etc/init.d/\mysqld
[root@localhost mysql]# chmod +x /etc/init.d/mysqld
# 查看/etc/init.d/mysqld是否被成功赋予执行权限
[root@localhost mysql]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 mysql mysql 10875 11 28 23:32 /etc/init.d/mysqld
[root@localhost mysql]# service mysqld start
Starting MySQL..                                         [确定]
# 查看进程和端口
[root@localhost mysql]# ps aux |grep mysqld
root     10475  0.0  0.0  11472  1384 pts/2    S    23:37   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/home/mysql/data/mysqldata1/mydata --pid-file=/home/mysql/data/mysqldata1/sock/mysql.pid
mysql   10743  0.0 24.21078428464964 pts/2  Sl  23:37  0:00 /usr/local/mysql/bin/mysqld--basedir=/usr/local/mysql --datadir=/home/ mysql/ data/mysqldata1/mydata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/home/mysql/data/mysqldata1/log/error.log --pid-file=/home/mysql/data/mysqldata1/sock/mysql.pid
--socket=/home/mysql/data/mysqldata1/sock/mysql.sock
root     10791  0.0  0.0103256   860 pts/2    S+   23:46   0:00 grep mysqld
[root@localhost mysql]# netstat -ntupl |grep mysqld
tcp        0      0 :::3306              :::*               LISTEN      10743/mysqld
# 查看错误日志
[root@localhost mysql]# vim /home/mysql/data/mysqldata1/log/error.log
# 注意:日志中不能出现ERROR错误,看到最后一行输出版本号和socket信息就表示MySQL启动成功
Version:  '5.6.35-log'   socket:  '/home/mysql/data/mysqldata1/sock/mysql.  sock'   port:3306  MySQL Community Server(GPL)