# Mysql 5.7.20 源码安装笔记
标签: Linux Mysql
**1、环境说明:**
OS: CentOS 6.9 x86-64
Mysql: mysql-5.7.20.tar.gz
cmake: cmake-3.7.2.tar.gz
**2、OS环境准备**
yum 配置
使用网易源
cd /etc/yum.repo.d/
mkdir backup
mv *.repo ./backup #备份系统默认yum源
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
运行以下命令生成缓存
yum clean all
yum makecache
软件包安装
yum groupinstall Development tools
yum install –y ncurses ncurses-devel
selinux配置
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
getenforce #检查配置是否正确
**3、软件准备:**
a、安装约定
软件包存放位置: /usr/local/src
软件安装位置: /usr/local/
b、软件安装包下载
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.20.tar.gz -P /usr/local/src/
wget https://cmake.org/files/v3.9/cmake-3.9.6.tar.gz -P /usr/local/src
wget https://ufpr.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz -P /usr/local/src
**4、安装**
a、cmake安装
cd /usr/local/src
tar xf cmake-3.9.6.tar.gz
cd cmake-3.9.6
./configure
make
make install
b、mysql安装
1)、创建mysql用户:
groupadd mysql
useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
2)、创建mysql的数据目录
mkdir /mydata/mysql
chown -R mysql:mysql /mydata
3)、安装
cd /usr/local/src
tar zxvf mysql-5.7.20.tar.gz
mkdir /usr/local/boost
cp /usr/local/src/boost_1_59_0.tar.gz /usr/local/boost/ #只需要把boost包放进去就可以,不需要解压
cd /usr/local/src/mysql-5.7.20
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/mysql -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -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_EMBEDDED_SERVER=OFF -DWITH_BOOST=/usr/local/boost/
如遇报错,根据错误信息,安装对应包。
yum install xxxxx
rm CMakeCache.txt # 需要删除编译缓存文件
重新编译。
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
MYSQL_USER
-- Build files have been written to: /usr/local/src/mysql-5.7.20
编译成功。
make
make install
4)、编译选项说明
指定安装文件的安装路径时常用的选项
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql ----指定残可安装路径(默认的就是/usr/local/mysql)
-DMYSQL_DATADIR=/data/mysql ----mysql的数据文件路径
-DSYSCONFDIR=/etc ----配置文件路径
编译过程中启用其他存储引擎时指令介绍
-DWITH_INNOBASE_STORAGE_ENGINE=1 ----使用INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 ----常应用于日志记录和聚合分析,不支持索引
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 ----黑洞存储引擎
编译过程中取消一些存储引擎指令介绍
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
示例如下:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
编译进过程中功能启用的指令介绍
-DWITH_READLINE=1 ----支持批量导入mysql数据
-DWITH_SSL=system ----mysql支持ssl会话,实现基于ssl的数据复
-DWITH_ZLIB=system ----压缩库
-DWITH_LIBWRAP=0 ----是否可以基于WRAP实现访问控制
其他功能指令
-DMYSQL_TCP_PORT=3306 ----默认端口
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock ----默认套接字文件路径
-DENABLED_LOCAL_INFILE=1 ----是否启用LOCAL_INFILE功能
-DEXTRA_CHARSETS=all ----是否支持额外的字符集
-DDEFAULT_CHARSET=utf8 ----默认编码机制
-DDEFAULT_COLLATION=utf8_general_ci ----设定默认语言的排序规则
-DWITH_DEBUG=0 ----DEBUG功能设置
-DENABLE_PROFILING=1 ----性能分析功能是否启用
5)、mysql服务脚本
进入安装目录:
[root@name mysql-5.7.20]# cd /usr/local/src/mysql-5.7.20
[root@name mysql-5.7.20]# cp support-files/mysql.server /etc/init.d/mysqld #从源码包中复制脚本
[root@name mysql-5.7.20]# chmod +x /etc/init.d/mysqld
[root@name mysql-5.7.20]# chkconfig --add mysqld
[root@name mysql-5.7.20]# chkconfig mysqld on
vim /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql #检查MySQL程序安装路径
datadir=/mydata/mysql #检查MySQl数据库存放目录
:wq! #保存退出
6)、初始化mysql
进入MySQL安装目录
[root@name bin]# cd /usr/local/mysql/bin/
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/mysql/
#生成mysql系统数据库
报错:
2018-01-27T23:27:56.890018Z 0 [Warning] option 'table_open_cache': unsigned value 256901120 adjusted to 524288
2018-01-27T23:27:56.890633Z 0 [Warning] Could not increase number of max_open_files to more than 1024 (request: 1049086)
2018-01-27T23:27:56.890639Z 0 [Warning] Changed limits: max_connections: 214 (requested 500)
2018-01-27T23:27:56.890642Z 0 [Warning] Changed limits: table_open_cache: 400 (requested 524288)
2018-01-27T23:27:56.890874Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-01-27T23:27:56.893143Z 0 [ERROR] You have enabled the binary log, but you haven't provided the mandatory server-id. Please refer to the proper server start-up parameters documentation
2018-01-27T23:27:56.893171Z 0 [ERROR] Aborting
#server-id错误,在mysql配置文件中【mysqld】增加server-id参数:
vim /etc/my.cnf
[mysqld]
# GENERAL #
server-id = 1
进入数据文件目录,查看文件,若有文件生成,说明初始化成功。
[root@mysql57 mysql]# cd /mydata/mysql
[root@mysql57 mysql]# ls
auto.cnf ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema sys
7)、编辑配置文件:
[root@name db_data]# rm -rf /etc/my.cnf #删除系统默认的配置文件(如果默认没有就不用删除)
[root@mysql mysql]# vim my.cnf #来源mysql安装文档
推荐一个mysql配置文件生成工具,供如我这般小白使用:
https://tools.percona.com/wizard
基于上述配置的my.cnf
# Generated by Percona Configuration Wizard (http://tools.percona.com/)
[mysql]
# CLIENT #
port = 3306
socket = /mydata/mysql/mysql.sock
[mysqld]
# GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /mydata/mysql/mysql.sock
pid-file = /mydata/mysql/mysql.pid
# MyISAM #
key-buffer-size = 32M
# SAFETY #
max-allowed-packet = 16M
max-connect-errors = 10000
# DATA STORAGE #
datadir = /mydata/mysql/
# BINARY LOGGING #
log-bin = /mydata/mysql/mysql-bin
expire-logs-days = 14
sync-binlog = 1
# CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type = 0
query-cache-size = 0
max-connections = 500
thread-cache-size = 50
open-files-limit = 65535
table-definition-cache = 4096
table-open-cache = 245M
# INNODB #
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 6G
# LOGGING #
log-error = /mydata/mysql/mysql-error.log
log-queries-not-using-indexes = 1
slow-query-log = 1
slow-query-log-file = /mydata/mysql/mysql-slow.log
8)、环境变量配置
vim /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/mysql/bin
:wq! #保存退出
source /etc/profile #使配置立刻生效
9)、输出mysql的man手册到man命令的查找路径
vim /etc/man.config
#新增一行
MANPATH /usr/local/mysql/man
10)、输出mysql的库文件
vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
ldconfig -v
11)、输出mysql的头文件到系统头文件
ln -sv /usr/local/mysql/include/ /usr/include/mysql
"/usr/include/mysql" -> "/usr/local/mysql/include/"
cd /usr/include/mysql/
ls
big_endian.h m_string.h my_global.h mysql_embed.h plugin_validate_password.h
byte_order_generic.h my_alloc.h my_list.h mysql.h sql_common.h
byte_order_generic_x86_64.h my_attribute.h my_net.h mysql_time.h sql_state.h
byte_order_generic_x86.h my_byteorder.h my_pthread.h mysql_version.h sslopt-case.h
decimal.h my_compiler.h mysql my_sys.h sslopt-longopts.h
errmsg.h my_config.h mysql_com.h my_xml.h sslopt-vars.h
keycache.h my_dbug.h mysql_com_server.h plugin_audit.h typelib.h
little_endian.h my_dir.h mysqld_ername.h plugin_ftparser.h
m_ctype.h my_getopt.h mysqld_error.h plugin.h
12)、启动mysql
[root@mysql57 mysql]# service mysqld start
Starting MySQL... SUCCESS!
13)、测试
查看端口
[root@mysql mysql]# netstat -ntulp | grep :3306
tcp 0 0 :::3306 :::* LISTEN 41347/mysqld
登录mysql
[root@mysql57 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20-log Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql_secure_installation #设置Mysql密码,根据提示按Y 回车输入2次密码
14)、开放远程访问:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password123'; #配置远程访问权限、密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'Password2#$'; #配置本地访问权限、密码
mysql> FLUSH PRIVILEGES;