MySQL的安装

MySql安装 (Version5.7.36,基于Debian11)

1.下载、解压


2.创建mysql用户及用户组

1
2
[root@localhost]# groupadd mysql
[root@localhost]# useradd -g mysql mysql

3.创建/opt/mysql/data、/opt/mysql/log目录,修改mysql目录下所有文件归属于mysql用户及用户组

1
2
3
[root@localhost]# mkdir /opt/mysql/data
[root@localhost]# mkdir /opt/mysql/log
[root@localhost]# chown -R mysql:mysql mysql

4.准备配置文件

  • 4.1 /etc目录新建my.cnf文件,写入以下内容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [mysql]
    # 设置mysql客户端默认字符集
    default-character-set=utf8mb4
    socket=/var/lib/mysql/mysql.socket

    [mysqld]
    skip-name-resolve
    #设置3306端口
    port = 3306
    socket=/var/lib/mysql/mysql.socket
    # 设置mysql的安装目录
    basedir=/opt/mysql
    # 设置mysql数据库的数据的存放目录
    datadir=/opt/mysql/data
    # 允许最大连接数
    max_connections=200
    # 服务端使用的字符集默认为8比特编码的latin1字符集
    character-set-server=utf8mb4
    # 创建新表时将使用的默认存储引擎
    default-storage-engine=INNODB
    lower_case_table_names=1
    max_allowed_packet=16M
    log-error=/opt/mysql/log/error.log
  • 4.2 创建/var/lib/mysql目录,设置权限
    1
    2
    [root@localhost]# mkdir /var/lib/mysql
    [root@localhost]# chmod 755 /var/lib/mysql

5.开始安装MySql

  • 5.1 安装依赖
    1
    [root@localhost]# apt install libaio1 libncurses5 libtinfo5
  • 5.2 安装
    运行完成无报错后,默认的root密码在/opt/mysql/log/error.log文件内
    1
    2
    [root@localhost]# cd /opt/mysql/bin
    [root@localhost]# ./mysqld --initialize --user=mysql

6.利用Systemd实现开机自启动

  • 6.1 创建/usr/lib/systemd/system/mysqld.service文件
  • 6.2 /usr/lib/systemd/system/mysqld.service文件写入以下内容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [Unit]
    Description=MySQL
    Documentation=https://dev.mysql.com/doc/refman/5.7/en

    [Service]
    ExecStart=/usr/bin/bash /opt/mysql/bin/mysqld_safe --user=mysql
    KillMode=mixed
    TimeoutStopSec=1s
    Restart=always

    [Install]
    WantedBy=multi-user.target
  • 6.3 设置开机自启动并启动mysql
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@localhost]# systemctl enable mysqld.service
    Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /lib/systemd/system/mysqld.service. //出现这句话即设置成功
    [root@localhost]# systemctl start mysqld.service //启动mysql
    [root@localhost]# systemctl status mysqld.service //查看启动情况,Active: active (running)即成功运行
    ● mysqld.service - MySQL
    Loaded: loaded (/lib/systemd/system/mysqld.service; enabled; vendor preset: enabled)
    Active: active (running) since Wed 2021-11-03 18:43:06 CST; 1 months 2 days ago
    Docs: https://dev.mysql.com/doc/refman/5.7/en
    Main PID: 494 (bash)
    Tasks: 35 (limit: 2341)
    Memory: 232.5M
    CPU: 14min 20.836s
    CGroup: /system.slice/mysqld.service
    ├─494 /bin/bash /opt/mysql/bin/mysqld_safe --user=debian
    └─762 /opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/opt/mysql/data

7. 总结

  • 这次安装实际上还是靠别人的总结,我主要还是按照CodeSheepB站Up主的视频和他制作的《编程环境和软件工具安装手册》pdf文件的步骤来进行的,由于他是在CentOS安装的,我在Debian11安装时还是有一些问题,我做了一些小小的改动,最后成功运行还是很激动的
  • pdf文件得去Up主公众号CodeSheep里回复”安装手册”获取