CentOS 7.6 安装与配置 MySql 5.7.40

Posted 程序员大宝(coder-dabao)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 7.6 安装与配置 MySql 5.7.40相关的知识,希望对你有一定的参考价值。

1 通过wget下载mysql的rpm

# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

2 检查是否存在MySql的repo

# cd /etc/yum.repos.d/
# ls
CentOS-Base.repo  CentOS-Epel.repo

可以看到没有MySql的repo

3 安装MySql的repo,并查看是否安装成功

安装

# rpm -ivh /tmp/mysql57-community-release-el7-9.noarch.rpm

检查

[root@VM-12-7-centos yum.repos.d]# cd /etc/yum.repos.d/
[root@VM-12-7-centos yum.repos.d]# ll
总用量 16
-rw-r--r-- 1 root root  614 12月 25 2021 CentOS-Base.repo
-rw-r--r-- 1 root root  230 12月 25 2021 CentOS-Epel.repo
-rw-r--r-- 1 root root 1416 9月  12 2016 mysql-community.repo
-rw-r--r-- 1 root root 1440 9月  12 2016 mysql-community-source.repo
[root@VM-12-7-centos yum.repos.d]#

4 查看系统原来是否存在MySql包

[root@VM-12-7-centos yum.repos.d]# rpm -qa | grep mysql
mysql57-community-release-el7-9.noarch
[root@VM-12-7-centos yum.repos.d]#

可以看到只有我们刚才安装的包,没有其他的mysql包

5 安装MySql

忽略GPG检查。否则会报错。

# yum install -y mysql-server --nogpgcheck

6 修改数据目录

# mkdir -p /data/apps/mysql

vi /etc/my.cnf
将datadir的值从/var/lib/mysql变成自己的数据目录/data/apps/mysql

datadir=/data/apps/mysql

7 启动MySql服务并查看启动状态

7.1 启动

#  systemctl start mysqld

7.2 状态

# systemctl status mysqld

7.3 停止

# systemctl stop mysqld

7.4 开机启动

# systemctl enable mysqld
# systemctl daemon-reload

8 初始密码

# grep "password" /var/log/mysqld.log

9 登录与重置密码

登录

# mysql -uroot -p

重置密码

mysql> alter user 'root'@'localhost' identified by "HaiMy$2022****";
mysql> flush privileges;

10 开放访问权限

1)公网账号(%:全部ip)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxxx' WITH GRANT OPTION;
mysql> flush privileges;

mysql> grant ALTER,CREATE,DELETE,INDEX,INSERT,SELECT,UPDATE on *.* to 'ww_admin'@'%' IDENTIFIED BY 'b@xxxx555666' WITH GRANT OPTION;
mysql> flush privileges;

mysql> grant SELECT on *.* to 'ww_select'@'%' IDENTIFIED BY 'b@xxxx555888' WITH GRANT OPTION;
mysql> flush privileges;

2)内网账号(10.0.%开头的ip)

mysql> grant DELETE,INSERT,SELECT,UPDATE on *.* to 'nw_rw'@'10.0.%' IDENTIFIED BY 'b@xxxx555111' WITH GRANT OPTION;
mysql> flush privileges;

mysql> grant SELECT,INSERT,DELETE,UPDATE on `nacos`.* to 'nacos'@'10.0.%' IDENTIFIED BY 'b@xxxx555222' WITH GRANT OPTION;
mysql> flush privileges;

11 创建数据库和表

11.1 创建数据库

创建user数据库。字符集:utf8mb4。排序规则:utf8mb4_unicode_ci

mysql> create database user default character set utf8mb4 collate utf8mb4_unicode_ci;

查看数据库

mysql> show create database user\\G;
*************************** 1. row ***************************
       Database: user
Create Database: CREATE DATABASE `user` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */
1 row in set (0.04 sec)

11.2 创建表

创建表user_info

Create Table: CREATE TABLE `user_info` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键id',
  `user_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '用户id',
  `platform` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '平台id',
  `avatar_url` varchar(1024) NOT NULL DEFAULT '' COMMENT '头像',
  `nick_name` varchar(200) NOT NULL DEFAULT '' COMMENT '用户昵称',
  `gender` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户性别。0 未知;1 男 2 女',
  `birthday` varchar(10) NOT NULL DEFAULT '' COMMENT '生日',
  `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态 1 正常',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户信息表'

12 查看版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.40    |
+-----------+
1 row in set (0.00 sec)

mysql>

13 参考文档

1、《腾讯云轻量服务器Centos 7.6通过rpm安装MySql 5.7.37》:https://blog.csdn.net/cssweb_sh/article/details/123599846
2、《【腾讯云】CentOS7.6系统环境下安装并配置MySQL5.7,实现远程登录》:https://blog.csdn.net/weixin_43805939/article/details/127534346

以上是关于CentOS 7.6 安装与配置 MySql 5.7.40的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7.6 安装与配置 MySql 5.7.40

CentOS 7中源码安装MySQL 5.7.6

CentOS 7.6安装 MySQL8.0.25

CentOS 7.6安装 MongoDB 5.0.2

CentOS 7.6 Docker服务的安装与配置

CentOS 7.6安装配置Chrony同步系统时钟