CentOS安装MySQL

参考官方链接:https://dev.mysql.com/doc/refman/8.0/en/linux-installation-rpm.html

CentOS版本

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

下载MySQL安装包(版本:8.0.35)

  • 访问地址:https://dev.mysql.com/downloads/mysql/
  • 下载对应的安装包
    [root@localhost local]# cd /usr/local && mkdir mysql && cd mysql
    [root@localhost mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-server-8.0.35-1.el7.x86_64.rpm
    [root@localhost mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-client-8.0.35-1.el7.x86_64.rpm
    [root@localhost mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-client-plugins-8.0.35-1.el7.x86_64.rpm
    [root@localhost mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-common-8.0.35-1.el7.x86_64.rpm
    [root@localhost mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-icu-data-files-8.0.35-1.el7.x86_64.rpm
    [root@localhost mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-libs-8.0.35-1.el7.x86_64.rpm
    

按照顺序安装

[root@localhost mysql]# rpm -ivh mysql-community-common-8.0.35-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-icu-data-files-8.0.35-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-client-plugins-8.0.35-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-libs-8.0.35-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-client-8.0.35-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-server-8.0.35-1.el7.x86_64.rpm

可能在执行安装mysql-community-libs-8.0.35-1.el7.x86_64.rpm过程中出现以下错误:

[root@localhost mysql]# rpm -ivh mysql-community-libs-8.0.35-1.el7.x86_64.rpm
warning: mysql-community-libs-8.0.35-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
error: Failed dependencies:
        mariadb-libs is obsoleted by mysql-community-libs-8.0.35-1.el7.x86_64

则需要先卸载系统自带的mariadb-libs:

[root@localhost mysql]# yum remove mariadb-libs

查看mysqld运行状态

[root@localhost mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html

 发现mysql没有运行,则启动mysqld:

[root@localhost mysql]# systemctl start mysqld

          再次查看mysqld运行状态,发现已经成功运行:

[root@localhost mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-10-31 06:21:52 PDT; 2min 22s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 20965 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 21096 (mysqld)
   Status: "Server is operational"
    Tasks: 37
   CGroup: /system.slice/mysqld.service
           └─21096 /usr/sbin/mysqld

Oct 31 06:21:40 localhost.localdomain systemd[1]: Starting MySQL Server...
Oct 31 06:21:52 localhost.localdomain systemd[1]: Started MySQL Server.

           设置为开机启动项:

[root@localhost mysql]# systemctl enable mysqld

设置密码

  1. 获取初始密码
    [root@localhost mysql]# cat /var/log/mysqld.log  | grep temporary
    2023-10-31T13:21:46.161251Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: u.j.hqyqf1BW
    
  2. 登录mysql:使用初始化密码登录mysql
  3. 修改mysql密码(密码需要包含至少一个大写字母,一个小写字母,一个数字和一个特殊字符,密码长度至少为8位):
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
    Query OK, 0 rows affected (0.00 sec)
    
  4. 当然,在自己的开发机器上使用,没必要设置复杂的密码,那么我们可以降低密码验证强度,然后设置一个简单的密码
    mysql> SET GLOBAL validate_password.policy=LOW;
    Query OK, 0 rows affected (0.00 sec)
     当密码强度设置为LOW时,则只校验密码长度,此时可以设置一个较为简单的密码

设置其他IP可以访问数据库

mysql> UPDATE mysql.user SET host = '%' WHERE user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

               

你可能感兴趣的:(centos,mysql,linux)