跳转至

MySQL5.7.20二进制包安装

1. 下载二进制包

# https://downloads.mysql.com/archives/community/   
# 按下面提示输入参数,然后点击下载
#1. Product Version:5.7.20
#2. Operating System:linux-generic
#3. OS Version:64-bit
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

img.png

2. 解压缩二进制包

2.1 将二进制包上传到Linux的/usr/local目录

[root@k8s-node1 ~]# ls /usr/local
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz  sbin  share  src

2.2 解压缩二进制包

[root@k8s-node1 ~]# cd /usr/local/
[root@k8s-node1 local]# tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@k8s-node1 local]# ls
bin  games    lib    libexec                              mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz  share
etc  include  lib64  mysql-5.7.20-linux-glibc2.12-x86_64  sbin                                        src

2.3 将解压缩后的目录改名

[root@k8s-node1 local]# mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql
[root@k8s-node1 local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz  sbin  share  src

2.4 创建mysql用户,更改权限

[root@k8s-node1 local]# useradd mysql
[root@k8s-node1 local]# chown -R mysql:mysql /usr/local/mysql
疑问:是不是应该创建不允许登录的用户?
useradd mysql -s /sbin/nologin -M

3. 创建数据目录

mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql

4. 初始化数据

/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

得到密码

[root@k8s-node1 bin]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2021-04-23T01:14:18.556892Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-04-23T01:14:18.872704Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-04-23T01:14:18.931125Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-04-23T01:14:18.992194Z 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: 3a80765c-a3d1-11eb-bb00-000c29a0a205.
2021-04-23T01:14:18.993379Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-04-23T01:14:18.996600Z 1 [Note] A temporary password is generated for root@localhost: 3b%G<+#)mtfk
#初始化数据可能出现的报错,因为缺少依赖包!!!

[root@151 application]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
#报错了。。。
#安装依赖环境
[root@151 application]# yum install -y libaio
#再次初始化,成功!!!
[root@151 application]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
2021-05-13T06:16:50.793005Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-13T06:16:51.561551Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-13T06:16:51.759672Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-13T06:16:51.832643Z 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: ceb31800-b3b2-11eb-8aab-000c299fbc6f.
2021-05-13T06:16:51.834690Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-13T06:16:51.843074Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

5. 编写配置文件

vim /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log_error=/var/log/mysql.log

[client]
socket=/tmp/mysql.sock

6. 启动

[root@k8s-node1 bin]# ./mysqld_safe &
[1] 78665
[root@k8s-node1 bin]# 2021-04-23T01:21:33.554572Z mysqld_safe Logging to '/var/log/mysql.log'.
2021-04-23T01:21:33.583723Z mysqld_safe Starting mysqld daemon with databases from /data/mysql

[root@k8s-node1 bin]# netstat -tunlp |grep 330
tcp6       0      0 :::3306                 :::*                    LISTEN      78780/mysqld

7. 连接测试

[root@k8s-node1 bin]# ./mysql -uroot -p'3b%G<+#)mtfk'
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 3
Server version: 5.7.20

Copyright (c) 2000, 2017, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>

7.1 修改密码

mysql> set password='123';
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

7.2 使用新密码再次登录测试

mysql> exit
Bye
[root@k8s-node1 bin]# ./mysql -uroot -p123
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.20 MySQL Community Server (GPL)

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

8. 复制快捷管理mysql服务脚本到/etc/init.d/

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

stop测试

[root@k8s-node1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL..2021-04-23T01:27:13.643796Z mysqld_safe mysqld from pid file /data/mysql/k8s-node1.pid ended
 SUCCESS!
[1]+  Done                    ./mysqld_safe  (wd: /usr/local/mysql/bin)
(wd now: ~)

[root@k8s-node1 ~]# netstat -tunlp|grep 330
[root@k8s-node1 ~]#

start测试

[root@k8s-node1 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@k8s-node1 ~]# netstat -tunlp|grep 330
tcp6       0      0 :::3306                 :::*                    LISTEN      85524/mysqld

restart测试

[root@k8s-node1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@k8s-node1 ~]# netstat -tunlp|grep 330
tcp6       0      0 :::3306                 :::*                    LISTEN      86218/mysqld
#问题:要使用快捷命令,命令必须要在: /usr/local/mysql/bin    目录下!!!

[root@151 application]# ls mysql/support-files/
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@151 application]#
[root@151 application]#
[root@151 application]#
[root@151 application]# ls /etc/init.d/
functions  netconsole  network  README
[root@151 application]#
[root@151 application]# cp /application/mysql/support-files/mysqld_multi.server  /etc/init.d/mysqld
[root@151 application]#
[root@151 application]# ls /etc/init.d/
functions  mysqld  netconsole  network  README
[root@151 application]#
[root@151 application]# /etc/init.d/mysqld stop
Can't execute /usr/local/mysql/bin/mysqld_multi from dir /usr/local/mysql
[root@151 application]#
[root@151 application]#

8. 可以添加环境变量(略)

echo "export /usr/local/mysql/bin:$PATH" >>/etc/profile
source /etc/profile

测试

echo $PATH
which mysql

最后更新: 2022-02-20 11:21:58