> 文章列表 > 60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载

60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载

60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载

文章目录

    • 60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载
      • 60.1 安装
      • 60.2 运行
      • 60.3 卸载

60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载

60.1 安装

  1. 配置本地yum源,详细信息请参考《openEuler 22.03-LTS 搭建repo服务器》。

  2. 清除缓存。

    # dnf clean all
    

    例如示例命令如下:

    [root@superman-21 ~]# dnf clean all
    45 files removed
    [root@superman-21 ~]# 
    
  3. 创建缓存。

    # dnf makecache
    

    例如示例命令如下:

    [root@superman-21 ~]# dnf makecache
    OS                                                                                           6.5 MB/s | 3.4 MB     00:00    
    everything                                                                                    18 MB/s |  16 MB     00:00    
    EPOL                                                                                         5.4 MB/s | 2.6 MB     00:00    
    debuginfo                                                                                    3.2 MB/s | 3.9 MB     00:01    
    source                                                                                       3.9 MB/s | 1.7 MB     00:00    
    update                                                                                        15 MB/s |  29 MB     00:01    
    Metadata cache created.
    [root@superman-21 ~]# 
    
  4. 在root权限下安装mariadb服务器。

    # dnf install mysql-server
    

    例如示例命令如下:

    [root@superman-21 ~]# dnf install mysql-server
    Last metadata expiration check: 0:45:47 ago on 2022年12月21日 星期三 17时18分58秒.
    Dependencies resolved.
    ====================================================================================================================================Package                          Architecture             Version                               Repository                    Size
    ====================================================================================================================================
    Installing:mysql-server                     x86_64                   8.0.28-1.oe2203                       everything                    23 M
    Installing dependencies:mariadb-config                   x86_64                   4:10.5.16-1.oe2203                    update                       8.0 kmecab                            x86_64                   0.996-2.oe2203                        everything                   371 kmysql                            x86_64                   8.0.28-1.oe2203                       OS                            10 Mmysql-common                     x86_64                   8.0.28-1.oe2203                       OS                            30 kmysql-errmsg                     x86_64                   8.0.28-1.oe2203                       everything                   545 kmysql-selinux                    noarch                   1.0.0-2.oe2203                        everything                    35 kprotobuf-lite                    x86_64                   3.14.0-6.oe2203                       update                       217 kTransaction Summary
    ====================================================================================================================================
    Install  8 PackagesTotal download size: 35 M
    Installed size: 188 M
    Is this ok [y/N]: y
    Downloading Packages:
    (1/8): mysql-common-8.0.28-1.oe2203.x86_64.rpm                                                      241 kB/s |  30 kB     00:00    
    (2/8): mecab-0.996-2.oe2203.x86_64.rpm                                                              1.2 MB/s | 371 kB     00:00    
    (3/8): mysql-selinux-1.0.0-2.oe2203.noarch.rpm                                                      784 kB/s |  35 kB     00:00    
    (4/8): mysql-errmsg-8.0.28-1.oe2203.x86_64.rpm                                                      1.3 MB/s | 545 kB     00:00    
    (5/8): mariadb-config-10.5.16-1.oe2203.x86_64.rpm                                                   113 kB/s | 8.0 kB     00:00    
    (6/8): protobuf-lite-3.14.0-6.oe2203.x86_64.rpm                                                     1.2 MB/s | 217 kB     00:00    
    (7/8): mysql-8.0.28-1.oe2203.x86_64.rpm                                                             7.6 MB/s |  10 MB     00:01    
    (8/8): mysql-server-8.0.28-1.oe2203.x86_64.rpm                                                      5.1 MB/s |  23 MB     00:04    
    ------------------------------------------------------------------------------------------------------------------------------------
    Total                                                                                               7.1 MB/s |  35 MB     00:04     
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transactionPreparing        :                                                                                                            1/1 Installing       : mariadb-config-4:10.5.16-1.oe2203.x86_64                                                                   1/8 Installing       : mysql-common-8.0.28-1.oe2203.x86_64                                                                        2/8 Installing       : mysql-8.0.28-1.oe2203.x86_64                                                                               3/8 Installing       : mysql-errmsg-8.0.28-1.oe2203.x86_64                                                                        4/8 Installing       : protobuf-lite-3.14.0-6.oe2203.x86_64                                                                       5/8 Running scriptlet: mysql-selinux-1.0.0-2.oe2203.noarch                                                                        6/8 Installing       : mysql-selinux-1.0.0-2.oe2203.noarch                                                                        6/8 Running scriptlet: mysql-selinux-1.0.0-2.oe2203.noarch                                                                        6/8 
    libsemanage.semanage_direct_install_info: Overriding mysql module at lower priority 100 with module at priority 200.Installing       : mecab-0.996-2.oe2203.x86_64                                                                                7/8 Running scriptlet: mecab-0.996-2.oe2203.x86_64                                                                                7/8 Running scriptlet: mysql-server-8.0.28-1.oe2203.x86_64                                                                        8/8 Installing       : mysql-server-8.0.28-1.oe2203.x86_64                                                                        8/8 Running scriptlet: mysql-server-8.0.28-1.oe2203.x86_64                                                                        8/8 Running scriptlet: mysql-selinux-1.0.0-2.oe2203.noarch                                                                        8/8 Running scriptlet: mysql-server-8.0.28-1.oe2203.x86_64                                                                        8/8 
    /usr/lib/tmpfiles.d/pesign.conf:1: Line references path below legacy directory /var/run/, updating /var/run/pesign → /run/pesign; please update the tmpfiles.d/ drop-in file accordingly.Verifying        : mysql-8.0.28-1.oe2203.x86_64                                                                               1/8 Verifying        : mysql-common-8.0.28-1.oe2203.x86_64                                                                        2/8 Verifying        : mecab-0.996-2.oe2203.x86_64                                                                                3/8 Verifying        : mysql-errmsg-8.0.28-1.oe2203.x86_64                                                                        4/8 Verifying        : mysql-selinux-1.0.0-2.oe2203.noarch                                                                        5/8 Verifying        : mysql-server-8.0.28-1.oe2203.x86_64                                                                        6/8 Verifying        : mariadb-config-4:10.5.16-1.oe2203.x86_64                                                                   7/8 Verifying        : protobuf-lite-3.14.0-6.oe2203.x86_64                                                                       8/8 Installed:mariadb-config-4:10.5.16-1.oe2203.x86_64      mecab-0.996-2.oe2203.x86_64               mysql-8.0.28-1.oe2203.x86_64            mysql-common-8.0.28-1.oe2203.x86_64           mysql-errmsg-8.0.28-1.oe2203.x86_64       mysql-selinux-1.0.0-2.oe2203.noarch     mysql-server-8.0.28-1.oe2203.x86_64           protobuf-lite-3.14.0-6.oe2203.x86_64     Complete!
    [root@superman-21 ~]# 
    
  5. 查看安装后的rpm包。

    # rpm -qa | grep mysql
    

    例如示例命令如下:

    [root@superman-21 ~]# rpm -qa | grep mysql
    mysql-server-8.0.28-1.oe2203.x86_64
    mysql-selinux-1.0.0-2.oe2203.noarch
    mysql-errmsg-8.0.28-1.oe2203.x86_64
    mysql-common-8.0.28-1.oe2203.x86_64
    mysql-8.0.28-1.oe2203.x86_64
    [root@superman-21 ~]# 
    

