在CentOS系统中,通过源码编译安装MySQL数据库是一种常见的方式,尽管相对复杂,但它能够提供更高的定制性和灵活性,下面将详细介绍如何在CentOS 7和CentOS 8上通过源码编译安装MySQL 8.0的步骤:
基本环境准备
1、更新系统并安装必要工具
CentOS 7:
```bash
sudo yum update y
sudo yum install y wget cmake gcc gccc++ ncurses ncursesdevel libaiodevel openssl openssldevel bison
```
CentOS 8:
```bash
sudo dnf update y
sudo dnf install y wget cmake gcc gccc++ ncurses ncursesdevel libaiodevel openssl openssldevel bison
```
2、下载MySQL源码包
CentOS 7:
```bash
wget HTTPS://cdn.mysql.com//Downloads/MySQL8.0/mysql8.0.29.tar.gz
tar xzvf mysql8.0.29.tar.gz
cd mysql8.0.29
```
CentOS 8:
```bash
wget https://dev.mysql.com//get//Downloads//MySQL8.0//mysql8.0.23.tar.gz
tar xzvf mysql8.0.23.tar.gz
cd mysql8.0.23
```
3、创建用户和目录
```bash
groupadd mysql
useradd r g mysql s /bin/false mysql
mkdir p /usr/local/mysql
mkdir /data/mysql
chown R mysql:mysql /usr/local/mysql
chown R mysql:mysql /data/mysql
chmod R 755 /data/mysql
chmod R 755 /usr/local/mysql
```
编译安装MySQL
1、配置编译选项
通用配置:
```bash
cmake . DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
DMYSQL_DATADIR=/data/mysql \
DSYSCONFDIR=/etc \
DMYSQL_TCP_PORT=3306 \
DWITH_BOOST=~/path/to/boost \
DDEFAULT_CHARSET=utf8 \
DDEFAULT_COLLATION=utf8_general_ci \
DENABLED_LOCAL_INFILE=ON \
DWITH_INNODB_MEMCACHED=ON \
DWITH_INNOBASE_STORAGE_ENGINE=1 \
DWITH_FEDERATED_STORAGE_ENGINE=1 \
DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
DWITH_ARCHIVE_STORAGE_ENGINE=1 \
DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
DWITH_PERFSCHEMA_STORAGE_ENGINE=1
```
特定需求配置:根据具体需求调整编译选项,例如启用或禁用某些存储引擎等。
2、执行编译和安装
```bash
make j $(nproc)
sudo make install
```
初始化数据库和启动服务
1、初始化数据库
```bash
sudo /usr/local/mysql/bin/mysqld initialize user=mysql
```
2、启动MySQL服务
```bash
sudo /usr/local/mysql/bin/mysqld_safe user=mysql &
```
3、创建systemd服务文件(可选)
```bash
sudo cat > /etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld_safe
Restart=always
RestartSec=10
[Install]
WantedBy=multiuser.target
EOF
```
然后启用并启动服务:
```bash
sudo systemctl enable mysqld
sudo systemctl start mysqld
```
FAQs
1、如何设置root密码?
登录MySQL:使用以下命令登录MySQL。
```bash
sudo /usr/local/mysql/bin/mysql u root p
```
设置密码:在MySQL命令行中输入以下命令。
```sql
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
EXIT;
```
2、如何检查MySQL服务状态?
```bash
sudo systemctl status mysqld
```
3、如何停止MySQL服务?
```bash
sudo systemctl stop mysqld
```
通过上述步骤,您应该能够在CentOS系统上成功编译并安装MySQL 8.0,请确保在实际操作过程中备份重要数据,并根据实际情况调整编译选项以满足业务需求。