1、卸载系统自带mysql
[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb*
[root@localhost ~]# yum remove mariadb-libs-5.5.60-1.el7_5.x86_64
2、添加mysql禁止登录的用户及目录规划
2.1、 创建用户
useradd -M -s /sbin/nologin mysql
2.2、 创建目录
[root@db02 ~/mysql-5.7.37]# cd /usr/local
# 软件目录(为mysql安装目录创建软链接或改目录名)
[root@db02 /usr/local]# ln -s /usr/local/mysql-5.7.37 /usr/local/mysql
# 数据目录
[root@db02 /usr/local]# mkdir /usr/local/mysql-5.7.37/data
# 日志目录
[root@db02 /usr/local]# mkdir /var/log/mysql
[root@db02 /usr/local]# touch /var/log/mysql/mysqld.log
[root@db02 /usr/local]# chown -R mysql.mysql /var/log/mysql/mysqld.log
# 其他目录()
[root@db02 /usr/local]# mkdir /usr/local/mysql-5.7.37/tmp
[root@db02 /usr/local]# chown -R mysql.mysql /usr/local/mysql-5.7.37
3、下载、解压源码包
[root@db02 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.37.tar.gz
[root@db02 ~]# tar xf mysql-5.7.37.tar.gz
[root@db02 ~]# cd mysql-5.7.37/
4、下载boost软件
#安装boots 一组C++ 库的集合,提供了线性代数、伪随机数生成、多线程、图像处理、正则表达式、单元测试等功能。
[root@db02 ~/mysql-5.7.37]# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
[root@db02 ~/mysql-5.7.37]# tar xf boost_1_59_0.tar.gz -C /usr/local
5、安装依赖
[root@db02 ~/mysql-5.7.37]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel libstdc++* libtool lrzsz
6、进行cmake编译mysql源文件
cmake 通过编写cmakelist文件,自动生成makefile
#cmake一下
[root@db02 ~/mysql-5.7.37]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.37 \
-DMYSQL_DATADIR=/usr/local/mysql-5.7.37/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.7.37/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
7、编译并安装
# make可以用来批处理.c或.cpp文件的,它从makefile中读取相应指令,然后编译。
[root@db02 ~/mysql-5.7.37]# make
# make install将 make 生成的文件安装到系统的对应目录中,比如从github上下载下来一个依赖库的安装包,解压后经过上面几步,该依赖库的对应头文件和库文件就安装到系统中了。
[root@db02 ~/mysql-5.7.37]# make install
#这条命令来进行安装(当然有些软件需要先运行 make check 或 make test来进行一些测试),这一步一般需要你有 root 权限(因为要向系统写入文件)
8、初始化数据库(初始化mysql数据库(建立默认的库和表))
#初始化 注:1、初始化时,data目录必须为空,2、将随机生成的登录密码记录下来
[root@db02 /usr/local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2022-08-04T06:40:03.463503Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2022-08-04T06:40:03.632146Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-08-04T06:40:03.662409Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-08-04T06:40:03.672882Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 455d0f7e-13c0-11ed-b58d-000c29b71c46.
2022-08-04T06:40:03.674258Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2022-08-04T06:40:03.919464Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-08-04T06:40:03.919483Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-08-04T06:40:03.919903Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-04T06:40:04.007134Z 1 [Note] A temporary password is generated for root@localhost: 0fkhCeNYFb*E
9、编辑配置文件
[root@db02 /usr/local]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/tmp/mysql.sock
pid-file=/usr/local/mysql/tmp/mysqld.pid
port=3306
character-set-server=utf8mb4
log-error=/var/log/mysql/mysqld.log
[mysql]
character-set-server=utf8mb4
socket=/usr/local/mysql/tmp/mysql.sock
[client]
character-set-server=utf8mb4
socket=/usr/local/mysql/tmp/mysql.sock
10、测试连接
[root@db02 /usr/local]# /usr/local/mysql/bin/mysql -uroot -p'0fkhCeNYFb*E'
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 4
Server version: 5.7.37
Copyright (c) 2000, 2021, 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>
目录
8、初始化数据库(初始化mysql数据库(建立默认的库和表))
11、将mysqld服务添加到系统服务中
[root@db02 /usr/local]# cp mysql/support-files/mysql.server /etc/init.d/mysqld
[root@db02 /usr/local]# /etc/init.d/mysqld start
12、优化
12.1、加入systemd管理
[root@db02 /usr/local]# 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/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[root@db02 /usr/local]# systemctl daemon-reload
[root@db02 /usr/local]# systemctl start mysqld
12.2设置环境变量
[root@db02 /usr/local]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@db02 /usr/local]# source /etc/profile