通过编译、二进制安装MySQL5.7

编译安装

1、源码包下载,官网下载地址:https://downloads.mysql.com/archives/community/,点击下载,还有boost_1_59_0.tar.gz这个源码包也要提前下载好,然后将源码包上传到Linux系统

[root@centos7 ~]#ll
total 131916
-rw-r--r-- 1 root root 51363998 Jan 27 21:58 mysql-boost-5.7.30.tar.gz
-rw-r--r-- 1 root root 83709983 Jan 27 21:58 boost_1_59_0.tar.gz

2、安装相关依赖包

[root@centos7 ~]#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 perl-Data-Dumper

3、创建用户和数据目录

[root@centos7 ~]#useradd -r -s /sbin/nologin -d /data/mysql mysql
[root@centos7 ~]#id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)

4、创建数据库目录,修改权限

[root@centos7 ~]#mkdir /data/mysql -pv
mkdir: created directory ‘/data/mysql’
[root@centos7 ~]#chown mysql.mysql /data/mysql/
[root@centos7 ~]#ll -d /data/mysql/
drwxr-xr-x 2 mysql mysql 6 Jan 27 20:42 /data/mysql/

5、解压缩源码包

[root@centos7 ~]#tar xvf mysql-boost-5.7.30.tar.gz -C /usr/local/src
[root@centos7 ~]#tar xvf boost_1_59_0.tar.gz -C /usr/local/src

6、源码编译安装 MySQL

[root@centos7 ~]#cd /usr/local/src/mysql-5.7.30/
[root@centos7 mysql-5.7.30]#pwd
/usr/local/src/mysql-5.7.30
[root@centos7 mysql-5.7.30]#cmake . \
> -DCMAKE_INSTALL_PREFIX=/apps/mysql \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc/ \
> -DMYSQL_USER=mysql \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
> -DWITH_DEBUG=0 \
> -DWITH_READLINE=1 \
> -DWITH_SSL=system \
> -DWITH_ZLIB=system \
> -DWITH_LIBWRAP=0 \
> -DENABLED_LOCAL_INFILE=1 \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DDOWNLOAD_BOOST=1 \
> -DWITH_BOOST=/usr/local/src/boost_1_59_0

[root@centos7 mysql-5.7.30]#make && make install

提示:如果出错,执行rm -f CMakeCache.txt
 

7、准备环境变量

[root@centos7 mysql-5.7.30]#echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7 mysql-5.7.30]#. /etc/profile.d/mysql.sh

8、修改配置文件

[root@centos7 mysql-5.7.30]#cd /apps/mysql/
[root@centos7 mysql]#pwd
/apps/mysql

[root@centos7 mysql]#vim /etc/my.cnf
[mysqld]
explicit_defaults_for_timestamp=true
datadir=/data/mysql/
basedir=/apps/mysql/bin/mysql/
port=3306
pid-file=/data/mysql/mysql.pid
socket=/data/mysql/mysql.socket
symbolic-links=0
character_set_server=utf8
user=mysql

[mysqld_safe]
log-error=/data/mysql/mysql.log

[client]
port=3306
socket=/data/mysql/mysql.socket
default-character-set=utf8

9、初始化数据库


[root@centos7 mysql]#bin/mysqld --initialize-insecure --datadir=/data/mysql/ --user=mysql

10、准备启动脚本,并启动服务

[root@centos7 mysql]#cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@centos7 mysql]#chkconfig --add mysqld
[root@centos7 mysql]#systemctl start mysqld
[root@centos7 mysql]#ss -ntl
State      Recv-Q Send-Q              Local Address:Port                             Peer Address:Port
LISTEN     0      128                             *:22                                          *:*
LISTEN     0      100                     127.0.0.1:25                                          *:*
LISTEN     0      128                          [::]:22                                       [::]:*
LISTEN     0      100                         [::1]:25                                       [::]:*
LISTEN     0      80                           [::]:3306                                     [::]:*

11、修改密码,设置的密码一定要记好(大写字母、小写字母、数字加标点符号,密码要定期更换)

[root@centos7 mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 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> status
--------------
mysql  Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using  EditLine wrapper

Connection id:		2
Current database:
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.30 Source distribution
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/data/mysql/mysql.socket
Uptime:			15 min 17 sec

Threads: 1  Questions: 5  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.005
--------------

mysql> alter user root@'localhost' identified by 'MySQL@2022.';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

二进制安装

1、安装相关包

yum -y install libaio numactl-libs

2、创建用户和组

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

3、准备程序文件
 

wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linuxglibc2.12-x86_64.tar.gz
tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local/
ln -s mysql-5.7.31-linux-glibc2.12-x86_64/ mysql
chown -R root.root /usr/local/mysql/

4、准备环境变量

echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh

5、准备配置文件

cp /etc/my.cnf{,.bak}
vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock

6、初始化数据库文件并提取root密码,生成 root 空密码
 

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

7、准备服务脚本和启动

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start

8、修改口令

#修改前面生成的空密码为指定密码(密码要定期修改)

mysqladmin -uroot password MySQL@2022.

9、测试登录
 

mysql -uroot -pMySQL@2022.

猜你喜欢

转载自blog.csdn.net/weixin_51867896/article/details/122722795#comments_20499187