MySQL5.7的几种安装方式总结(排错踩坑呕心沥血的经历)

2023-12-26 06:35:23

包安装

添加国内源:mysql | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

注意:5.7安装之后有一个临时密码,进行登录并修改新密码后才可以对mysql进行操作。

可以yun list看看各个系统光盘自带的都是什么版本,或者什么分支(mysql/mariadb)。

centos7.9自带的是mariadb

[root@ctos79 mysql 09:04:29]#yum list | grep mariadb
mariadb-libs.x86_64                      1:5.5.68-1.el7                @anaconda
mariadb.x86_64                           1:5.5.68-1.el7                BaseOS
mariadb-bench.x86_64                     1:5.5.68-1.el7                BaseOS
mariadb-devel.i686                       1:5.5.68-1.el7                BaseOS
mariadb-devel.x86_64                     1:5.5.68-1.el7                BaseOS
mariadb-embedded.i686                    1:5.5.68-1.el7                BaseOS
mariadb-embedded.x86_64                  1:5.5.68-1.el7                BaseOS
mariadb-embedded-devel.i686              1:5.5.68-1.el7                BaseOS
mariadb-embedded-devel.x86_64            1:5.5.68-1.el7                BaseOS
mariadb-libs.i686                        1:5.5.68-1.el7                BaseOS
mariadb-server.x86_64                    1:5.5.68-1.el7                BaseOS
mariadb-test.x86_64                      1:5.5.68-1.el7                BaseOS

二进制文件安装MySQL 5.7

安装依赖包

yum -y install libaio numactl-libs ncurses-compat-libs

添加用户和组

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

下载包并安装

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz

tar xf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local

ln -s mysql-5.7.44-linux-glibc2.12-x86_64 mysql
chown -R root.root /usr/local/mysql/

添加环境变量

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

修改配置文件

cp /etc/my.cnf{,.bak}
cat > /etc/my.cnf <<EOF
[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
EOF

创建数据文件路径

mkdir -pv /data/mysql

生成临时密码

# 随机密码
mysqld --initialize --user=mysql --datadir=/data/mysql
awk '/temporary password/{print $NF}' /data/mysql/mysql.log
vp-jEZe,K4lg

# 空密码
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

准备服务脚本和启动

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

修改密码

# 修改root密码
mysqladmin -uroot -p'vp-jEZe,K4lg' password 123456

# 如果前面为空密码
mysqladmin -uroot password 123456

最后一步,登录测试

[root@ctos79 mysql 03:56:50]#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 4
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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>

源码安装(centos7)

编译 make -j 8 多线程编译

5.6还是需要执行安全加固的。

安装依赖包

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

添加用户及数据目录

useradd -r -s /sbin/nologin -d /data/mysql mysql

mkdir /data/mysql -p
chown mysql.mysql /data/mysql

下载源码

yum install -y unzip
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.44.zip

unzip mysql-5.7.44.zip
mv mysql-5.7.44 /usr/local/src/
cd /usr/local/src/mysql-5.7.44/

编译

# 编译前需要升级boost库,否则会报错
wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
tar -xf boost_1_59_0.tar.gz
cd boost_1_59_0
./bootstrap.sh --prefix=/usr/local
./b2 install --with=all

---这里安装实测执行了10分钟---

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

make -j 8 && make install
---开8线程的编译安装时间实测是6分钟---

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

设置环境变量

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

生成数据库文件

cd /apps/mysql/
~~# bin/mysql_install_db --datadir=/data/mysql/ --user=mysql~~
bin/mysqld --initialize --user=mysql --datadir=/data/mysql

# 如果执行报错,说明数据库文件目录不为空
mv /data/mysql/* /tmp

记住生成的临时密码:r#x?yudwb3lX

准备配置文件

cp /etc/my.cnf{,.bak}
cat > /etc/my.cnf <<EOF
[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
EOF

修改启动脚本并启动服务

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

修改密码

mysqladmin -uroot -p'r#x?yudwb3lX' password 123456

访问测试

root@ctos79 mysql 09:00:13]#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 6
Server version: 5.7.44 Source distribution

Copyright (c) 2000, 2023, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

最后

也可以使用docker进行安装,拉镜像就好了。

文章来源:https://blog.csdn.net/sinat_36050376/article/details/135211825
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。