• MySQL——Centos7下环境安装


    目录

    0.说明

    1. 检查环境(systemctl start/stop/restart)

    2. 检查系统安装包(rpm -qa)

    3. 卸载这些默认安装包(yum remove、xargs)

    4. 获取mysql官方yum源

    5. 安装mysql yum源,对比前后yum源(rpm -ivh)

    6. 能否正常工作

    7. 安装mysql服务

    8. 查看配置文件和数据存储位置

    9. 启动服务(systemctl start)

    10. 查看启动服务

    11. 登陆方法一

    12. 登陆方法二

    13. 登陆方式三

    14. 设置开机启动

    15. 配置my.cnf

    16. 常见问题


    0.说明

    • 安装与卸载中,⽤⼾全部切换成为root,⼀旦安装,普通⽤⼾能使⽤的
    • mysql不进⾏⽤⼾管理,全部使⽤root进⾏,掌握mysql语句,学习⽤⼾管理后,在考虑新建普通⽤⼾

    1. 检查环境(systemctl start/stop/restart)

    1. 查看系统当中是否存在MySQL或者mariadb

    (mariadb是MySQL开源分支),存在可直接使用MySQL

    1. [root@VM-4-10-centos /]# ps axj | grep mysql
    2. 8577 10898 10897 17609 pts/2 10897 S+ 0 0:00 grep --color=auto mysql
    3. 1 20559 20558 20558 ? -1 Sl 27 25:33 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    4. [root@VM-4-10-centos /]# ps axj | grep mariadb
    5. 8577 10978 10977 17609 pts/2 10977 S+ 0 0:00 grep --color=auto mariadb

    2. 确定MySQL版本

    1. [root@VM-4-10-centos /]# which mysql
    2. /usr/bin/mysql
    3. [root@VM-4-10-centos /]# mysql --version
    4. mysql Ver 14.14 Distrib 5.7.43, for Linux (x86_64) using EditLine wrapper

    3. 使用Linux脚本程序终止MySQL服务,systemctl stop/ restart/start,相当于暂停守护进程

    1. [root@VM-4-10-centos /]# systemctl stop mysqld
    2. [root@VM-4-10-centos /]#
    3. [root@VM-4-10-centos /]# ps axj | grep mariadb
    4. 8577 12824 12823 17609 pts/2 12823 S+ 0 0:00 grep --color=auto mariadb

    2. 检查系统安装包(rpm -qa)

    使用系统命令rpm检查系统所安装的MySQL安装包

    1. [root@VM-4-10-centos /]# rpm -qa | grep mysql
    2. mysql57-community-release-el7-9.noarch
    3. mysql-community-libs-5.7.43-1.el7.x86_64
    4. mysql-community-common-5.7.43-1.el7.x86_64
    5. mysql-community-libs-compat-5.7.43-1.el7.x86_64
    6. mysql-community-server-5.7.43-1.el7.x86_64
    7. mysql-community-client-5.7.43-1.el7.x86_64

    3. 卸载这些默认安装包(yum remove、xargs)

    使用系统工具xargs将从标准输出读取内容按照行转换为参数argv,传递给yum,逐行卸载,需要加参数-y,yum卸载会询问,因此批量卸载会中断

    1. [root@VM-4-10-centos /]# rpm -qa | grep mysql | xargs yum -y remove
    2. Loaded plugins: fastestmirror, langpacks
    3. Repository epel is listed more than once in the configuration
    4. Resolving Dependencies

    4. 获取mysql官方yum源

    获取mysql官⽅yum源 http://repo.mysql.com/

    查看Linux本地yum源:

    1. [root@VM-4-10-centos /]# ls /etc/yum.repos.d/ -l
    2. total 52
    3. -rw-r--r-- 1 root root 615 Jul 20 18:00 CentOS-Base.repo
    4. -rw-r--r-- 1 root root 1309 Nov 23 2020 CentOS-CR.repo
    5. -rw-r--r-- 1 root root 649 Nov 23 2020 CentOS-Debuginfo.repo
    6. -rw-r--r-- 1 root root 230 Mar 21 2023 CentOS-Epel.repo
    7. -rw-r--r-- 1 root root 314 Nov 23 2020 CentOS-fasttrack.repo
    8. -rw-r--r-- 1 root root 630 Nov 23 2020 CentOS-Media.repo
    9. -rw-r--r-- 1 root root 1331 Nov 23 2020 CentOS-Sources.repo
    10. -rw-r--r-- 1 root root 8515 Nov 23 2020 CentOS-Vault.repo
    11. -rw-r--r-- 1 root root 616 Nov 23 2020 CentOS-x86_64-kernel.repo
    12. -rw-r--r-- 1 root root 1358 Sep 5 2021 epel.repo
    13. -rw-r--r-- 1 root root 1457 Sep 5 2021 epel-testing.repo

    有关安装版本选择:

    1.  确定自身系统版本,Centos对应安装el,Ubuntu对应安装Ubuntu,SUSE对应sles

    1. [root@VM-4-10-centos /]# cat /etc/redhat-release
    2. CentOS Linux release 7.9.2009 (Core)

    2. 最好安装和⾃⼰系统⼀致的mysql版本,否则可能会存在软件兼容性问题,yum源中mysql后数字对应版本,本示例安装5.7版本mysql157

    3. 根据系统版本7.9 选择对应的mysql157 - release -el7.9.noarch.rpm

    5. 安装mysql yum源,对比前后yum源(rpm -ivh)

    1. 使用系统命令rz上传yum源

    1. [root@VM-4-10-centos 1Lesson]# ls
    2. mysql57-community-release-el7-9.noarch.rpm
    3. [root@VM-4-10-centos 1Lesson]# rz

    2. 使用系统命令rpm -ivh对yum源相当于解包解压

    1. [root@VM-4-10-centos 1Lesson]# ls
    2. mysql57-community-release-el7-9.noarch.rpm
    3. [root@VM-4-10-centos 1Lesson]# ls /etc/yum.repos.d/ -l
    4. total 52
    5. -rw-r--r-- 1 root root 615 Jul 20 18:00 CentOS-Base.repo
    6. -rw-r--r-- 1 root root 1309 Nov 23 2020 CentOS-CR.repo
    7. -rw-r--r-- 1 root root 649 Nov 23 2020 CentOS-Debuginfo.repo
    8. -rw-r--r-- 1 root root 230 Mar 21 2023 CentOS-Epel.repo
    9. -rw-r--r-- 1 root root 314 Nov 23 2020 CentOS-fasttrack.repo
    10. -rw-r--r-- 1 root root 630 Nov 23 2020 CentOS-Media.repo
    11. -rw-r--r-- 1 root root 1331 Nov 23 2020 CentOS-Sources.repo
    12. -rw-r--r-- 1 root root 8515 Nov 23 2020 CentOS-Vault.repo
    13. -rw-r--r-- 1 root root 616 Nov 23 2020 CentOS-x86_64-kernel.repo
    14. -rw-r--r-- 1 root root 1358 Sep 5 2021 epel.repo
    15. -rw-r--r-- 1 root root 1457 Sep 5 2021 epel-testing.repo
    16. [root@VM-4-10-centos 1Lesson]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm
    17. Preparing... ################################# [100%]
    18. Updating / installing...
    19. 1:mysql57-community-release-el7-9 ################################# [100%]
    20. [root@VM-4-10-centos 1Lesson]# ls /etc/yum.repos.d/ -l
    21. total 60
    22. -rw-r--r-- 1 root root 615 Jul 20 18:00 CentOS-Base.repo
    23. -rw-r--r-- 1 root root 1309 Nov 23 2020 CentOS-CR.repo
    24. -rw-r--r-- 1 root root 649 Nov 23 2020 CentOS-Debuginfo.repo
    25. -rw-r--r-- 1 root root 230 Mar 21 2023 CentOS-Epel.repo
    26. -rw-r--r-- 1 root root 314 Nov 23 2020 CentOS-fasttrack.repo
    27. -rw-r--r-- 1 root root 630 Nov 23 2020 CentOS-Media.repo
    28. -rw-r--r-- 1 root root 1331 Nov 23 2020 CentOS-Sources.repo
    29. -rw-r--r-- 1 root root 8515 Nov 23 2020 CentOS-Vault.repo
    30. -rw-r--r-- 1 root root 616 Nov 23 2020 CentOS-x86_64-kernel.repo
    31. -rw-r--r-- 1 root root 1358 Sep 5 2021 epel.repo
    32. -rw-r--r-- 1 root root 1457 Sep 5 2021 epel-testing.repo
    33. -rw-r--r-- 1 root root 1416 Sep 12 2016 mysql-community.repo
    34. -rw-r--r-- 1 root root 1440 Sep 12 2016 mysql-community-source.repo

    3. 观察mysql-community.repo,拥有各版本MySQL,yum链接及客服端服务端,yum会根据系统找最合适的yum源

    1. 1 [mysql-connectors-community]
    2. 2 name=MySQL Connectors Community
    3. 3 baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
    4. 4 enabled=1
    5. 5 gpgcheck=1
    6. 6 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    7. 7
    8. 8 [mysql-tools-community]
    9. 9 name=MySQL Tools Community
    10. 10 baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
    11. 11 enabled=1
    12. 12 gpgcheck=1
    13. 13 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    14. 14
    15. 15 # Enable to use MySQL 5.5
    16. 16 [mysql55-community]
    17. 17 name=MySQL 5.5 Community Server
    18. 18 baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
    19. 19 enabled=0
    20. 20 gpgcheck=1
    21. 21 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    22. 22
    23. 23 # Enable to use MySQL 5.6
    24. 24 [mysql56-community]
    25. 25 name=MySQL 5.6 Community Server
    26. 26 baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
    27. 27 enabled=0
    28. 28 gpgcheck=1
    29. 29 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

    6. 能否正常工作

    使用yum查找mysql,查看能否成功工作,根据yum源在对应的网站中mysql镜像或者安装包进行获取

    1. [root@VM-4-10-centos 1Lesson]# yum list | grep mysql
    2. Repository epel is listed more than once in the configuration
    3. mysql57-community-release.noarch el7-9 installed
    4. akonadi-mysql.x86_64 1.9.2-4.el7 os
    5. anope-mysql.x86_64 2.0.14-1.el7 epel
    6. apr-util-mysql.x86_64 1.5.2-6.el7_9.1 updates
    7. calligra-kexi-driver-mysql.x86_64 2.9.10-2.el7 epel
    8. collectd-mysql.x86_64 5.8.1-1.el7 epel
    9. dmlite-plugins-mysql.x86_64 1.15.2-15.el7 epel
    10. dovecot-mysql.x86_64 1:2.2.36-8.el7 os
    11. dpm-copy-server-mysql.x86_64 1.13.0-1.el7 epel
    12. dpm-name-server-mysql.x86_64 1.13.0-1.el7 epel

    7. 安装mysql服务

    1. 安装前是不存在mysql服务及客户端,以及配置文件的

    1. [root@VM-4-10-centos 1Lesson]# which mysql
    2. /usr/bin/which: no mysql in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/customer/.local/bin:/home/customer/bin)
    3. [root@VM-4-10-centos 1Lesson]# which mysqld
    4. /usr/bin/which: no mysqld in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/customer/.local/bin:/home/customer/bin)
    5. [root@VM-4-10-centos 1Lesson]# ls /etc/my.cf
    6. ls: cannot access /etc/my.cf: No such file or directory

    2. 使用yum install进行安装mysql-community-server,会将服务端、客户端、开发库、公共组件等全部进行安装

    1. [root@VM-4-10-centos 1Lesson]# yum install -y mysql-community-server
    2. Loaded plugins: fastestmirror, langpacks
    3. Repository epel is listed more than once in the configuration
    4. Loading mirror speeds from cached hostfile

    3. 安装成功

    1. [root@VM-4-10-centos 1Lesson]# which mysql
    2. /usr/bin/mysql
    3. [root@VM-4-10-centos 1Lesson]# which mysqld
    4. /usr/sbin/mysqld
    5. [root@VM-4-10-centos 1Lesson]# ls /etc/my.cnf
    6. /etc/my.cnf

    4. 安装失败跳转第16步

    8. 查看配置文件和数据存储位置

    查看配置文件/etc/my.cnf

    1. # For advice on how to change settings please see
    2. # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    3. [mysqld]
    4. #
    5. # Remove leading # and set to the amount of RAM for the most important data
    6. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    7. # innodb_buffer_pool_size = 128M
    8. #
    9. # Remove leading # to turn on a very important data integrity option: logging
    10. # changes to the binary log between backups.
    11. # log_bin
    12. #
    13. # Remove leading # to set options mainly useful for reporting servers.
    14. # The server defaults are faster for transactions and fast SELECTs.
    15. # Adjust sizes as needed, experiment to find the optimal values.
    16. # join_buffer_size = 128M
    17. # sort_buffer_size = 2M
    18. # read_rnd_buffer_size = 2M
    19. datadir=/var/lib/mysql
    20. socket=/var/lib/mysql/mysql.sock
    21. # Disabling symbolic-links is recommended to prevent assorted security risks
    22. symbolic-links=0
    23. log-error=/var/log/mysqld.log
    24. pid-file=/var/run/mysqld/mysqld.pid

    9. 启动服务(systemctl start)

    启动mysql服务端mysqld

    1. [root@VM-4-10-centos 1Lesson]# systemctl start mysqld
    2. [root@VM-4-10-centos 1Lesson]# ps axj | grep mysqld
    3. 1 3696 3695 3695 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    4. 8577 3806 3805 17609 pts/2 3805 S+ 0 0:00 grep --color=auto mysqld

    10. 查看启动服务

    查看mysqld服务端口号,TCP协议,应用层服务(网络服务:有自己的协议)

    1. [root@VM-4-10-centos 1Lesson]# netstat -nltp
    2. Active Internet connections (only servers)
    3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1164/sshd
    5. tcp6 0 0 :::22 :::* LISTEN 1164/sshd
    6. tcp6 0 0 :::3306 :::* LISTEN 3696/mysqld

    11. 登陆方法一

    #获取临时root密码

    1. [root@VM-4-10-centos 1Lesson]# grep 'temporary password' /var/log/mysqld.log
    2. 2022-10-17T08:56:20.292348Z 1 [Note] A temporary password is generated for root@localhost: 5xk/,!/t)5Pl

    #使⽤临时密码登录

    #判断修改密码时候新密码是否符合当前的策略,不满⾜报错,不让修改,关闭它 #安全强度,默认为中,即1,要求必须包含 数字、符号、⼤⼩写字⺟,⻓度⾄少为8位

    mysql> set global validate_password_policy=0;

    Query OK, 0 rows affected (0.00 sec)

    #设置密码最⼩⻓度

    mysql> set global validate_password_length=1;

    Query OK, 0 rows affected (0.00 sec)

    #修改本地登录密码,暂不授权远程登录

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root.888';

    Query OK, 0 rows affected (0.00 sec)

    mysql> FLUSH PRIVILEGES;

    Query OK, 0 rows affected (0.00 sec)

    #如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码

    12. 登陆方法二

    # 如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码

    # 试着直接client登陆⼀下

    13. 登陆方式三

    1. 打开mysql配置⽂件

    [root@VM-4-10-centos 1Lesson]# vim /etc/my.cnf

    在[mysqld]最后⼀栏配置(不知道是什么,就放在配置⽂件最后) 加⼊: skip-grant-tables 选项, 并保存退出

    2. 重启服务systemctl restart

    3. 重新登陆

    1. [root@VM-4-10-centos 1Lesson]# mysql -u root -p
    2. Enter password:
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 2
    5. Server version: 5.7.44 MySQL Community Server (GPL)

    14. 设置开机启动

    #开启开机⾃启动

    systemctl enable mysqld

    systemctl daemon-reload

    15. 配置my.cnf

    #配置⼀下my.conf,主要是数据库客⼾端和服务器的编码格式

    #default-character-set=utf8 ,暂不设置,mysql有bug,汉字不回显

    character-set-server=utf8

    default-storage-engine=innodb

    # 配置完毕,重启mysql即可

    16. 常见问题

    1. 安装遇到秘钥过期的问题(服务器是统一环境,一部分秘钥在打包中是过期的):

    Failing package is: mysql-community-client-5.7.39-1.el7.x86_64

    GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

    解决⽅案执行此行命令,更新秘钥(主要用于mysql安全通信,防止握手失败):

    rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

    2. mysql 已经配置了客⼾端服务器utf8编码,但是⽆法输⼊中⽂

    确保您在终端命令⾏中可以输⼊中⽂

    [root@VM-0-3-centos ~]$ env | grep LANG LANG=en_US.utf8

  • 相关阅读:
    计算机毕业设计Java桂林恒保健康防护有限公司官网(源码+系统+mysql数据库+Lw文档)
    Unity 游戏开发、03 基础篇 | C#初级编程
    Kotlin 开发Android app(六):Kotlin 中的空判断 问号和感叹号
    Git 的内部工作原理
    可能就蛮好了呢,哈哈哈哈
    在 Git Bash 中为 vim 设置 Dracula 配色
    WiFi基础学习到实战(三:WiFi网络“物理层”)
    mysql-面试50题-2
    Spring系列篇一《Spring底层的核心原理解析》
    期货开户供求平衡周而复始
  • 原文地址:https://blog.csdn.net/IfYouHave/article/details/134251931