美文网首页
centos系统安装MySQL

centos系统安装MySQL

作者: hubery_zhao | 来源:发表于2021-03-03 16:41 被阅读0次

    MySQL作为开源占比很高的数据库,安装是基本功,说学逗唱一样都不能落下,需要随时拿可以展示出来,有时间写一篇文章简单介绍下安装流程。本次安装未优化配置文件。
    可以从官网获取安装方法https://www.mysql.com/
    机器:腾讯云CVM
    centos7.8

    [root@VM-16-16-centos ~]# uname -a
    Linux VM-16-16-centos 3.10.0-1127.13.1.el7.x86_64 #1 SMP Tue Jun 23 15:46:38 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
    

    一. yum安装

    yum安装:https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html
    (https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/)
    使用MySQL Yum存储库时,默认情况下会选择最新的GA系列

    1. 安装MySQL需要先卸载Mariadb

    [root@VM-16-16-centos ~]# rpm -qa |grep mariadb
    mariadb-libs-5.5.65-1.el7.x86_64
    [root@VM-16-16-centos ~]# yum remove  mariadb-libs.x86_64 
    Loaded plugins: fastestmirror, langpacks
    Resolving Dependencies
    --> Running transaction check
    ---> Package mariadb-libs.x86_64 1:5.5.65-1.el7 will be erased
    --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
    --> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
    --> Running transaction check
    ---> Package postfix.x86_64 2:2.10.1-9.el7 will be erased
    --> Finished Dependency Resolution
    ...
    Removed:
      mariadb-libs.x86_64 1:5.5.65-1.el7                                                                                           
    
    Dependency Removed:
      postfix.x86_64 2:2.10.1-9.el7                                                                                                
    
    Complete!
    

    2. 获取Yum存储库文件

    [root@VM-16-16-centos ~]# cd /data
    [root@VM-16-16-centos data]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    ...
    Connecting to repo.mysql.com (repo.mysql.com)|184.51.136.242|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 26024 (25K) [application/x-redhat-package-manager]
    Saving to: ‘mysql80-community-release-el7-3.noarch.rpm’
    
    100%[=====================================================================================>] 26,024      99.7KB/s   in 0.3s   
    
    2021-03-02 15:48:41 (99.7 KB/s) - ‘mysql80-community-release-el7-3.noarch.rpm’ saved [26024/26024]
    [root@VM-16-16-centos data]# ll
    total 28
    -rw-r--r-- 1 root root 26024 Apr 25  2019 mysql80-community-release-el7-3.noarch.rpm
    [root@VM-16-16-centos data]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm 
    warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:mysql80-community-release-el7-3  ################################# [100%]
    [root@VM-16-16-centos data]# ll /etc/yum.repos.d/
    total 20
    -rw-r--r-- 1 root root  614 Mar  2 15:25 CentOS-Base.repo
    -rw-r--r-- 1 root root  230 Mar  2 15:25 CentOS-Epel.repo
    -rw-r--r-- 1 root root  616 Apr  8  2020 CentOS-x86_64-kernel.repo
    -rw-r--r-- 1 root root 2076 Apr 25  2019 mysql-community.repo
    -rw-r--r-- 1 root root 2108 Apr 25  2019 mysql-community-source.repo
    

    查看/etc/yum.repo.d/,yum存储库文件已存在,查看文件配置是否符合我们的要求(安装5.7最新的GA版本,不安装8.0,8.0应该是默认配置)
    查看yum库文件内容

    [root@VM-16-16-centos data]# cat /etc/yum.repos.d/mysql-community.repo 
    # Enable to use MySQL 5.5
    [mysql55-community]
    name=MySQL 5.5 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    # Enable to use MySQL 5.6
    [mysql56-community]
    name=MySQL 5.6 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    # Enable to use MySQL 5.7
    [mysql57-community]
    name=MySQL 5.7 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql80-community]
    name=MySQL 8.0 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql-connectors-community]
    name=MySQL Connectors Community
    baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql-tools-community]
    name=MySQL Tools Community
    baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql-tools-preview]
    name=MySQL Tools Preview
    baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql-cluster-7.5-community]
    name=MySQL Cluster 7.5 Community
    baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql-cluster-7.6-community]
    name=MySQL Cluster 7.6 Community
    baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql-cluster-8.0-community]
    name=MySQL Cluster 8.0 Community
    baseurl=http://repo.mysql.com/yum/mysql-cluster-8.0-community/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    

    可以看到enabled有如下几项,需要使用5.7,可以在这里手动修改,也可以使用命令直接修改


    image.png

    3. 设置yum库文件,调整安装版本为5.7

    不修改的话,我们直接安装就是安装下面enabled的几项,也就是对应的库文件中的几项内容

    [root@VM-16-16-centos data]# yum repolist all | grep mysql
    mysql-cluster-7.5-community/x86_64  MySQL Cluster 7.5 Community  disabled
    mysql-cluster-7.5-community-source  MySQL Cluster 7.5 Community  disabled
    mysql-cluster-7.6-community/x86_64  MySQL Cluster 7.6 Community  disabled
    mysql-cluster-7.6-community-source  MySQL Cluster 7.6 Community  disabled
    mysql-cluster-8.0-community/x86_64  MySQL Cluster 8.0 Community  disabled
    mysql-cluster-8.0-community-source  MySQL Cluster 8.0 Community  disabled
    mysql-connectors-community/x86_64   MySQL Connectors Community   enabled:    185
    mysql-connectors-community-source   MySQL Connectors Community - disabled
    mysql-tools-community/x86_64        MySQL Tools Community        enabled:    123
    mysql-tools-community-source        MySQL Tools Community - Sour disabled
    mysql-tools-preview/x86_64          MySQL Tools Preview          disabled
    mysql-tools-preview-source          MySQL Tools Preview - Source disabled
    mysql55-community/x86_64            MySQL 5.5 Community Server   disabled
    mysql55-community-source            MySQL 5.5 Community Server - disabled
    mysql56-community/x86_64            MySQL 5.6 Community Server   disabled
    mysql56-community-source            MySQL 5.6 Community Server - disabled
    mysql57-community/x86_64            MySQL 5.7 Community Server   disabled
    mysql57-community-source            MySQL 5.7 Community Server - disabled
    mysql80-community/x86_64            MySQL 8.0 Community Server   enabled:    229
    mysql80-community-source            MySQL 8.0 Community Server - disabled
    

    使用命令修改为安装5.7最新的GA版本

    [root@VM-16-16-centos data]# yum-config-manager --enable mysql57-community
    Loaded plugins: fastestmirror, langpacks
    =================================================== repo: mysql57-community ===================================================
    [mysql57-community]
    async = True
    bandwidth = 0
    ...
    throttle = 0
    timeout = 30.0
    ui_id = mysql57-community/x86_64
    ui_repoid_vars = releasever,
       basearch
    username = 
    [root@VM-16-16-centos data]# yum-config-manager --disable mysql80-community
    Loaded plugins: fastestmirror, langpacks
    =================================================== repo: mysql80-community ===================================================
    [mysql80-community]
    async = True
    bandwidth = 0
    throttle = 0
    timeout = 30.0
    ui_id = mysql80-community/x86_64
    ui_repoid_vars = releasever,
       basearch
    username = 
    [root@VM-16-16-centos data]# yum repolist all | grep mysql
    ...
    mysql-connectors-community/x86_64   MySQL Connectors Community   enabled:    185
    mysql-tools-community/x86_64        MySQL Tools Community        enabled:    123
    mysql57-community/x86_64            MySQL 5.7 Community Server   enabled:    484
    

    4. yum安装MySQL

    [root@VM-16-16-centos data]# yum install mysql-community-server
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
    mysql-connectors-community                                                                              | 2.6 kB  00:00:00     
    mysql-tools-community                                                                                   | 2.6 kB  00:00:00     
    mysql57-community                                                                                       | 2.6 kB  00:00:00   
    ...
    Installed:
      mysql-community-server.x86_64 0:5.7.33-1.el7                                                                                 
    
    Dependency Installed:
      mysql-community-client.x86_64 0:5.7.33-1.el7                   mysql-community-common.x86_64 0:5.7.33-1.el7                  
      mysql-community-libs.x86_64 0:5.7.33-1.el7                     numactl-libs.x86_64 0:2.0.12-5.el7                            
    
    Complete!
    

    5. 启动、测试MySQL

    我这里是全新安装,服务在启动的时候就已经初始化了
    启动的时候会创建一个超级用户'root'@'localhost' , 密码存放在日志文件中/var/log/mysqld.log

    [root@VM-16-16-centos data]# systemctl start mysqld
    [root@VM-16-16-centos data]# systemctl status mysqld.service 
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2021-03-02 16:06:31 CST; 18s ago
    ...
    Mar 02 16:06:24 VM-16-16-centos systemd[1]: Starting MySQL Server...
    Mar 02 16:06:31 VM-16-16-centos systemd[1]: Started MySQL Server.
    [root@VM-16-16-centos data]# mysql -V
    mysql  Ver 14.14 Distrib 5.7.33, for Linux (x86_64) using  EditLine wrapper
    

    查看MySQL 的 root用户密码

    [root@VM-16-16-centos data]# grep 'temporary password' /var/log/mysqld.log
    2021-03-02T08:06:25.816954Z 1 [Note] A temporary password is generated for root@localhost: uD)0nK,M.rfB
    

    登录MySQL,修改以及测试密码

    [root@VM-16-16-centos data]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    ...
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY 'Test@1234';
    Query OK, 0 rows affected (0.00 sec)
    mysql> ^DBye
    [root@VM-16-16-centos data]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>
    

    6. yum升级MySQL

    yum升级没有实际测试过效果,现网环境请慎重使用
    MySQL是5.7的最新版本,所以升级只能往8.0走,修改yum库文件开启mysql8.0版本

    [root@VM-16-16-centos ~]# vim /etc/yum.repos.d/mysql-community.repo 
    ...
    [mysql80-community]
    name=MySQL 8.0 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    ...
    

    升级MySQL

    [root@VM-16-16-centos data]# yum update mysql-server
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
    mysql-connectors-community                                                                              | 2.6 kB  00:00:00     
    mysql-tools-community                                                                                   | 2.6 kB  00:00:00     
    mysql57-community                                                                                       | 2.6 kB  00:00:00     
    mysql80-community                                                                                       | 2.6 kB  00:00:00  
    ...
    Dependency Installed:
      mysql-community-client-plugins.x86_64 0:8.0.23-1.el7                                                                         
    
    Updated:
      mysql-community-server.x86_64 0:8.0.23-1.el7                                                                                 
    
    Dependency Updated:
      mysql-community-client.x86_64 0:8.0.23-1.el7                   mysql-community-common.x86_64 0:8.0.23-1.el7                  
      mysql-community-libs.x86_64 0:8.0.23-1.el7                    
    
    Complete!
    [root@VM-16-16-centos data]# mysql -V
    mysql  Ver 8.0.23 for Linux on x86_64 (MySQL Community Server - GPL)
    

    二. 使用二进制文件安装MySQL

    下载MySQL二进制包:https://dev.mysql.com/downloads/mysql/

    image.png

    1. 创建用户和组

    [root@VM-16-16-centos data]# groupadd mysql
    [root@VM-16-16-centos data]# useradd -g mysql -s /sbin/nologin mysql
    

    2. 上传二进制文件,解压到指定目录

    [root@VM-16-16-centos data]# ls
    mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
    [root@VM-16-16-centos data]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz 
    [root@VM-16-16-centos data]# ls
    mysql-5.7.26-linux-glibc2.12-x86_64  mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
    [root@VM-16-16-centos data]# 
    [root@VM-16-16-centos data]# mv mysql-5.7.26-linux-glibc2.12-x86_64  mysql
    [root@VM-16-16-centos data]# mv mysql  /usr/local/
    

    3. 添加环境变量

    [root@VM-16-16-centos data]# tail  -n 1 /etc/profile
    export PATH=/usr/local/mysql/bin:$PATH
    [root@VM-16-16-centos data]# source /etc/profile
    

    查看是否添加成功

    [root@VM-16-16-centos data]# mysql  -V
    mysql  Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using  EditLine wrapper
    

    4. 卸载mariadb 、 MySQL

    [root@VM-16-16-centos data]# yum remove mariadb-libs -y
    

    5. 创建基本目录、授权、安装依赖

    [root@VM-16-16-centos data]# mkdir  -p /data/mysql/data
    [root@VM-16-16-centos data]# mkdir  /data/binlog
    [root@VM-16-16-centos data]# chown  -R   mysql.mysql  /data  /usr/local/mysql
    [root@VM-16-16-centos data]# yum  install  libaio-devel  numactl  -y
    

    6. 初始化

    --initialize了 “默认安全”安装(即包括生成随机初始的 root密码)。在这种情况下,密码被标记为已过期,您必须选择一个新密码。
    --initialize-insecure不会root生成密码

    [root@VM-16-16-centos data]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
    2021-03-03T07:45:00.830212Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2021-03-03T07:45:01.317426Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2021-03-03T07:45:01.420513Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2021-03-03T07:45:01.510059Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5c439073-7bf4-11eb-a535-5254007a3b3b.
    2021-03-03T07:45:01.514516Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2021-03-03T07:45:01.514993Z 1 [Note] A temporary password is generated for root@localhost: hy5m:is(ca*B
    

    密码是hy5m:is(ca*B

    7. 配置文件

    [root@VM-16-16-centos data]# cat > /etc/my.cnf <<EOF
    > [mysqld]
    > user=mysql
    > basedir=/usr/local/mysql
    > datadir=/data/mysql/data
    > server_id=51
    > socket=/tmp/mysql.sock
    > [mysql]
    > socket=/tmp/mysql.sock
    > EOF
    

    8. 使用systemd管理

    [root@VM-16-16-centos data]# cat > /etc/systemd/system/mysqld.service <<EOF
    > [Unit]
    > Description=MySQL Server
    > Documentation=man:mysqld(8)
    > Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    > After=network.target
    > After=syslog.target
    > [Install]
    > WantedBy=multi-user.target
    > [Service]
    > User=mysql
    > Group=mysql
    > ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
    > LimitNOFILE = 5000
    > EOF
    

    9. 启动MySQL

    [root@VM-16-16-centos data]# systemctl start mysqld.service 
    [root@VM-16-16-centos data]# systemctl status mysqld.service 
    ● mysqld.service - MySQL Server
       Loaded: loaded (/etc/systemd/system/mysqld.service; disabled; vendor preset: disabled)
       Active: active (running) since Wed 2021-03-03 15:45:57 CST; 1s ago
    ...
    

    10. 测试密码是否正常、修改密码

    [root@VM-16-16-centos data]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.26
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    mysql> alter user root@localhost identified by 'Test@123456';
    Query OK, 0 rows affected (0.00 sec)
    mysql> ^DBye
    [root@VM-16-16-centos data]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.26 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    

    相关文章

      网友评论

          本文标题:centos系统安装MySQL

          本文链接:https://www.haomeiwen.com/subject/eezgzktx.html