翼度科技»论坛 编程开发 mysql 查看内容

【MySQL】MySQL8安装

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
1. MySQL8安装

安装环境


  • 操作系统:CentOS7
  • MySQL版本:8.0.28
  • 安装方式:二进制Generic
  • 软件路径:/app/database
  • 数据路径:/data/3306
  • 日志路径:/binlog/3306
MySQL Community Server 社区版官网下载链接
MySQL :: Download MySQL Community Server (Archived Versions)
2. MySQL三种安装方式


  • 源码安装:编译安装,可定制化高,安装难度大时间长,不适合小白,不推荐
  • rpm包安装:安装简单方便,但仅限rpm系列Linux系统,可定制化不高,生产不推荐
  • 二进制安装:解压即用,不限平台,官方已编译,可定制,推荐
3. 二进制版本安装

3.1 二进制安装包说明


二进制版本会有三个安装包下载

  • Compressed TAR Archive:主要安装包,如果没有特殊要求只下载这个包
  • Compressed TAR Archive, Test Suite:测试包,一般用不着
  • TAR:包含以上两个包
3.2 MySQL二进制版本软件目录说明

解压后目录如下
  1. drwxr-xr-x.  2 7161 31415   4096 Dec 18  2021 bin # mysql所有可执行程序命令,登陆、备份等等
  2. drwxr-xr-x.  2 7161 31415     55 Dec 18  2021 docs # mysql文档
  3. drwxr-xr-x.  3 7161 31415   4096 Dec 18  2021 include # 依赖库
  4. drwxr-xr-x.  6 7161 31415    201 Dec 18  2021 lib # 函数库
  5. -rw-r--r--.  1 7161 31415 276595 Dec 18  2021 LICENSE
  6. drwxr-xr-x.  4 7161 31415     30 Dec 18  2021 man # 帮助文件
  7. -rw-r--r--.  1 7161 31415    666 Dec 18  2021 README
  8. drwxr-xr-x. 28 7161 31415   4096 Dec 18  2021 share
  9. drwxr-xr-x.  2 7161 31415     77 Dec 18  2021 support-files # 脚本存放目录
复制代码
3.3 安装前环境准备

3.3.1 检查是否已安装MySQL
  1. # 查看rpm是否有安装
  2. rpm -qa | grep -i mysql  # -i 忽略大小写
  3. # 或者
  4. yum list installed | grep mysql
  5. # 查看mysql服务是否在运行
  6. systemctl status mysqld.service
  7. # 卸载mysql
  8. yum remove mysql-xxx mysql-xxx mysql-xxx mysqk-xxxx
  9. # 删除mysql相关文件 慎用!!
  10. find / -name mysql | xargs rm -rf
  11. mv /etc/my.cnd /etc/my.cnd_backup
复制代码
在安装之前最好检查一下,如果有务必删除干净,包括日志文件和数据文件。
3.3.2 创建MySQL用户和组
  1. useradd -s /sbin/nologin mysql
  2. id mysql
复制代码
3.3.3 创建主要目录


  • 软件目录:存放MySQL软件
  • 数据目录:存放MySQL数据,推荐使用端口号作为目录名称
  • 日志目录:存放binlog日志,推荐使用端口号作为目录名称
注意:生产环境下软件目录、数据目录、日志目录不要单独存放在一个磁盘下!
  1. mkdir -p /app/database  # 软件目录
  2. mkdir -p /data/3306  # 数据目录
  3. mkdir -p /binlog/3306  # 日志目录
复制代码
设置目录权限给mysql用户
  1. chown -R mysql:mysql /app /data /binlog
复制代码
3.3.4 下载并解压

注意:千万不要下载到32位的包,一走眼就很容易搞混!一定是glibc2.12-x86_64
  1. wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
  2. tar -xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
复制代码
3.4 安装

3.4.1 设置环境变量
  1. # 做软连接,文件名太长不方便后续设置。不推荐修改文件名,修改了不能很快的知道mysql版本
  2. ln -s mysql-8.0.28-linux-glibc2.12-x86_64/ mysql
  3. # 编辑当前用户环境变量文件
  4. vim ~/.bash_profile
  5. # 新增修改如下内容
  6. MYSQL_HONE=/app/database/mysql
  7. PATH=$PATH:$HOME/bin:$MYSQL_HONE/bin
  8. export PATH
  9. # 让环境变量生效
  10. source ~/.bash_profile
  11. # 验证
  12. # mysql -V
  13. mysql  Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
复制代码
3.4.2 初始化系统表
系统表是mysql运行必备的表,不可缺失!本次初始化选择手动设置密码方式
  1. mysqld --initialize-insecure --user=mysql --basedir=/app/database/mysql --datadir=/data/3306
  2. # 参数说明
  3. --initialize-insecure: reate the default database and exit. Create a super userwith empty password.
  4. --initialize:Create the default database and exit. Create a super userwith a random expired password and store it into the log.
  5. # 以上这两参数任选其一,区分就是一个自动生成密码,一个没有密码需要手动设置
  6. --user=name: Run mysqld daemon as user.
  7. --basedir=name: Path to installation directory. All paths are usuallyresolved relative to this
  8. --datadir=name: Path to the database root directory
