三种方式部署MySQL
1、yum方式部署MySQL
1.1 清除残留
[root@mysql01]# yum remove mariadb* -y
1.2 下载yum源
系统自带yum仓库安装的是maraidb,因此安装MySQL需要另外添加yum仓库:
[root@mysql01]# wget https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
1.3 安装yum源
[root@mysql01]# yum install mysql80-community-release-el7-5.noarch.rpm -y
1.4 查看仓库
[root@mysql01]# yum repolist enabled | grep mysql
1.5 安装yum管理工具包
[root@mysql01]# yum install -y yum-utils
1.6 更换为5.7版本的yum源
[root@mysql01]# yum-config-manager --disable mysql80-community
[root@mysql01]# yum-config-manager --enable mysql57-community
1.7 安装MySQL
[root@mysql01]# yum install -y mysql-community-server
1.8 启动MySQL
[root@mysql01]# systemctl start mysqld
1.9 获取默认密码
[root@mysql01]# cat /var/log/mysqld.log | grep password
1.10 登录数据库
[root@mysql01]# mysql -uroot -p'AoQ6hiL-hRRZ'
1.11 修改数据库默认密码
alter user user() identified by "Test123!";
1.12 退出系统重新登录
exit
[root@mysql01]# mysql -uroot -p'Test123!'
2、二进制方式部署MySQL
2.1 清除残留
[root@mysql02 ~]# yum remove mariadb* -y
2.2 下载安装包
前往mysql官网下载二进制安装包,https://dev.mysql.com/downloads/mysql/5.7.html #downloads
# 注意:选择操作系统时选Linux-Generic,版本选择linux-generic ( glibc 2.12 ) (x86,64-bit)
不注册也不登录,直接点下载。然后把下载好的压缩包上传到mysql02的/root目录下。
2.3 安装
[root@mysql02 ~]# tar -xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql02 ~]# ln -s /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql-5.7.37 # 给个软链接,目录名字太长了
2.4 统一用户
[root@mysql02 mysql-5.7.37]# groupadd mysql -g 666
[root@mysql02 mysql-5.7.37]# useradd mysql -g 666 -u 666 -r -M -s /sbin/nologin
2.5 创建配置文件
[root@mysql02 mysql-5.7.37]# vim /etc/my.cnf
[mysqld]
# 安装目录
basedir=/usr/local/mysql-5.7.37
# 存放数据的目录
datadir=/usr/local/mysql-5.7.37/data
# 指定端口号
port=3306
# 指定Socket文件存放路径
socket=/usr/local/mysql-5.7.37/data/mysql.sock
# 指定默认的字符集编码
character-set-server=utf8
# MySQL错误日志路径
log-error=/var/log/mysqld.log
# 指定MySQL pid文件路径
pid-file=/usr/local/mysql-5.7.37/data/mysqld.pid
[mysql]
socket=/usr/local/mysql-5.7.37/data/mysql.sock
[client]
socket=/usr/local/mysql-5.7.37/data/mysql.sock
2.6 创建数据目录和日志文件
[root@mysql02 run]# mkdir /usr/local/mysql-5.7.37/data
[root@mysql02 ~]# touch /var/log/mysqld.log
2.7 目录授权
[root@mysql02 ~]# chown mysql.mysql -R /usr/local/mysql-5.7.37
[root@mysql02 ~]# chown mysql.mysql -R /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64/
[root@mysql02 ~]# chown -R mysql.mysql /var/log/mysqld.log
2.8 安装MySQL依赖软件
[root@mysql02 ~]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ numactl libaio glibc cmake autoconf
2.9 初始化数据库
[root@mysql02 mysql-5.7.37]# cd /usr/local/mysql-5.7.37/
[root@mysql02 mysql-5.7.37]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.37 --datadir=/usr/local/mysql-5.7.37/data
2.10 注册MySQL启动服务
[root@mysql02 ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql-5.7.37/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
2.11 测试连接
[root@mysql02 ~]# systemctl daemon-reload
[root@mysql02 ~]# systemctl start mysqld
[root@mysql02 data]# cat /var/log/mysqld.log | grep password
2022-02-25T10:57:13.801643Z 1 [Note] A temporary password is generated for root@mysql03: fjrP<Tas.1iD
[root@mysql02 data]# /usr/local/mysql-5.7.37/bin/mysql -uroot -p'5AvsHgfN*B>t'
2.12 修改默认密码
alter user root@mysql03 identified by 'Test123!';
exit
2.13 添加环境变量
# 添加环境变量之后就可以直接用简单的命令登录了
[root@mysql02 data]# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql-5.7.37
export PATH=$PATH:$MYSQL_HOME/bin
[root@mysql02 data]# source /etc/profile
[root@mysql02 mysql-5.7.37]# mysql -uroot -pTest123! # 添加环境变量后可以用该方式登录
3、源码的方式部署MySQL
3.1 下载MySQL数据库源代码包
数据库官网下载地址 https://downloads.mysql.com/archives/community/
选择需要的版本下载。
3.2 上传并解压包
[root@mysql03 ~]# tar -xf mysql-5.7.36.tar.gz
3.3 统一用户
[root@mysql03 ~]# groupadd mysql -g 666
[root@mysql03 ~]# useradd mysql -g 666 -u 666 -r -M -s /sbin/nologin
3.4 配置文件
[root@mysql03 ~]# vim /etc/my.cnf
[mysqld]
# 安装目录
basedir=/usr/local/mysql-5.7.36
# 存放数据的目录
datadir=/usr/local/mysql-5.7.36/data
# 指定端口号
port=3306
# 指定Socket文件存放路径
socket=/usr/local/mysql-5.7.36/data/mysql.sock
# 指定默认的字符集编码
character-set-server=utf8
# MySQL错误日志路径
log-error=/var/log/mysqld.log
# 指定MySQL pid文件路径
pid-file=/usr/local/mysql-5.7.36/data/mysqld.pid
[mysql]
socket=/usr/local/mysql-5.7.36/data/mysql.sock
[client]
socket=/usr/local/mysql-5.7.36/data/mysql.sock
3.5 安装依赖软件
[root@mysql03 ~]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel
3.6 安装拓展功能包Boot
[root@mysql03 ~]# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz --no-check-certificate
[root@mysql03 ~]# tar -xf boost_1_59_0.tar.gz -C /usr/local/
3.7 创建数据目录
[root@mysql03 ~]# mkdir /usr/local/mysql-5.7.36
[root@mysql03 ~]# chown mysql.mysql -R /usr/local/mysql-5.7.36
3.8 设置编译参数
[root@mysql03 mysql-5.7.36]# pwd
/root/mysql-5.7.36
[root@mysql03 mysql-5.7.36]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.36 \
-DMYSQL_DATADIR=/usr/local/mysql-5.7.36/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.7.36/tmp/mysql.sock \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0
# 编译
[root@mysql03 mysql-5.7.34]# make
# 安装
[root@mysql03 mysql-5.7.34]# make install
# 创建数据目录和日志文件
[root@mysql03 ~]# mkdir /usr/local/mysql-5.7.36/data
[root@mysql03 mysql-5.7.36]# touch /var/log/mysqld.log
# 数据目录授权
[root@mysql03 mysql-5.7.36]# chown mysql.mysql -R /usr/local/mysql-5.7.36
[root@mysql03 mysql-5.7.36]# chown mysql.mysql /var/log/mysqld.log
3.9 初始化数据库
[root@mysql03 mysql-5.7.36]# cd /usr/local/mysql-5.7.36/
[root@mysql03 mysql-5.7.36]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.36/ --datadir=/usr/local/mysql-5.7.36/data/
3.10 注册并启动MySQL服务
[root@mysql03 mysql-5.7.36]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql-5.7.36/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
# 重载配置并启动服务
[root@mysql03 mysql-5.7.36]# systemctl daemon-reload
[root@mysql03 mysql-5.7.36]# systemctl start mysqld
3.11 连接并修改密码
[root@mysql03 mysql-5.7.36]# vim /var/log/mysqld.log | grep password # 获取默认密码
[root@mysql03 mysql-5.7.36]# /usr/local/mysql-5.7.36/bin/mysql -uroot -p'8ljB7S<eBioT'
# 修改密码
alter user user() identified by "Test123!";
exit
3.12 添加环境变量
[root@mysql03 data]# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql-5.7.36
export PATH=$PATH:$MYSQL_HOME/bin
[root@mysql03 data]# source /etc/profile
[root@mysql03 mysql-5.7.36]# mysql -uroot -pTest123! # 使用新密码登录