Centos7下源码编译安装mysql
📕作者:喜欢水星记
🏆系列:源码编译
🔋收藏:本文记录我搭建过程供大家学习和自己之后复习,如果对您有用,希望能点赞收藏加关注
运行环境
操作系统:Centos7.9
MySQL版本:5.7.31
IP:192.168.180.135
基础准备
将本次所需要的软件上传至/opt目录下
[软件包下载链接](链接:https://pan.baidu.com/s/1nIUn32dBaKimvh8Jnq_mHA
提取码:5ke6)
创建mysql用户
useradd -s /sbin/nologin mysql
安装源码编译所需要的依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel
解压上传的mysql软件包
tar -xf mysql-boost-5.7.32.tar.gz
编译安装
开始编译
cd mysql-5.7.32/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_BOOST=boost/boost_1_59_0
# 参数说明 (不需要输入进去)
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
#指定mysql的安装路径
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
#指定mysql进程监听套接字文件(数据库连接文件)的存储路径
-DSYSCONFDIR=/etc \
#指定配置文件的存储路径
-DSYSTEMD_PID_DIR=/usr/local/mysql \
#指定进程文件的存储路径
-DDEFAULT_CHARSET=utf8 \
#指定默认使用的字符集编码,如utf8
-DDEFAULT_COLLATION=utf8_general_ci \
#指定默认使用的字符集校对规则
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
#安装INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
#安装ARCHIVE存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
#安装BLACKHOLE存储引擎
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
#安装FEDERATED存储引擎
-DMYSQL_DATADIR=/usr/local/mysql/data \
#指定数据库文件的存储路径
-DWITH_BOOST=boost \
#指定boost的路径,
-DWITH_SYSTEMD=1
#生成便于systemctl管理的文件
安装
make -j4 && make install
make clean
注意:安装的过程可能会有5-10分钟左右,根据电脑性能而定
安装过程没有报错,则说明安装成功
配置
修改相关目录的权限
chown -R mysql:mysql /usr/local/mysql/
修改配置文件
vi /etc/my.cnf
将该文件的原本内容全部删除,添加如下内容:
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[mysqld_safe]
log-error=/var/log/mariadb.log
pid-file=/var/run/mariadb.pid
[client]
default-character-set=utf8mb4
[mysql]
default-character-set = utf8mb4
!includedir /etc/my.cnf.d
初始化数据库
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/ --pid-file=/data/mysql/mysql.pid
报错解决方案:
mkdir -p /data/mysql/
再次执行:
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/ --pid-file=/data/mysql/mysql.pid
注意: 上面命令输出的结果会得到一个mysql登录的临时密码,需要记住,后面登录会用到
给相关目录修改权限
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
ldconfig
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
配置环境变量
echo “PATH=$PATH:/usr/local/mysql/bin” > /etc/profile.d/mysql.sh
source /etc/profile
chkconfig mysqld on
service mysqld start
设置数据库的启动状态
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld
修改数据库密码
创建一个软链接:
ln -s /usr/local/mysql/bin/mysqladmin /usr/bin
用之前生成的临时密码 2UstY(sh!r)k
登录数据库:
mysql -uroot -p’2UstY(sh!r)k’
**修改root密码为123456 **:
set password=password(‘123456’);
mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit;
Bye
使用刚刚修改好的新密码登录mysql控制台
mysql -uroot -p123456
[root@LNMP bin]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.32 Source distribution
Copyright (c) 2000, 2020, 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> quit;
Bye
至此mysql的源码编译就已经完成了
ved.
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> quit;
Bye
`至此mysql的源码编译就已经完成了`