复制代码
注意:初始化数据目录一定是空目录!否则初始化失败
3.4.3 设置MySQL配置文件
配置文件路径:/etc/my.cnf
以下配置文件仅供参考!根据实际机器配置来修改具体设置。
  1. [mysqld]
  2. user=mysql
  3. basedir=/app/database/mysql
  4. datadir=/data/3306
  5. server_id=6
  6. socket=/tmp/mysql.sock
  7. bind-address = 0.0.0.0
  8. # connect
  9. max_connections = 1000
  10. max_connect_errors = 200
  11. # charset
  12. character_set_server = utf8mb4
  13. # InnoDB Settings
  14. innodb_buffer_pool_size = 2G
  15. innodb_read_io_threads = 15
  16. innodb_write_io_threads = 15
  17. # innodb_flush_log_at_trx_commit = 0
  18. innodb_buffer_pool_instances = 8
  19. innodb_log_file_size = 1G
  20. innodb_log_buffer_size = 64M
  21. # innodb_flush_method = O_DIRECT
  22. innodb_page_cleaners = 16
  23. # session memory settings
  24. read_buffer_size = 32M
  25. read_rnd_buffer_size = 32M
  26. sort_buffer_size = 64M
  27. tmp_table_size = 64M
  28. join_buffer_size = 128M
  29. thread_cache_size = 64
  30. # slow log
  31. slow_query_log = 1
  32. slow_query_log_file = slow.log
  33. long_query_time = 10
  34. min_examined_row_limit = 100
  35. log-queries-not-using-indexes
  36. log_throttle_queries_not_using_indexes = 10
  37. log_timestamps = system
  38. # log settings
  39. log_error = error.log
  40. # client settings
  41. [mysql]
  42. prompt = (\\u@\\h) [\\d]>\\_
  43. socket=/tmp/mysql.sock
  44. no-auto-rehash
复制代码
3.4.3 准备MySQL启动脚本
启动脚本存放在support-files目录下的mysql.server,将启动脚本拷贝到/etc/init.d方便运行管理
CentOS7有两种启动方式,一个是init.d,一个是systemctl,这里只用第一种,第二种方式具体参考百度或者官网。
  1. cp mysql.server /etc/init.d/mysqld
复制代码
3.4.5 启动MySQL
如果没有意外的话,可以启动成功。有一种情况会启动失败,启动脚本中设置的启动路径不在环境变量里,此时需要设置软连接到脚本中对应的路径。
  1. /etc/init.d/mysqld start
复制代码
3.4.6 验证
  1. # /etc/init.d/mysqld status
  2. SUCCESS! MySQL running (11794)
  3. # mysql
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. Your MySQL connection id is 1397
  6. Server version: 8.0.28 MySQL Community Server - GPL
  7. Copyright (c) 2000, 2022, 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. (root@localhost) [(none)]>
复制代码
3.5 安装完成后的尾收工作

3.5.1 设置管理员密码
  1. mysqladmin -uroot password "WeiMin@123"
复制代码
3.5.2 CentOS7设置开机自启动
设置开机自启动的方式有很多,这里用最简单的一种。
注意:

  • 开机自启动的前提是启动脚本必须存放在/etc/init.d目录下!!
  • 生产环境最好重启验证!!
  1. # 设置开机自启动
  2. chkconfig --add mysqld
  3. # 查看mysqld是否在列表中
  4. chkconfig --list
  5. mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
  6. netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
  7. network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
复制代码
3.5.3 CentOS7关闭防火墙、关闭开机自启动防火墙
  1. iptables -F
  2. systemctl disable firewalld.service
复制代码
3.5.4 关闭selinux、修改selinux配置文件永久关闭
  1. setenforce 0
  2. # vim /etc/selinux/config
  3. SELINUX=disabled
复制代码
3.6 忘记root密码处理办法


  • —skip-grant-tables:跳过授权表,关闭用户认证
  • —skip-networking:跳过远程登录,只能本地登陆
  1. # 1. 关闭数据库
  2. /etc/init.d/mysqld stop
  3. # 2. 启动数据库到维护模式,并放到后台
  4. mysqld_safe --skip-networking --skip-grant-tables &
  5. # 3. 登陆mysql并修改密码
  6. flush privileges;
  7. alter user root@'localhost' identified by 'Admin@123';
  8. # 4. 关闭数据库,启动验证
  9. /etc/init.d/mysqld stop
  10. mysql -uroot -pAdmin@123
复制代码
3.7 启动数据库方式

启动有两种方式,不建议使用命令执行!!

  • 执行官方提供的启动脚本
  • 设置systemctl脚本
本质上启动脚本执行的是mysqld_safe --datadir=/data/3306 --pid-file=/data/3306/mysql.pid 这条命令,所以手动执行这条命令也可以启动mysql
  1. # ps -ef | grep mysqld
  2. root       6067      1  0 03:18 pts/0    00:00:00 /bin/sh /app/database/mysql/bin/mysqld_safe --datadir=/data/3306 --pid-file=/data/3306/mysql.pid
  3. mysql      6519   6067  1 03:18 pts/0    00:00:01 /app/database/mysql/bin/mysqld --basedir=/app/database/mysql --datadir=/data/3306 --plugin-dir=/app/database/mysql/lib/plugin --user=mysql --log-error=error.log --pid-file=/data/3306/mysql.pid --socket=/tmp/mysql.sock
复制代码

3.8 关闭数据库方式

有三种方式正常关闭数据库,不建议使用kill来强行关闭数据库(被强奸的感觉)

  • 登陆数据,执行shutdown命令关闭
  • 启动脚本关闭,/etc/init.d/mysqld stop
  • systemctl关闭,前提是设置了脚本,命令和就是服务的关闭命令,没有特别的地方。

来源:https://www.cnblogs.com/kuxingsengwm/p/17053038.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

x

举报 回复 使用道具