1、环境介绍
- centos7.9
- mysql5.7.44
- boost1.59.0
注意:这里的编译版本mysql5.7.44和boost1.59.0是有依赖的,建议使用相同版本
2、安装编译要用的依赖软件
yum install -y gcc gcc-c++ cmake libaio-devel ncurses ncurses-devel bison openssl-devel
3、准备编译的源码包
mysql源码包下载:
boost源码包下载:
- boost官网:Boost Downloads
- boost官网下载历史版本:Boost Version History
- boost文档:Boost C++ Libraries
- boost-1.59.0:Chapter 43. Boost.Build User Manual - 1.59.0
注意:国内下载这个boost包很慢
#可直接linux上用wget下载
wget https://zenlayer.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.44.tar.gz
4、创建mysql用户
groupadd mysql
useradd -r -g mysql -M -s /sbin/nologin mysql
5、创建数据库目录(此目录可自定义)
mkdir -p /opt/mysql
chown mysql:mysql /opt/mysql
#目录自定义后,后面配置文件路径相应要改变
6、编译安装mysql和boost
#将本地下载的2个源码包传至/root
cd /root/
tar -xf boost_1_59_0.tar.gz
mv boost_1_59_0 /usr/local/boost
tar -xf mysql-5.7.44.tar.gz -C /usr/local/src/
cd /usr/local/src/mysql-5.7.44
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/opt/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/opt/mysql/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDOWNLOAD_BOOST=0 -DWITH_BOOST=../../boost -DWITH_INNODB_MEMCACHED=ON
make
make install
7、编辑my.cnf并初始化mysql数据库
#先备份编译安装后自动生成的
mv /etc/my.cnf /etc/my.cnf.bak
#自定义my.cnf
cat >/etc/my.cnf<<EOF
[client]
port=3306
socket=/opt/mysql/mysql.sock
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
skip-name-resolve
user=mysql
port=3306
basedir=/usr/local/mysql
datadir=/opt/mysql
tmpdir=/tmp
socket=/opt/mysql/mysql.sock
log-error=/opt/mysql/mysqld.log
pid-file=/opt/mysql/mysqld.pid
EOF
#根据my.cnf初始化mysql
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql
#开启数据库并查看状态
/usr/local/mysql/support-files/mysql.server start
/usr/local/mysql/support-files/mysql.server status
#查看登录root的初始密码
cat /opt/mysql/mysqld.log | grep root
8、设置mysql环境变量
vi /etc/profile
...
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
source /etc/profile
9、修改root的初始密码
#这里用前面查到的初始密码登录
mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
mysql> alter user root@localhost identified by 'ziu1988Q...'
-> ;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#关闭数据库服务(为了后面设置的自启正常使用)
/usr/local/mysql/support-files/mysql.server stop
10、设置启动文件以及开机自启
#复制启动文件到常用启动目录
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/
#编辑启动service
vi /usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Community Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PermissionsStartOnly=true
PIDFile=/opt/mysql/mysqld.pid
ExecStartPre=/etc/init.d/mysql.server restart
ExecStart=/etc/init.d/mysql.server start
TimeoutSec=600
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
#重载配置和启动mysql,设置开机自启
systemctl daemon-reload
systemctl start mysql
systemctl enable mysql