CentOS 7.x 安装 MySQL 并修改外网访问权限

CentOS 7.x 安装 MySQL 并修改外网访问权限

  1. CentOS 7.x 默认是没有 MySQL ,默认是 MariaDB 。所以我们要删除 MariaDB ,来为 MySQL 扫清障碍

    • 查询小海豹(mariadb)是否存在

      [root@bogon ~]# rpm -qa|grep mariadb
      mariadb-libs-5.5.68-1.el7.x86_64
      
    • 如果有就删除小海豹(mariadb)

      [root@bogon opt]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
      
  2. 官网下载 MySQL 5.7 的 rpm 包

    https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.34-1.el7.x86_64.rpm-bundle.tar

    • 解压 tar 压缩包
    [root@bogon opt]# tar -xvf mysql-5.7.34-1.el7.x86_64.rpm-bundle.tar 
    mysql-community-client-5.7.34-1.el7.x86_64.rpm
    mysql-community-common-5.7.34-1.el7.x86_64.rpm
    mysql-community-devel-5.7.34-1.el7.x86_64.rpm
    mysql-community-embedded-5.7.34-1.el7.x86_64.rpm
    mysql-community-embedded-compat-5.7.34-1.el7.x86_64.rpm
    mysql-community-embedded-devel-5.7.34-1.el7.x86_64.rpm
    mysql-community-libs-5.7.34-1.el7.x86_64.rpm
    mysql-community-libs-compat-5.7.34-1.el7.x86_64.rpm
    mysql-community-server-5.7.34-1.el7.x86_64.rpm
    mysql-community-test-5.7.34-1.el7.x86_64.rpm
    
    
  3. 安装 mysql-community-servermysql-community-client

    • 安装 mysql-community-server

      [root@bogon opt]# rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm --nodeps --force
      警告:mysql-community-server-5.7.34-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
      准备中...                          ################################# [100%]
      正在升级/安装...
         1:mysql-community-server-5.7.34-1.e################################# [100%]
      
    • 安装 mysql-community-client

      [root@bogon opt]# rpm -ivh mysql-community-client-5.7.34-1.el7.x86_64.rpm --nodeps --force
      警告:mysql-community-client-5.7.34-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
      准备中...                          ################################# [100%]
      正在升级/安装...
         1:mysql-community-client-5.7.34-1.e################################# [100%]
      
      
  4. 启动 mysql-community-server 和配置 开机自启

    • 启动

      [root@bogon opt]# systemctl start mysqld.service
      [root@bogon opt]# systemctl status mysqld.service 
      ● mysqld.service - MySQL Server
         Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
         Active: active (running) since 三 2021-05-12 12:44:31 CST; 18s ago
           Docs: man:mysqld(8)
                 http://dev.mysql.com/doc/refman/en/using-systemd.html
        Process: 9454 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
        Process: 9405 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
       Main PID: 9457 (mysqld)
         CGroup: /system.slice/mysqld.service
                 └─9457 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
      
      5月 12 12:44:25 bogon systemd[1]: Starting MySQL Server...
      5月 12 12:44:31 bogon systemd[1]: Started MySQL Server.
      
    • 开机自启

      [root@bogon opt]# systemctl enable mysqld.service
      
  5. 查看 root 密码 并且修改密码

    • 查看 root 密码用来登录

      [root@bogon opt]# cat| grep "password" /var/log/mysqld.log 
      2021-05-12T04:44:28.941775Z 1 [Note] A temporary password is generated for root@localhost: q/PVUekws8mf
      
    • 登录账号并修改 root 密码

      [root@bogon opt]# mysql -uroot -p
      Enter password:
      
      mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'G36syzy~Xlhtltq';
      Query OK, 0 rows affected (0.08 sec)
      
  6. 修改 root 的外网访问

    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
    mysql> flush privileges;
    

    有时候还是访问不了这时就还需要打开 centos的 3306 防火墙

    [root@bogon opt]# firewall-cmd --permanent --add-port=3306/tcp
    success
    [root@bogon opt]# firewall-cmd --reload
    success
    
  7. 访问成功

  8. 修改创建数据库的默认编码

    • 查看当前数据库默认编码

      mysql> status
      --------------
      mysql  Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using  EditLine wrapper
      
      Connection id:		15
      Current database:	
      Current user:		root@localhost
      SSL:			Not in use
      Current pager:		stdout
      Using outfile:		''
      Using delimiter:	;
      Server version:		5.7.34 MySQL Community Server (GPL)
      Protocol version:	10
      Connection:		Localhost via UNIX socket
      # 此处不是 utf8 所以要改成 utf8
      Server characterset:	latin1
      Db     characterset:	latin1
      Client characterset:	utf8
      Conn.  characterset:	utf8
      UNIX socket:		/var/lib/mysql/mysql.sock
      Uptime:			5 hours 25 min 17 sec
      
      Threads: 3  Questions: 158  Slow queries: 0  Opens: 152  Flush tables: 1  Open tables: 145  Queries per second avg: 0.008
      --------------
      
      
    • 修改配置为 utf8

      [root@bogon opt]# vi /etc/my.cnf
      
      [client]
      default-character-set=utf8mb4
      [mysql]
      default-character-set=utf8mb4
      [mysqld]
      character-set-server=utf8mb4
      collation-server=utf8mb4_general_ci
      
    • 重新启动数据库

      [root@bogon opt]# systemctl restart mysqld.server
      
# Linux  yum  Mysql 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×