60.2 运行

  1. 修改配置文件。

    1. 在root权限下创建my.cnf文件,其中文件路径(包括软件安装路径basedir、数据路径datadir等)根据实际情况修改。

      # vi /etc/my.cnf
      

      编辑my.cnf内容如下:

      log-error=/data/mysql/log/mysqlerr.log
      pid-file=/data/mysql/run/mysqld.pid
      [mysqldump]
      quick
      [mysql]
      no-auto-rehash
      [client]
      default-character-set=utf8
      [mysqld]
      basedir=/usr/share/mysql
      socket=/data/mysql/run/mysql.sock
      tmpdir=/data/mysql/tmp
      datadir=/data/mysql/data
      lc-messages-dir=/usr/share/mysql
      authentication_policy=mysql_native_password
      port=3306
      user=mysql
      
    2. 确保my.cnf配置文件修改正确。

      # cat /etc/my.cnf
      

      60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载 注意:
      其中basedir为软件安装路径,请根据实际情况修改。

      例如示例命令如下:

      [root@superman-21 ~]# cat /etc/my.cnf
      [mysqld_safe]
      log-error=/data/mysql/log/mysqlerr.log
      pid-file=/data/mysql/run/mysqld.pid
      [mysqldump]
      quick
      [mysql]
      no-auto-rehash
      [client]
      default-character-set=utf8
      [mysqld]
      basedir=/usr/share/mysql
      socket=/data/mysql/run/mysql.sock
      tmpdir=/data/mysql/tmp
      datadir=/data/mysql/data
      lc-messages-dir=/usr/share/mysql
      authentication_policy=mysql_native_password
      port=3306
      user=mysql
      [root@superman-21 ~]# 
      
    3. 设置socket软连接。

      # ln -s /data/mysql/run/mysql.sock /var/lib/mysql/mysql.sock
      

      例如示例命令如下:

      [root@superman-21 ~]# ln -s /data/mysql/run/mysql.sock /var/lib/mysql/mysql.sock
      [root@superman-21 ~]# 
      
  2. 在root权限下初始化数据库。

    60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载 说明:
    本步骤倒数第2行中有初始密码,请注意保存,登录数据库时需要使用。

    # mysqld --defaults-file=/etc/my.cnf --initialize
    

    例如示例命令如下:

    [root@superman-21 ~]# mysqld --defaults-file=/etc/my.cnf --initialize
    2022-12-21T11:46:52.711486Z 0 [System] [MY-013169] [Server] /usr/libexec/mysqld (mysqld 8.0.28) initializing of server in progress as process 9296
    2022-12-21T11:46:52.719415Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    2022-12-21T11:46:52.929564Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    2022-12-21T11:46:53.791786Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +KmMm/t/q8x4
    [root@superman-21 ~]# 
    

    查看打印信息,打印信息中包括“initializing of server has completed”表示初始化数据库完成,且打印信息中“A temporary password is generated for root@localhost: +KmMm/t/q8x4”的“+KmMm/t/q8x4”为初始密码。

  3. 启动数据库。

    60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载 注意:
    如果第一次启动数据库服务,以root用户启动数据库,则启动时会提示缺少mysql.log文件而导致失败。使用mysql用户启动之后,会在/data/mysql/log目录下生成mysql.log文件,再次使用root用户启动则不会报错。

    在root权限下启动MySQL数据库。

    # systemctl start mysqld 
    

    例如示例命令如下:

    [root@superman-21 ~]# systemctl start mysqld 
    [root@superman-21 ~]# 
    [root@superman-21 ~]# systemctl status mysqld
    ● mysqld.service - MySQL 8.0 database serverLoaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)Active: active (running) since Wed 2022-12-21 19:51:29 CST; 6s agoProcess: 9349 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)Process: 9372 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)Main PID: 9408 (mysqld)Status: "Server is operational"Tasks: 38 (limit: 21420)Memory: 362.6MCGroup: /system.slice/mysqld.service└─ 9408 /usr/libexec/mysqld --basedir=/usr1221 19:51:20 superman-21 systemd[1]: Starting MySQL 8.0 database server...
    1221 19:51:24 superman-21 mysqld[9408]: 2022-12-21T11:51:24.402543Z 0 [System] [MY-010116] [Server] /usr/libexec/mysqld (mysqld >
    1221 19:51:24 superman-21 mysqld[9408]: 2022-12-21T11:51:24.447428Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has st>
    1221 19:51:28 superman-21 mysqld[9408]: 2022-12-21T11:51:28.034838Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has en>
    1221 19:51:29 superman-21 mysqld[9408]: 2022-12-21T11:51:29.808010Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is se>
    1221 19:51:29 superman-21 mysqld[9408]: 2022-12-21T11:51:29.808074Z 0 [System] [MY-013602] [Server] Channel mysql_main configure>
    1221 19:51:29 superman-21 mysqld[9408]: 2022-12-21T11:51:29.830258Z 0 [System] [MY-010931] [Server] /usr/libexec/mysqld: ready f>
    1221 19:51:29 superman-21 mysqld[9408]: 2022-12-21T11:51:29.830320Z 0 [System] [MY-011323] [Server] X Plugin ready for connectio>
    1221 19:51:29 superman-21 systemd[1]: Started MySQL 8.0 database server.
    [root@superman-21 ~]#
    
  4. 登录数据库。

    60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载 说明:

    • 提示输入密码时,请输入2产生的初始密码。
    • 如果采用官网RPM安装方式,则mysql文件在/usr/bin目录下。登录数据库的命令根据实际情况修改。
    # mysql -uroot -p
    

    例如示例命令如下:

    [root@superman-21 ~]# mysql -uroot -p                                           
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \\g.
    Your MySQL connection id is 8
    Server version: 8.0.28Copyright (c) 2000, 2022, Oracle and/or its affiliates.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>
    
  5. 配置数据库帐号密码。

    1. 登录数据库以后,修改通过root用户登录数据库的密码。

      mysql> alter user 'root'@'localhost' identified by "123456";
      

      例如示例命令如下:

      mysql> alter user 'root'@'localhost' identified by "123456";
      Query OK, 0 rows affected (0.01 sec)mysql> 
      
    2. 创建全域root用户(允许root从其他服务器访问)。

      mysql> create user 'root'@'%' identified by '123456';
      

      例如示例命令如下:

      mysql> create user 'root'@'%' identified by '123456';
      Query OK, 0 rows affected (0.00 sec)mysql> 
      
    3. 进行授权。

      mysql> grant all privileges on *.* to 'root'@'%';
      mysql> flush privileges;
      

      例如示例命令如下:

      mysql> grant all privileges on *.* to 'root'@'%';
      Query OK, 0 rows affected (0.01 sec)mysql> flush privileges;
      Query OK, 0 rows affected (0.00 sec)mysql> 
      
  6. 退出数据库。

    执行 \\q 或者 exit 退出数据库。

    mysql> \\q
    mysql> exit
    

    例如示例命令如下:

    mysql> \\q
    Bye
    [root@superman-21 ~]# 
    mysql> exit
    Bye
    [root@superman-21 ~]# 
    

60.3 卸载

  1. 在root权限下关闭数据库进程。

    # systemctl stop mysqld
    

    例如示例命令如下:

    [root@superman-21 ~]# systemctl stop mysqld
    [root@superman-21 ~]# 
    
  2. 在root权限下执行dnf remove mysql命令卸载MySQL。

    # dnf remove mysql
    

    例如示例命令如下:

    [root@superman-21 ~]# dnf remove mysql
    Dependencies resolved.
    ====================================================================================================================================Package                          Architecture             Version                              Repository                     Size
    ====================================================================================================================================
    Removing:mysql                            x86_64                   8.0.28-1.oe2203                      @OS                            59 M
    Removing dependent packages:mysql-server                     x86_64                   8.0.28-1.oe2203                      @everything                   117 M
    Removing unused dependencies:mariadb-config                   x86_64                   4:10.5.16-1.oe2203                   @update                       345  mecab                            x86_64                   0.996-2.oe2203                       @everything                   2.1 Mmysql-common                     x86_64                   8.0.28-1.oe2203                      @OS                           185 kmysql-errmsg                     x86_64                   8.0.28-1.oe2203                      @everything                   9.1 Mmysql-selinux                    noarch                   1.0.0-2.oe2203                       @everything                    49 kprotobuf-lite                    x86_64                   3.14.0-6.oe2203                      @update                       779 kTransaction Summary
    ====================================================================================================================================
    Remove  8 PackagesFreed space: 188 M
    Is this ok [y/N]: y
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transactionPreparing        :                                                                                                            1/1 Running scriptlet: mysql-server-8.0.28-1.oe2203.x86_64                                                                        1/1 Running scriptlet: mysql-server-8.0.28-1.oe2203.x86_64                                                                        1/8 Erasing          : mysql-server-8.0.28-1.oe2203.x86_64                                                                        1/8 Running scriptlet: mysql-server-8.0.28-1.oe2203.x86_64                                                                        1/8 Erasing          : mysql-errmsg-8.0.28-1.oe2203.x86_64                                                                        2/8 Erasing          : mysql-8.0.28-1.oe2203.x86_64                                                                               3/8 Erasing          : mysql-common-8.0.28-1.oe2203.x86_64                                                                        4/8 Erasing          : mariadb-config-4:10.5.16-1.oe2203.x86_64                                                                   5/8 
    警告:/etc/my.cnf 已另存为 /etc/my.cnf.rpmsaveErasing          : mysql-selinux-1.0.0-2.oe2203.noarch                                                                        6/8 Running scriptlet: mysql-selinux-1.0.0-2.oe2203.noarch                                                                        6/8 
    libsemanage.semanage_direct_remove_key: mysql module at priority 100 is now active.Erasing          : mecab-0.996-2.oe2203.x86_64                                                                                7/8 Running scriptlet: mecab-0.996-2.oe2203.x86_64                                                                                7/8 Erasing          : protobuf-lite-3.14.0-6.oe2203.x86_64                                                                       8/8 Running scriptlet: protobuf-lite-3.14.0-6.oe2203.x86_64                                                                       8/8 Verifying        : mariadb-config-4:10.5.16-1.oe2203.x86_64                                                                   1/8 Verifying        : mecab-0.996-2.oe2203.x86_64                                                                                2/8 Verifying        : mysql-8.0.28-1.oe2203.x86_64                                                                               3/8 Verifying        : mysql-common-8.0.28-1.oe2203.x86_64                                                                        4/8 Verifying        : mysql-errmsg-8.0.28-1.oe2203.x86_64                                                                        5/8 Verifying        : mysql-selinux-1.0.0-2.oe2203.noarch                                                                        6/8 Verifying        : mysql-server-8.0.28-1.oe2203.x86_64                                                                        7/8 Verifying        : protobuf-lite-3.14.0-6.oe2203.x86_64                                                                       8/8 Removed:mariadb-config-4:10.5.16-1.oe2203.x86_64      mecab-0.996-2.oe2203.x86_64               mysql-8.0.28-1.oe2203.x86_64            mysql-common-8.0.28-1.oe2203.x86_64           mysql-errmsg-8.0.28-1.oe2203.x86_64       mysql-selinux-1.0.0-2.oe2203.noarch     mysql-server-8.0.28-1.oe2203.x86_64           protobuf-lite-3.14.0-6.oe2203.x86_64     Complete!
    [root@superman-21 ~]#
    

👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!

60 openEuler 22.03-LTS 搭建MySQL数据库服务器-安装、运行和卸载