CentOS 安装通用二进制 MySQL 5.7

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

下载二进制包

在官网如下地址下载最新版

http://dev.mysql.com/downloads/mysql/5.7.html#downloads

# cd /usr/local/src/
# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.x-linux-glibc2.5-x86_64.tar.gz

解压缩mysql

# cd ..
# tar zxvf mysql-5.7.x-linux-glibc2.5-x86_64.tar.gz
# mv mysql-5.7.x-linux-glibc2.5-x86_64 /usr/local/mysql

更改权限

# 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/mysql

启动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安全

# /usr/local/mysql/bin/mysql_secure_installation

按照提示配置安全信息

设置mysql开机启动

# chkconfig mysqld on

添加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/aexport 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错误日志和慢查询日志就可以每天自动轮转。