源码包安装MySQL 5.7

    技术2022-07-10  114

     

    源码包官网下载地址:https://downloads.mysql.com/archives/community/

    mysql5.7 编译安装需要boost 库,这里官网下载含boost的源码包

    可根据需要下载不同的版本

     

    也可从国内镜像下载,地址:http://mirrors.163.com/mysql/Downloads/MySQL-5.7/

    解压源码包,并移动到/usr/local/src目录下

     tar -zxf mysql-boost-5.7.28.tar.gz

     mv mysql-5.7.28 ../mysql

    创建mysql用户

    groupadd mysql

    useradd -r -g mysql -s /bin/false -d /usr/local/mysql

    useradd -r:创建系统用户

    修改mysql目录的所有者

    chown -R mysql.mysql mysql

    创建mysql数据存放目录,并修改所有者

    mkdir -p /data/mysql

    chown -R mysql.mysql /data/mysql

    安装mysql依赖

    yum install -y  gcc gcc-c++ cmake ncurses ncurses-devel bison zlib zlib-devel openssl openssl-devel pcre pcre-devel 

    进入mysql安装目录,安装mysql

    cd /usr/local/mysql

    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_BOOST=boost -DWITH_SSL=system

    参数信息:

    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #安装目录

    -DMYSQL_DATADIR=/data/mysql \                 #数据存放目录

    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \     #Unix socket 文件路径

    -DDEFAULT_CHARSET=utf8mb4 \                     #使用 utf8mb4字符集

    -DDEFAULT_COLLATION=utf8mb4_general_ci \ #校验规则

    -DWITH_BOOST=boost                      #关联boost

                            

    编译安装

    make && make install

    编译过程中报错,内存不足

     

    增加虚拟内存swap,建议设置为2G

    dd if=/dev/zero of=/swapfile bs=64M count=32 #count的大小就是增加的swap空间的大小,64M是块大小,所以空间大小是bs*count=2048MB mkswap /swapfile #把刚才空间格式化成swap格式 swapon /swapfile #使用刚才创建的swap空间

    使用完之后再删除就行了

    swapoff /swapfile rm /swapfile

     

    重新编译make && make install

    编译完成

    添加mysql配置文件

    vim /etc/my.cnf

    [mysqld]

    datadir=/data/mysql

    basedir=/usr/local/mysql

    socket=/tmp/mysql.sock

    user=mysql

    port=3306

    character-set-server=utf8mb4

    # Disabling symbolic-links is recommended to prevent assorted security risks

    symbolic-links=0

    log_error=/data/mysql/mysql.err

    server_id=1

    log_bin=/data/mysql/mysql-bin

    sync_binlog=1

    binlog_format=row

    [mysqld_safe]

    pid-file=/data/mysql/mysqld.pid

    配置环境变量

    vim /etc/profile

    export MYSQL_HOME=/usr/local/mysql

    export PATH=$PATH:$ZOOKEEPER_HOME/bin:${JAVA_HOME}/bin:$MYSQL_HOME/bin

    source /etc/profile

    初始化mysql

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

    启动并连接mysql

    cd /usr/local/mysql/support-files

    cp mysql.server /etc/init.d/mysqld

    [root@third support-files]# /etc/init.d/mysqld start

    Starting MySQL.. SUCCESS!

    [root@third support-files]# mysql

    Welcome to the MySQL monitor.  Commands end with ; or \g.

    Your MySQL connection id is 2

    Server version: 5.7.28-log Source distribution

     

    Copyright (c) 2000, 2019, 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>flush privileges;  #刷新权限表

    mysql> set password=password('Wlni60207!');  #修改当前用户即root用户密码

    使用新密码重新登录

    mysql> show databases;

    +--------------------+

    | Database           |

    +--------------------+

    | information_schema |

    | mysql              |

    | performance_schema |

    | sys                |

    +--------------------+

    4 rows in set (0.00 sec)

     

    mysql>

    Processed: 0.018, SQL: 9