在CentOS 7上源码编译安装MySQL 5.7
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在CentOS 7上源码编译安装MySQL 5.7相关的知识,希望对你有一定的参考价值。
1.系统环境
[[email protected] ~]# uname -r
3.10.0-514.el7.x86_64
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[[email protected] ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
[[email protected] ~]# rpm -e --nodeps mariadb-libs [[email protected] ~]# [[email protected] ~]# yum install -y gcc gcc-c++ perl
2.安装依赖包
相关依赖包的作用
cmake:由于从 mysql5.5 版本开始弃用了常规的 configure 编译方法,所以需要 CMake 编译器,用于设置 mysql 的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等。
Boost:#从 MySQL 5.7.5 开始 Boost 库是必需的,mysql 源码中用到了 C++的 Boost 库,要求必须安装 boost1.59.0 或以上版本
GCC:是 Linux 下的 C 语言编译工具,mysql 源码编译完全由 C 和 C++编写,要求必须安装GCC
bison:Linux 下 C/C++语法分析器
ncurses:字符终端处理库
1) 安装文件准备
下载 cmake-3.8.1.tar.gz 源码发布包
http://www.cmake.org/download/
wget https://cmake.org/files/v3.8/cmake-3.8.1.tar.gz
下载 ncurses-6.0.tar.gz
ftp://ftp.gnu.org/gnu/ncurses/
wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-6.0.tar.gz
下载 m4-1.4.18.tar.gz
wget http://mirrors.kernel.org/gnu/m4/m4-1.4.18.tar.gz
下载 bison-3.0.4.tar.gz
http://ftp.gnu.org/gnu/bison/
wget http://ftp.gnu.org/gnu/bison/bison-3.0.4.tar.gz
下载 mysql-5.7.18.tar.gz
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18.tar.gz
下载 Boost_1_59_0.tar.gz
wget https://ncu.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
2)安装 CMAKE 及必要的软件
安装 cmake
[[email protected] ~]# tar zxvf cmake-3.8.1.tar.gz [[email protected] ~]# cd cmake-3.8.1 [[email protected] cmake-3.8.1]# ./bootstrap [[email protected] cmake-3.8.1]# gmake && gmake install [[email protected] cmake-3.8.1]# cmake -version [[email protected] cmake-3.8.1]# cd ~
安装m4
[[email protected] ~]# tar zxvf m4-1.4.18.tar.gz [[email protected] ~]# cd m4-1.4.18 [[email protected] m4-1.4.18]# ./configure [[email protected] m4-1.4.18]# make && make install [[email protected] m4-1.4.18]# cd ~
安装 bison(注:需要perl和m4支持)
[[email protected] ~]# tar zxvf bison-3.0.4.tar.gz [[email protected] ~]# cd bison-3.0.4 [[email protected] bison-3.0.4]# ./configure [[email protected] bison-3.0.4]# make && make install [[email protected] bison-3.0.4]# cd ~
安装 ncurses
[[email protected] ~]# tar zxvf ncurses-6.0.tar.gz [[email protected] ~]# cd ncurses-6.0 [[email protected] ncurses-6.0]# ./configure [[email protected] ncurses-6.0]# make && make install [[email protected] ncurses-6.0]# cd ~
安装 boost(注:MySQL 5.7.18需要的boost正确版本为boost_1_59_0)
[[email protected] ~]# tar zxvf boost_1_59_0.tar.gz [[email protected] ~]# mv boost_1_59_0 /usr/local/boost
3.创建mysql用户、用户组及目录
[[email protected] ~]# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql [[email protected] ~]# mkdir /usr/local/mysql [[email protected] ~]# mkdir -p /data/mysql
4.预编译
[[email protected] ~]# tar zxvf mysql-5.7.18.tar.gz [[email protected] ~]# cd mysql-5.7.18 [[email protected] mysql-5.7.18]#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/data/mysql \ -DWITH_BOOST=/usr/local/boost \ -DSYSCONFDIR=/etc \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DENABLE_DTRACE=0 \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_general_ci \ -DWITH_EMBEDDED_SERVER=OFF
5.编译安装
[[email protected] mysql-5.7.18]# make -j `grep processor /proc/cpuinfo | wc -l` [[email protected] mysql-5.7.18]# make install
-j 参数表示根据 CPU 核数指定编译时的线程数,可以加快编译速度。默认为 1 个线程编译。
6.设置权限并初始化 MySQL 系统授权表
[[email protected] mysql-5.7.18]# cd /usr/local/mysql [[email protected] mysql]# chown -R mysql:mysql . [[email protected] mysql]# mkdir temp [[email protected] mysql]# chmod 777 ./temp [[email protected] mysql]# cd /data/mysql [[email protected] mysql]# chown -R mysql:mysql .
MySQL 5.7.6之前的版本执行这个脚本初始化系统数据库
./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
5.7.6之后版本初始系统数据库脚本
[[email protected] mysql]# cd /usr/local/mysql [[email protected] mysql]# ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
注意:
MySQL之前版本mysql_install_db是在mysql_basedir/script下,MySQL 5.7直接放在了mysql_basedir/bin目录下。
"--initialize"已废弃,生成一个随机密码(~/.mysql_secret)
"--initialize-insecure"不会生成密码
"--datadir"目录下不能有数据文件
[[email protected] mysql]# ./bin/mysql_ssl_rsa_setup
7.创建配置文件
[[email protected] mysql]# cat > /etc/my.cnf << EOF
[client]
port=3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
skip-external-locking
skip-name-resolve
user=mysql
port=3306
basedir=/usr/local/mysql
datadir=/data/mysql
tmpdir=/usr/local/mysql/temp
# server_id = .....
socket=/usr/local/mysql/mysql.sock
log-error=/usr/local/mysql/logs/mysql_error.log
pid-file=/usr/local/mysql/mysql.pid
open_files_limit=10240
back_log=600
max_connections=500
max_connect_errors=6000
wait_timeout=605800
#open_tables=600
#table_cache = 650
#opened_tables = 630
max_allowed_packet=32M
sort_buffer_size=4M
join_buffer_size=4M
thread_cache_size=300
query_cache_type=1
query_cache_size=256M
query_cache_limit=2M
query_cache_min_res_unit=16k
tmp_table_size=256M
max_heap_table_size=256M
key_buffer_size=256M
read_buffer_size=1M
read_rnd_buffer_size=16M
bulk_insert_buffer_size=64M
lower_case_table_names=1
default-storage-engine=INNODB
innodb_buffer_pool_size=2G
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_flush_method=O_DIRECT
#####################
thread_concurrency=32
long_query_time=2
slow-query-log=on
slow-query-log-file=/usr/local/mysql/logs/mysql-slow.log
[mysqldump]
quick
max_allowed_packet=32M
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF
8.配置MySQL服务
[[email protected] mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [[email protected] mysql]# chkconfig --add mysqld [[email protected] mysql]# chkconfig mysqld on
9.启动服务
[[email protected] mysql]# service mysqld start [[email protected] mysql]# netstat -anpt | grep mysqld
10. 配置MySQL环境变量
[[email protected] mysql]# echo -e ‘\n\nexport PATH=/usr/local/mysql/bin:$PATH\n‘ >> /etc/profile && source /etc/profile
11.设置数据库root用户密码
[[email protected] mysql]# ./bin/mysql_secure_installation
12.创建其他MySQL数据库用户
[[email protected] mysql]# mysql -uroot -p mysql> CREATE DATABASE `testdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> grant all privileges on testdb.* to ‘ryanxu‘@‘%‘ identified by ‘[email protected]#‘; mysql> flush privileges; myqsl> exit
错误汇总:
1.如果中途编译失败了,需要删除cmake生成的预编译配置参数的缓存文件和make编译后生成的文件,再重新编译。
[[email protected] mysql-5.7.18]# make clean [[email protected] mysql-5.7.18]# rm -f CMakeCache.txt
2.如果报错
make[2]: *** [libmysqld/examples/mysql_client_test_embedded] 错误 1
make[1]: *** [libmysqld/examples/CMakeFiles/mysql_client_test_embedded.dir/all] 错误 2
加上
-DWITH_EMBEDDED_SERVER=OFF
本文出自 “SQL Server Deep Dive” 博客,请务必保留此出处http://ultrasql.blog.51cto.com/9591438/1924467
以上是关于在CentOS 7上源码编译安装MySQL 5.7的主要内容,如果未能解决你的问题,请参考以下文章