找回密码
 立即注册
首页 业界区 安全 Linux系统搭建单机MySQL8.0.26版本

Linux系统搭建单机MySQL8.0.26版本

都硎唷 2025-6-11 19:47:38
概述

本文主要是写Ubuntu22.04搭建MySQL8.0.26版本
环境信息

[tr]IP系统规格[/tr]
10.0.0.10Ubuntu22.042c4g
数据库服务安装步骤

下载前置依赖

  1. # 下载libtinfo5、libnuma1依赖
  2. [root@lb ~]# apt update -y && apt install -y libtinfo5 libnuma1
复制代码
服务下载

方式一:进入官网下载,并上传到宿主机中,适合离线环境

官网下载地址: https://downloads.mysql.com/archives/community/
1.png

方式二:直接在宿主机中使用wget进行下载,适合宿主机联网环境

  1. wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
复制代码
服务解压并创建软链接

  1. # 服务解压至家目录
  2. [root@lb ~]# tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz

  3. # 创建软链接
  4. [root@lb ~]# ln -s mysql-8.0.26-linux-glibc2.12-x86_64 mysql

  5. # 查看
  6. [root@lb ~]# ll | grep mysql
  7. lrwxrwxrwx  1 root root        35 May  1 15:43 mysql -> mysql-8.0.26-linux-glibc2.12-x86_64/
  8. drwxr-xr-x  9 root root      4096 May  1 15:43 mysql-8.0.26-linux-glibc2.12-x86_64/
  9. -rw-r--r--  1 root root 914806904 Jul  2  2021 mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
复制代码
配置环境变量

  1. # /root/mysql/bin根据你安装的实际地址来进行替换
  2. [root@lb ~]# echo 'export PATH=/root/mysql/bin:$PATH' >> /etc/profile
  3. [root@lb ~]# source /etc/profile
复制代码
检查环境变量是否配置正确

  1. [root@lb ~]# mysql -V
  2. mysql  Ver 8.0.26 for Linux on x86_64 (MySQL Community Server - GPL)
复制代码
创建MySQL的虚拟用户

  1. [root@lb ~]# useradd -s /sbin/nologin -M mysql
复制代码
创建数据存储目录,并修改目录的拥有者

  1. [root@lb ~]# mkdir -p /data/mysql/data
  2. [root@lb ~]# chown -R mysql.mysql /data/mysql/data
复制代码
初始化数据库

  1. # 初始化数据库,没有报错即代表成功
  2. [root@lb ~]# mysqld --initialize-insecure  --user=mysql  --datadir=/data/mysql/data  --basedir=/root/mysql
  3. 2025-05-01T07:59:04.638615Z 0 [System] [MY-013169] [Server] /root/mysql-8.0.26-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 132486
  4. 2025-05-01T07:59:04.656989Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  5. 2025-05-01T07:59:05.254538Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  6. 2025-05-01T07:59:05.950015Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
  7. 2025-05-01T07:59:05.950635Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
  8. 2025-05-01T07:59:06.009584Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
复制代码
参数解释:
  • --initialize-insecure:不安全的初始化,表示数据库启动后没有密码信息,
  • --initialize:安全初始化,表示数据库启动后,会有默认的密码信息
  • --user:指定用户
  • --datadir:指定数据存储目录
  • --basedir:指定MySQL安装的目录
数据库初始化成功之后数据目录会有以下文件
  1. [root@lb ~]# ll /data/mysql/data/
  2. total 177608
  3. -rw-r----- 1 mysql mysql   196608 May  1 15:59 '#ib_16384_0.dblwr'
  4. -rw-r----- 1 mysql mysql  8585216 May  1 15:59 '#ib_16384_1.dblwr'
  5. drwxr-x--- 2 mysql mysql     4096 May  1 15:59 '#innodb_temp'/
  6. drwxr-xr-x 6 mysql mysql     4096 May  1 15:59  ./
  7. drwxr-xr-x 3 root  root      4096 May  1 15:56  ../
  8. -rw-r----- 1 mysql mysql       56 May  1 15:59  auto.cnf
  9. -rw------- 1 mysql mysql     1676 May  1 15:59  ca-key.pem
  10. -rw-r--r-- 1 mysql mysql     1112 May  1 15:59  ca.pem
  11. -rw-r--r-- 1 mysql mysql     1112 May  1 15:59  client-cert.pem
  12. -rw------- 1 mysql mysql     1680 May  1 15:59  client-key.pem
  13. -rw-r----- 1 mysql mysql     5995 May  1 15:59  ib_buffer_pool
  14. -rw-r----- 1 mysql mysql 50331648 May  1 15:59  ib_logfile0
  15. -rw-r----- 1 mysql mysql 50331648 May  1 15:59  ib_logfile1
  16. -rw-r----- 1 mysql mysql 12582912 May  1 15:59  ibdata1
  17. drwxr-x--- 2 mysql mysql     4096 May  1 15:59  mysql/
  18. -rw-r----- 1 mysql mysql 26214400 May  1 15:59  mysql.ibd
  19. drwxr-x--- 2 mysql mysql     4096 May  1 15:59  performance_schema/
  20. -rw------- 1 mysql mysql     1676 May  1 15:59  private_key.pem
  21. -rw-r--r-- 1 mysql mysql      452 May  1 15:59  public_key.pem
  22. -rw-r--r-- 1 mysql mysql     1112 May  1 15:59  server-cert.pem
  23. -rw------- 1 mysql mysql     1680 May  1 15:59  server-key.pem
  24. drwxr-x--- 2 mysql mysql     4096 May  1 15:59  sys/
  25. -rw-r----- 1 mysql mysql 16777216 May  1 15:59  undo_001
  26. -rw-r----- 1 mysql mysql 16777216 May  1 15:59  undo_002
