CentOS 编译安装 MySQL 5.7

MySQL 编译配置参数说明请查看官网
http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html

安装约定

mysql安装路径:/usr/local/mysql
mysql数据库路径:/data/mysql
my配置文件路径:/etc/my.cnf

创建mysql用户和组

# groupadd -g 27 mysql
# useradd -u 27 -g mysql -c "MySQL Server" -d /var/lib/mysql -s /sbin/nologin mysql

安装编译所需要的工具和库

# yum -y install gcc gcc-c++ make cmake autoconf automake
# yum -y install ncurses-devel bison

下载源代码包

在官网如下地址下载最新版
http://dev.mysql.com/downloads/mysql/5.7.html#downloads
http://download.savannah.gnu.org/releases/libunwind/
http://code.google.com/p/gperftools/downloads/list

# cd /usr/local/src/
# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
# wget http://gperftools.googlecode.com/files/gperftools-2.1.tar.gz
# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.x.tar.gz

安装libunwind

# tar zxvf libunwind-1.1.tar.gz
# cd libunwind-1.1
# ./configure
# make
# make install

安装gperftools

# cd ..
# tar zxvf gperftools-2.1.tar.gz
# cd gperftools-2.1
# ./configure
# make
# make install
# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
# ldconfig

安装mysql

# cd ..
# tar zxvf mysql-5.7.x.tar.gz
# cd mysql-5.7.x

编译前可用如下命令查看编译参数说明

# cmake . -LH | more

如果中间过程有报错,请删除CMakeCache.txt,在重新编译

# rm -rf CMakeCache.txt

下面进行编译安装

# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 -DMYSQL_DATADIR=/data/mysql \
 -DSYSCONFDIR=/etc \
 -DEXTRA_CHARSETS=all \
 -DDEFAULT_CHARSET=utf8 \
 -DDEFAULT_COLLATION=utf8_general_ci \
 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
 -DENABLED_LOCAL_INFILE=1 \
 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
 -DMYSQL_TCP_PORT=3306 \
 -DMYSQL_USER=mysql \
 -DWITH_DEBUG=0
# make
# make install

更改权限

# chmod 755 /var/lib/mysql
# cd /usr/local/mysql
# chown -R mysql:mysql .

初始化mysql

# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql

2016-06-26T05:40:22.355444Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2016-06-26T05:40:24.429548Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-06-26T05:40:24.733103Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-06-26T05:40:24.850793Z 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: 7b09a546-3b60-11e6-9531-00163e000d26.
2016-06-26T05:40:24.856413Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2016-06-26T05:40:24.861099Z 1 [Note] A temporary password is generated for root@localhost: KL0bgn3n7o13

初始化的时候,会生成一个临时密码(上面红色字体12位密码),这个要记住,是首次登陆的临时密码,否则你会登陆不上mysql

创建mysql配置文件

# cp support-files/my-default.cnf  /etc/my.cnf

配置mysql服务

# cp support-files/mysql.server  /etc/init.d/mysqld

启动mysql

# service mysqld start

初始化root密码

第一次登陆需要修改临时密码,否则报如下错误:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

设置登陆新密码

mysql> set PASSWORD='123456'

设置mysql开机启动

# chkconfig mysqld on

开启tcmalloc提高mysql在高并发下的性能

# vim /usr/local/mysql/bin/mysqld_safe

在# executing mysqld_safe在添加如下代码:

export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

或者用以下命令添加

sed -i '/# executing mysqld_safe/a\export LD_PRELOAD=/usr/local/lib/libtcmalloc.so' /usr/local/mysql/bin/mysqld_safe

然后重启mysql,通过命令查看lsof -n | grep tcmalloc,如果有如下输出,说明已经加载成功

mysqld 14017 mysql mem REG 8,3 2171746 51382767 /usr/local/lib/libtcmalloc.so.4.1.2

添加MySQL 库文件搜索路径,防止依赖MySQL的第三方软件找不到库文件

# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig

设置mysql环境变量,方便直接使用mysql命令

# vim /etc/profile

在其文件末尾添加如下变量

export PATH=$PATH:/usr/local/mysql/bin

或者用以下命令添加

# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/mysql/bin' /etc/profile

运行如下命令使环境变量生效

# source /etc/profile

配置日志轮转

在/root目录新建.my.cnf文件,里面内容为mysql数据库的管理账号,如下:

[client]
user=root
password=123456
socket=/tmp/mysql.sock

修改my.cnf中错误日志和慢查询日志路径为/var/log/mysql

# mkdir -p /var/log/mysql
# chown -R mysql:mysql /var/log/mysql

将如下内容粘贴到mysql中

/var/log/mysql/*log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    create 644 mysql mysql
    postrotate
        MYCNF_FILE=/root/.my.cnf
        MYSQLADMIN=/usr/local/mysql/bin/mysqladmin
        if test -x $MYSQLADMIN && $MYSQLADMIN --defaults-file="$MYCNF_FILE" ping >/dev/null
        then
            $MYSQLADMIN --defaults-file="$MYCNF_FILE" flush-logs
        fi
    endscript
}

然后将该文件移动到/etc/logrotate.d目录下

重启crond定时计划任务

# service crond restart

这样子mysql错误日志和慢查询日志就可以每天自动轮转。