中心件 mysql安装

[复制链接]
发表于 2025-12-2 12:28:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1、下载mysql   
Downloads->MySQL Community (GPL) Downloads ->[MySQL Community Server]
MySQL :: Download MySQL Community Server
检察体系版本
[root@localhost mysql]# cat /etc/redhat-release ​ CentOS Linux release 7.9.2009 (Core)
检察硬件架构范例
[root@localhost mysql]# uname -m ​ x86_64
起首检察体系版本
   rpm -qa | grep glibc
  glibc-devel-2.17-326.el7_9.3.x86_64
glibc-2.17-326.el7_9.3.x86_64
glibc-headers-2.17-326.el7_9.3.x86_64
glibc-common-2.17-326.el7_9.3.x86_64

Select Version: 8.0.40
Select Operating Systeminux-Generic
Select OS Versioninux-Generic(glibc)(x86,64-bit)


1、下载安装包

  1. [root@localhost mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar
复制代码
检察下载文件安装包:
  1. [root@localhost mysql]# ll
  2. 总用量 458972
  3. -rw-r--r--.  1 root  root  469985280 9月  21 02:30 mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar
复制代码
解压mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar 安装包
  1. [root@localhost mysql]# tar -xvf mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar
  2. mysql-test-8.0.40-linux-glibc2.17-x86_64-minimal.tar.xz
  3. mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar.xz
  4. mysql-router-8.0.40-linux-glibc2.17-x86_64-minimal.tar.xz
复制代码
检察解压文件
  1. [root@localhost mysql]# ll
  2. 总用量 917940
  3. drwxr-xr-x. 10 mysql mysql       141 12月 19 10:02 mysql8
  4. -rw-r--r--.  1 root  root  469985280 9月  21 02:30 mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar
  5. -rw-r--r--.  1  7155 31415  60957800 9月  21 00:57 mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar.xz
  6. -rw-r--r--.  1  7155 31415   7981904 9月  21 01:41 mysql-router-8.0.40-linux-glibc2.17-x86_64-minimal.tar.xz
  7. -rw-r--r--.  1  7155 31415 401035692 9月  21 02:24 mysql-test-8.0.40-linux-glibc2.17-x86_64-minimal.tar.xz
复制代码
解压 mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar.xz
  1. [root@localhost mysql]# tar -xvf mysql-8.0.40-linux-glibc2.17-x86_64-minimal.tar.xz
  2. mysql-8.0.40-linux-glibc2.17-x86_64-minimal/bin/
  3. mysql-8.0.40-linux-glibc2.17-x86_64-minimal/bin/myisam_ftdump
  4. mysql-8.0.40-linux-glibc2.17-x86_64-minimal/bin/myisamchk
  5. ​
复制代码


  • 卸载mariadb
在 Linux 体系中,如果已经安装了 MariaDB,而且想要安装 MySQL,须要先卸载 MariaDB 以制止辩说。
  1. #先列出MariaDB相关的包:
  2. [root@localhost bin]# rpm -qa | grep mariadb
  3. mariadb-libs-6.0-2.el7.x86_64
  4. mariadb-server-5.5.56-2.el7.x86_64
复制代码
卸载列出的包:
  1. rpm -e --nodeps  mariadb-libs-6.0-2.el7.x86_64
  2. rpm -e --nodeps  mariadb-server-5.5.56-2.el7.x86_64
复制代码
查抄是否卸载乐成:(无任何返回值,则体现卸载乐成)
  1. rpm -qa | grep mariadb
复制代码

2、检察文件而且重名文件夹名

  1. [root@localhost mysql]# ll
  2. 总用量 917940
  3. drwxr-xr-x. 9 root root        129 12月 19 09:52 mysql-8.0.40-linux-glibc2.17-x86_64-minimal
  4. ​
  5. [root@localhost mysql]# mv mysql-8.0.40-linux-glibc2.17-x86_64-minimal  mysql8
  6. ​
复制代码
cd到mysql解压后的目次中:
  1. [root@localhost mysql]# cd /home/tools/mysql/mysql8
复制代码

3、创建存储初始化数据目次

  1. [root@localhost mysql8]# mkdir data
  2. ​
  3. [root@localhost mysql8]# ll
  4. 总用量 268
  5. drwxr-xr-x.  2 7161 31415   4096 9月  18 19:35 bin
  6. drwxr-xr-x.  2 root root       6 12月 18 17:42 data
  7. drwxr-xr-x.  2 7161 31415     38 9月  18 19:35 docs
  8. drwxr-xr-x.  3 7161 31415   4096 9月  18 19:35 include
  9. drwxr-xr-x.  6 7161 31415    201 9月  18 19:35 lib
  10. -rw-r--r--.  1 7161 31415 257478 9月  18 18:08 LICENSE
  11. drwxr-xr-x.  4 7161 31415     30 9月  18 19:35 man
  12. -rw-r--r--.  1 7161 31415    666 9月  18 18:08 README
  13. drwxr-xr-x. 28 7161 31415   4096 9月  18 19:35 share
  14. drwxr-xr-x.  2 7161 31415     77 9月  18 19:35 support-files
复制代码

4、创建 MySQL 用户和用户组并设置目次权限

创建用户和用户组:
  1. groupadd mysql
  2. useradd -g mysql mysql
复制代码
groupadd mysql:创建一个名为 mysql 的用户组,以便于管理 MySQL 用户。
useradd -g mysql mysql:创建一个名为 mysql 的用户,并将其添加到 mysql 用户组,以进步安全性。

设置目次权限:
  1. ​
  2. [root@localhost mysql8]#  chown -R mysql:mysql /home/tools/mysql/mysql8
  3. [root@localhost mysql8]#  chmod 750 /home/tools/mysql/mysql8/data
复制代码
chown -R mysql:mysql /home/tools/mysql/mysql8:递归地将 MySQL 安装目次的全部权更改为 mysql 用户和用户组。
chmod 750 /home/tools/mysql/mysql8/data:设置数据目次的权限为 750,体现全部者有读、写、实行权限,组用户有读和实行权限,其他用户无权限。

5、创建立置文件

MySQL所需的设置文件放在/etc下叫my.cnf(原来没有,直接创建)
  1. [root@localhost mysql8] vim /etc/my.cnf
复制代码
  1. [mysqld]
  2. bind-address=0.0.0.0  # 允许从任何 IP 地址连接
  3. port=3306              # MySQL 默认端口
  4. user=mysql             # 运行 MySQL 的用户
  5. basedir=/home/tools/mysql/mysql8  # MySQL 安装根目录
  6. datadir=/home/tools/mysql/mysql8/data  # 数据存储目录
  7. ​
  8. [mysqld_safe]
  9. socket=/home/tools/mysql/mysql8/data/tmp/mysql.sock  # socket 文件路径
  10. log-error=/home/tools/mysql/mysql8/data/error/mysql.log  # 错误日志日志路径
  11. pid-file=/home/tools/mysql/mysql8/data/RR.pid  # 进程 ID 文件路径
  12. ​
  13. character_set_server=utf8mb4  # 设置服务器字符集为 utf8mb4
  14. symbolic-links=0              # 是否支持符号链接
  15. explicit_defaults_for_timestamp=ON  # 时间戳处理模式
  16. sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION  # SQL 模式设置
  17. lower_case_table_names=1      # 忽略表名大小写
  18. ​
  19. [client]
  20. default-character-set=utf8mb4   # 客户端默认字符集
  21. socket=/home/tools/mysql/mysql8/data/tmp/mysql.sock  # 客户端 socket 文件路径
  22. ​
  23.                                                                                  
复制代码

6、创建mysql服务
将 MySQL 服务脚本复制到 /etc/init.d/:
   cp -a /home/tools/mysql/mysql8/support-files/mysql.server /etc/init.d/mysql
  授权并添加服务:


  • 给服务脚本实行权限
  chmod +x /etc/init.d/mysql
  

  • 将 MySQL 服务添加到体系管理工具中
  chkconfig --add mysql
  7、初始化mysql
  1. ./mysqld --user=mysql --basedir=/home/tools/mysql/mysql8 --datadir=/home/tools/mysql/mysql8/data/ --initialize
  2. 2024-12-19T02:02:42.439684Z 0 [System] [MY-013169] [Server] /home/tools/mysql/mysql8/bin/mysqld (mysqld 8.0.40) initializing of server in progress as process 15266
  3. 2024-12-19T02:02:42.520016Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  4. 2024-12-19T02:02:44.027181Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  5. 2024-12-19T02:02:48.155762Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 1zN*IUOpc#qs
  6. ​
复制代码
./mysqld:指向 MySQL 服务器的可实行文件。通常位于 MySQL 安装目次中的 bin 子目次下,比如 /usr/local/mysql/mysql8/bin/mysqld。
--user=mysql:指定以 mysql 用户的身份运行 MySQL 服务器。这是为了确保 MySQL 进程具有适当的文件权限,特殊是对于数据目次的读写权限。
--basedir=/usr/local/mysql/mysql8:指定 MySQL 的安装根目次。MySQL 将根据此路径找到二进制文件和其他干系资源(如 share 目次中的支持文件)。
--datadir=/usr/local/mysql/mysql8/data/:指定 MySQL 的数据存储目次。在初始化过程中,MySQL 会在此目次下创建体系数据库和须要的文件,以便 MySQL 服务可以大概正常运行。
--initialize:实行初始化操纵。这会在指定的数据目次中创建 MySQL 体系表和其他须要的体系文件,别的还会天生一个随机的 root 初始暗码。留意,该操纵只能在数据目次为空时运行。
6、创建MySQL数据文件

创建用于存储 MySQL 数据的目次,包罗数据文件、错误日志日志和暂时文件目次,这些目次可以大概管理和故障排查
  1. mkdir -p /home/tools/mysql/mysql-8.0.40/data/error
  2. chown -R mysql:mysql /home/tools/mysql/mysql-8.0.40/data/error
  3. chmod 755 /home/tools/mysql/mysql-8.0.40/data/error
  4. ​
  5. touch /home/tools/mysql/mysql-8.0.40/data/error/mysql.log
  6. chown mysql:mysql /home/tools/mysql/mysql-8.0.40/data/error/mysql.log
  7. chmod 644 /home/tools/mysql/mysql-8.0.40/data/error/mysql.log
  8. ​
  9. chown -R mysql:mysql /home/tools/mysql/mysql-8.0.40/data
  10. chmod -R 755 /home/tools/mysql/mysql-8.0.40/data
  11. ​
  12. mkdir -p /home/tools/mysql/mysql-8.0.40/data/tmp
  13. chown -R mysql:mysql /home/tools/mysql/mysql-8.0.40/data/tmp
  14. chmod 755 /home/tools/mysql/mysql-8.0.40/data/tmp
复制代码
7、启动mysql

  1. [root@localhost data]# service mysql start
  2. Starting MySQL.. SUCCESS!
  3. ​
  4. #查看mysql进程
  5. [root@localhost data]# ps -ef|grep mysql
  6. root     30069     1  0 10:08 pts/3    00:00:00 /bin/sh /home/tools/mysql/mysql8/bin/mysqld_safe --datadir=/home/tools/mysql/mysql8/data --pid-file=/home/tools/mysql/mysql8/data/localhost.localdomain.pid
  7. mysql    30349 30069  5 10:08 pts/3    00:00:01 /home/tools/mysql/mysql8/bin/mysqld --basedir=/home/tools/mysql/mysql8 --datadir=/home/tools/mysql/mysql8/data --plugin-dir=/home/tools/mysql/mysql8/lib/plugin --user=mysql --log-error=/home/tools/mysql/mysql8/data/error/mysql.log --pid-file=/home/tools/mysql/mysql8/data/localhost.localdomain.pid --socket=/home/tools/mysql/mysql8/data/tmp/mysql.sock --port=3306
  8. root     31125 14690  0 10:08 pts/3    00:00:00 grep --color=auto mysql
  9. ​
复制代码
8、修改数据库暗码

  1. [root@localhost data]# mysql -uroot -p
  2. #将刚才初始化的密码输入进去 1zN*IUOpc#qs
  3. Enter password:
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. Your MySQL connection id is 8
  6. Server version: 8.0.40
  7. Copyright (c) 2000, 2024, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. ​
  13. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
  14. Query OK, 0 rows affected (0.01 sec)
  15. ​
  16. #刷新一下权限
  17. mysql> flush privileges;
  18. Query OK, 0 rows affected (0.00 sec)
  19. ​
  20. #退出
  21. mysql> quit
  22. ​
  23. ​
  24. -bash: mysql: 未找到命令
  25. ​
  26. #解决方案:
  27. vim /etc/profile或者 su - mysql
  28. #添加环境变量
  29.     export PATH=$PATH:/usr/local/mysql/bin
  30. #然后执行:
  31. source /etc/profile命令,使环境变量配置生效。之后再尝试输入 mysql -uroot -p 命令,看是否能正常运行。
  32. ​
  33. ​
复制代码
重启数据库
  1. [root@localhost bin]# service mysql restart
  2. Shutting down MySQL.. SUCCESS!
  3. Starting MySQL.. SUCCESS!
复制代码
9、设置远程登录
  1. #新密码登录
  2. [root@localhost bin]# mysql -uroot -p
  3. Enter password:
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. Your MySQL connection id is 8
  6. Server version: 8.0.40 MySQL Community Server - GPL
  7. ​
  8. Copyright (c) 2000, 2024, Oracle and/or its affiliates.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. ​
  14. #查看数据库
  15. mysql> show databases
  16.    -> ;
  17. +--------------------+
  18. | Database           |
  19. +--------------------+
  20. | information_schema |
  21. | mysql              |
  22. | performance_schema |
  23. | sys                |
  24. +--------------------+
  25. 4 rows in set (0.01 sec)
  26. ​
  27. #user 表在mysql库中,先进mysql
  28. mysql> use mysql;
  29. Reading table information for completion of table and column names
  30. You can turn off this feature to get a quicker startup with -A
  31. Database changed
  32. ​
  33. #远程登录设置:更新 MySQL 数据库中 mysql 库的 user 表,将 root 用户的 host 字段设置为 %,意味着 root 用户可以从任何主机连接到 MySQL 服务。% 是 MySQL 中的通配符,表示任何 IP 地址。
  34. mysql> UPDATE user SET host='%' WHERE user='root';
  35. Query OK, 1 row affected (0.01 sec)
  36. Rows matched: 1  Changed: 1  Warnings: 0
  37. #刷新一下权限
  38. mysql> flush privileges;
  39. Query OK, 0 rows affected (0.00 sec)
  40. ​
  41. #退出
  42. mysql> exit
  43. ​
  44. ​
复制代码
重启数据库
  1. [root@localhost bin]# service mysql restart
  2. Shutting down MySQL.... SUCCESS!
  3. Starting MySQL.. SUCCESS!
复制代码
9、添加防火墙

  1. ​
  2. [root@localhost bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
  3. Warning: ALREADY_ENABLED: 3306:tcp
  4. success
  5. [root@localhost bin]# firewall-cmd --reload
  6. success
  7. [root@localhost bin]# firewall-cmd --zone=public --query-port=3306/tcp
  8. yes
复制代码
10、远程验证


11、维护下令

  1. [root@localhost bin]# service mysql stop
  2. Shutting down MySQL.. SUCCESS!
  3. [root@localhost bin]# service mysql start
  4. Starting MySQL.. SUCCESS!
复制代码
12、卸载重装

1.查询本机安装的mysql rpm -qa |grep -i mysql
2.利用 yum remove …删除每一个安装 删除后再利用 rpm -qa |grep -i mysql 举行验证
3.查找mysql干系目次 find / -name mysql
4.对这些目次一个个的举行删除 rm -rf
5.删除/etc/my.cnf 文件 rm -rf /etc/my.cnf
删除 /var/log/mysql/mysqld.log文件 rm -rf /var/log/mysql/mysqld.log
6.卸载mariadb
在 Linux 体系中,如果已经安装了 MariaDB,而且想要安装 MySQL,须要先卸载 MariaDB 以制止辩说。
  1. #先列出MariaDB相关的包:
  2. [root@localhost bin]# rpm -qa | grep mariadb
  3. mariadb-libs-6.0-2.el7.x86_64
  4. mariadb-server-5.5.56-2.el7.x86_64
复制代码
卸载列出的包:
  1. rpm -e --nodeps  mariadb-libs-6.0-2.el7.x86_64
  2. rpm -e --nodeps  mariadb-server-5.5.56-2.el7.x86_64
复制代码
查抄是否卸载乐成:(无任何返回值,则体现卸载乐成)
  1. rpm -qa | grep mariadb
复制代码
参考链接:
Linux安装MySQL8.0.40详情(保姆级教程)-CSDN博客
【linux彻底卸载mysql8.0(亲测可用)】_linux卸载mysql8.0-CSDN博客
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表