复制代码
编写MySQL配置文件

  1. [root@lb ~]# cat /etc/my.cnf
  2. [mysqld]
  3. # 数据库文件的存储路径
  4. datadir=/data/mysql/data
  5. # 服务器监听的端口号
  6. port=3306
  7. # 字符集设置
  8. character-set-server=utf8mb4
  9. # 排序规则
  10. collation-server=utf8mb4_general_ci
  11. # 允许的最大连接数
  12. max_connections=1000
  13. # MySQL服务器的缓存大小,用于缓存数据和索引
  14. innodb_buffer_pool_size=1G
  15. # 日志文件的路径
  16. log_error=/var/log/mysql/error.log
  17. # 慢查询日志文件的路径
  18. slow_query_log_file=/var/log/mysql/slow-query.log
  19. # 开启慢查询日志,1表示开启,0表示关闭
  20. slow_query_log=1
  21. # 设定慢查询的时间阈值,单位为秒,超过此时间的查询将被记录到慢查询日志中
  22. long_query_time=3
  23. # 设置用户
  24. user=mysql
  25. # 设置socket连信息
  26. socket=/tmp/mysql.sock

  27. [client]
  28. # 客户端默认字符集
  29. default-character-set=utf8mb4

  30. [mysql]
  31. # MySQL命令行工具的默认字符集
  32. default-character-set=utf8mb4
复制代码
创建日志目录

  1. [root@lb ~]# mkdir -p /var/log/mysql/
  2. [root@lb ~]# chown -R mysql.mysql /var/log/mysql/
复制代码
修改MySQL安装目录中的启动文件

拷贝启动文件至/etc/init.d目录下
  1. [root@lb ~]# cp /root/mysql/support-files/mysql.server /etc/init.d/mysqld
  2. [root@lb ~]# chmod +x /etc/init.d/mysqld
复制代码
修改启动文件的内容
vim /etc/init.d/mysqld
2.png

启动MySQL服务

  1. # 没报错即代表启动成功
  2. [root@lb ~]# /etc/init.d/mysqld start
  3. Starting mysqld (via systemctl): mysqld.service.

  4. # 检查端口号
  5. [root@lb ~]# ss -lntup | grep 3306
  6. tcp   LISTEN 0      70                 *:33060            *:*    users:(("mysqld",pid=144896,fd=21))
  7. tcp   LISTEN 0      1000               *:3306             *:*    users:(("mysqld",pid=144896,fd=24))
复制代码
修改root用户密码

  1. [root@lb ~]# mysqladmin -u root password "huangsir"
  2. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
  3. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
复制代码
登录MySQL服务

  1. [root@lb ~]# mysql -uroot -phuangsir
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.
  4. Your MySQL connection id is 10
  5. Server version: 8.0.26 MySQL Community Server - GPL

  6. Copyright (c) 2000, 2021, Oracle and/or its affiliates.

  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.

  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  11. mysql>
复制代码
远程连接MySQL

MySQL默认的root用户是无法远程登录的,所以我们需要创建一个用户用于远程登录
  1. # 创建用户,10.0.0.0/24代表网段,只有在这个网段之内的IP才能连接数据库,密码设置为huangsir
  2. mysql> CREATE USER 'root'@'10.0.0.0/24' IDENTIFIED BY 'huangsir';
  3. Query OK, 0 rows affected (0.01 sec)

  4. # 授权,*.*代表所有的库和表
  5. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.0.0.0/24' WITH GRANT OPTION;
  6. Query OK, 0 rows affected (0.00 sec)

  7. # 刷新权限
  8. mysql> FLUSH PRIVILEGES;
  9. Query OK, 0 rows affected (0.00 sec)
复制代码
使用navicat连接数据库测试
3.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册