MySQL的编译安装以及初始化操作(内附一键安装脚本)

    科技2024-11-08  7

    编译安装

    1.下载源码包

    官网:https://downloads.mysql.com/archives/community/

    2.上传下载的源码包mysql-community-5.7.29-1.el7.src.rpm到linux家目录

    3.新建用户mysql

    useradd -s /sbin/nologin mysql

    4.解决软件的依赖关系

    yum install cmake ncurses-devel gcc gcc-c++ vim lsof bzip2 openssl-devel -y

    5.安装源码的rpm包,得到真正的源代码

    rpm -ivh mysql-community-5.7.29-1.el7.src.rpm

    6.进入/root/rpmbuild目录

    cd /root/rpmbuild/SOURCES

    7.解压真正的源码包

    tar xf mysql-5.7.29.tar.gz
    tar xf boost_1_59_0.tar.bz2

    8.将boost包移动到mysql解压的包里

    mv boost_1_59_0 mysql-5.7.29

    9.切换到mysql-5.7.29目录下

    cd mysql-5.7.29

    10.编译前的配置

    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/sc_mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost_1_59_0

    11.编译和编译安装

    make -j 2 && make install

    初始化操作

    1.关闭防火墙

    service firewalld stop
    systemctl disable firewalld

    2.关闭selinux

    setenforce 0 (临时关闭selinux)
    sed -i ‘s/enforcing/disabled/’ /etc/selinux/config (永久关闭selinux)

    3.初始化操作

    mkdir /data/mysql -p
    chown mysql:mysql /data/mysql/
    chmod 750 /data/mysql/
    cd /usr/local/sc_mysql/bin
    ./mysqld --initialize --user=mysql --basedir=/usr/local/sc_mysql/ --datadir=/data/mysql &>passwd.txt

    4.获得临时密码

    tem_passwd=$(cat passwd.txt |grep “temporary”|awk ‘{print $NF}’)

    5.修改环境变量,添加我们编译安装的MySQL的可执行命令的路径

    echo “PATH=$PATH:/usr/local/sc_mysql/bin” >>/root/.bashrc
    PATH=$PATH:/usr/local/sc_mysql/bin

    6.复制mysql提供的启动mysqld服务的脚本到/etc/init.d目录下–>复制mysqld的启动脚本

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

    7.生成/etc/my.cnf配置文件

    cat >/etc/my.cnf <<EOF
    [mysqld_safe]
    [client]
    socket=/data/mysql/mysql.sock
    [mysqld]
    socket=/data/mysql/mysql.sock
    port = 3306
    open_files_limit = 8192
    innodb_buffer_pool_size = 512M
    [mysql]
    auto-rehash
    prompt=\u@\d \R:\m mysql>
    EOF

    8.启动mysqld服务

    service mysqld start

    9.初次修改密码需要使用–connect-expired-password 选项,登录重新设置初始密码为Sanchuang123#

    mysql -uroot -p$tem_passwd --connect-expired-password -e “set password=‘Sanchuang123#’;”

    10.验证密码是否设置成功

    mysql -uroot -pSanchuang123# -e ‘show databases;’ && echo “database is installed success”

    一键安装脚本

    链接:https://pan.baidu.com/s/1MSMo5Iq0G1jjI_bc0Mn0IQ 提取码:20h9 复制这段内容后打开百度网盘手机App,操作更方便哦

    Processed: 0.015, SQL: 8