四时宝库

程序员的知识宝库

MySQL 5.7 的安装,你想知道的都在这里了

本文先以 MySQL 5.7.36 为例,现在最常用的是 MySQL 5.7,本文安装环境是 debian

1. 首先在官网下载对应版本,此步骤我相信大家都会,就不再赘述

2. 创建 mysql 组和 mysql 用户

```shell
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
```

3. 将下载的 MySQL 压缩文件,拷贝到 `/usr/local/`, 并解压,创建软连接

```shell
shell> cp mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz /usr/local
shell> tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
shell> ln -s mysql-5.7.36-linux-glibc2.12-x86_64 mysql
```

4. 创建 mysql-files 并修改其属主和属组

```shell
shell> cd mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files
```

5. 创建 MySQL 数据目录,创建 MySQL 全局配置文件

```shell
# 创建数据文件
shell> cd /opt
shell> mkdir -p mysql_data/mysql57
# 创建配置文件
shell> vim my.cnf
# 内容如下
[mysqld]
port = 3306
user = mysql
datadir = /opt/mysql_data/mysql57
log_error = error.log
```

6. 初始化数据,并修改 MySQL 安装目录的属组为 root

```shell
shell> bin/mysqld --initialize --user=mysql # --initialize-insecure 无密码
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
shell> apt install libaio1
shell> mysqld --initialize --user=mysql # 再次执行
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
shell> apt install numactl
shell> mysqld --initialize --user=mysql # 再次执行成功
shell> bin/mysql_ssl_rsa_setup
shell> chown -R root .
```

7. 启动 MySQL

```shell
shell> cp support-files/mysql.server /etc/init.d/mysqld
shell> service mysqld start # 启动 mysql 服务,或者: systemctl start mysqld
shell> service mysqld status # 查看 mysql 状态,或者: systemctl status mysqld
shell> service mysqld stop # 停止 mysql 服务,或者: systemctl stop mysqld
```

8. 登录 MySQL

```shell
shell> export PATH=/usr/local/mysql/bin:$PATH #配置环境变量
shell> mysql -uroot -p # 回车,报错如下
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
# 解决报错
shell> apt install libncurses5
shell> mysql -uroot -p # 再次登录,在 /opt/mysql_data/mysql57/error.log 查找密码,如下:
2022-03-03T03:33:49.812373Z 1 [Note] A temporary password is generated for root@localhost: 4ojuihu(OhBw
```

9. 修改密码

```shell
shell> mysql -uroot -p # 登录
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.36 MySQL Community Server (GPL)
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> set password = '123'; # 修改密码
```

至此,MySQL 5.7 版本安装完成了,下一篇说一下 MySQL 8.0 的安装 ,敬请期待......

大家加油!!!

下面是广告时间,关注 “老刘说技术” 获取更多教程

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接