CentOS 7 下编译安装 MySQL-5.7.21

系统环境

操作系统: CentOS Linux release 7.4.1708 (Core)
MySQL版本: MySQL-5.7.21

  1. 安装前的准备
  • 编译安装MySQL需要一些必备的组件,可以直接使用yum安装即可

    yum -y install cmake ncurses-devel gcc-c++
  • 创建 Mysql用户

    useradd mysql
  • 分别给mysql创建程序安装目录和数据存储目录,这里将mysql安装在/opt下,数据存储在 /data/mysql下

    mkdir /opt/mysql-5.7.21
    mkdir /data/mysql/{log,data} -p
  • 到官网下载MySQL的安装包并解压, 为了能使用mysql用户编译安装,下载和解压要使用mysql用户,否则解压完后还需要手动改变源码的属主权限,才能使mysql用户有权限执行编译安装

    CentOS 7 下编译安装 MySQL-5.7.21

    wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21.tar.gz
    tar xf mysql-5.7.21.tar.gz

2.开始编译安装

  • MySQL常见编译参数

    参数说明
    -DCMAKE_INSTALL_PREFIX=dir_name基础的文件夹,对应mysqld的--basedir参数
    -DINSTALL_BINDIR=dir_namebin目录位置
    -DINSTALL_DOCDIR=dir_name文档目录位置
    -DINSTALL_DOCREADMEDIR=dir_nameReadme文件位置
    -DINSTALL_INCLUDEDIR=dir_nameInclude目录位置
    -DINSTALL_LAYOUT=name布局选项,包括Standalone、RPM、SRV4、DEB
    -DMYSQL_DATADIR=dir_name数据存放目录
    -DSYSCONFDIR=dir_name默认配置my.cnf目录
  • 进入mysql目录,开始编译

    cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql-5.7.21 -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/opt/mysql-5.7.21/conf -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DWITH_SSL=yes -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/opt/mysql-5.7.21

    CentOS 7 下编译安装 MySQL-5.7.21

  • 编译成功,执行安装

    make install

3.安装后的工作

  • /opt/mysql-5.7.21目录下创建 conf目录,并将源码包中的mysql默认配制文件拷贝至其中

    mkdir /opt/mysql-5.7.21/conf/
    cp  packaging/rpm-common/my.cnf  /opt/mysql-5.7.21/conf/
  • 初始化mysql

    ./bin/mysqld --defaults-file=/opt/mysql-5.7.21/conf/my.cnf --datadir=/data/mysql --initialize-insecure --collation-server=utf8_general_ci --explicit_defaults_for_timestamp=true
    2018-02-12T06:48:25.794971Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2018-02-12T06:48:25.858793Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2018-02-12T06:48:25.915154Z 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: b9bda646-0fc0-11e8-b0a0-5254005cdf47.
    2018-02-12T06:48:25.916131Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2018-02-12T06:48:26.297824Z 0 [Warning] CA certificate ca.pem is self signed.
    2018-02-12T06:48:26.420848Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
  • 修改mysql配制并创建 mysql log目录

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    
    [mysqld]
    #
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    #
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    #
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M
    datadir=/data/mysql
    socket=/tmp/mysql.sock
    
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    log-error=/data/mysql/log/mysqld.log
    pid-file=/data/mysql/mysqld.pid
    mkdir /data/mysql/log
  • 添加mysql系统启动脚本

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

    报错:

    Starting MySQL. ERROR! The server quit without updating PID file (/data/mysql/mysqld.pid).

    查看错误日志

    tail /data/mysql/log/mysql
    
    ----------
    
    2018-02-12T06:58:48.615325Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2018-02-12T06:58:48.615416Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
    2018-02-12T06:58:48.615453Z 0 [Note] /opt/mysql-5.7.21/bin/mysqld (mysqld 5.7.21) starting as process 24900 ...
    2018-02-12T06:58:48.617334Z 0 [ERROR] COLLATION 'latin1_swedish_ci' is not valid for CHARACTER SET 'utf8'
    2018-02-12T06:58:48.617368Z 0 [ERROR] Aborting
    
    2018-02-12T06:58:48.617389Z 0 [Note] Binlog end
    2018-02-12T06:58:48.617574Z 0 [Note] /opt/mysql-5.7.21/bin/mysqld: Shutdown complete
    
    2018-02-12T06:58:48.625393Z mysqld_safe mysqld from pid file /data/mysql/mysqld.pid ended

    修改 mysql启动脚本,在启动参数中加上 --collation-server=utf8_general_ci 即可

    CentOS 7 下编译安装 MySQL-5.7.21

  • 将mysql添加进系统环境变量中

    vim /etc/profile.d/mysql.sh
    
    ----------
    export PATH=$PATH:/opt/mysql-5.7.21/bin
    
    
    ----------
    source /etc/profile.d/mysql.sh
  • 给mysql的root用户设置密码

    mysqladmin -u root password '123456'

相关推荐