安装MySQL数据库
1、MySQL 的下载
下载地址:http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar
选择:mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar (也可以选择其他适合的版本)
2、Linux 平台下的安装
- 前提工作:卸载 mariadb
[root@mysql ~]# yum remove mysql-libs -y
- 解压缩 mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar :
[root@mysql ~]# tar -xvf mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar
mysql-community-embedded-devel-5.7.31-1.el7.x86_64.rpm
mysql-community-libs-5.7.31-1.el7.x86_64.rpm
mysql-community-client-5.7.31-1.el7.x86_64.rpm
mysql-community-server-5.7.31-1.el7.x86_64.rpm
mysql-community-embedded-5.7.31-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.31-1.el7.x86_64.rpm
mysql-community-common-5.7.31-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm
mysql-community-devel-5.7.31-1.el7.x86_64.rpm
mysql-community-test-5.7.31-1.el7.x86_64.rpm
- 安装 RPM 包:
因为包之间存在着一些依赖关系,所以需要按照一定的顺序安装:
-
mysql-community-common-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-common-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-common-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-common-5.7.31-1.e################################# [100%]
-
mysql-community-libs-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-libs-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-libs-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-libs-5.7.31-1.el7################################# [100%]
-
mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-libs-compat-5.7.3################################# [100%]
-
mysql-community-client-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-client-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-client-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-client-5.7.31-1.e################################# [100%]
-
mysql-community-server-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-server-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-server-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 错误:依赖检测失败: /usr/bin/perl 被 mysql-community-server-5.7.31-1.el7.x86_64 需要 net-tools 被 mysql-community-server-5.7.31-1.el7.x86_64 需要 perl(Getopt::Long) 被 mysql-community-server-5.7.31-1.el7.x86_64 需要 perl(strict) 被 mysql-community-server-5.7.31-1.el7.x86_64 需要
需要 perl 和 net-tools,安装即可:
[root@mysql ~]# yum install perl net-tools -y
依赖安装完成后,即可安装:
[root@mysql ~]# rpm -ivh mysql-community-server-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-server-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-server-5.7.31-1.e################################# [100%]
- 启动 MySQL:
[root@mysql ~]# systemctl start mysqld #启动服务
[root@mysql ~]# systemctl enable mysqld #开机自启
[root@mysql ~]# systemctl status mysqld #查看状态
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2020-09-21 20:12:15 CST; 15s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 3278 (mysqld)
CGroup: /system.slice/mysqld.service
└─3278 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mys...
9月 21 20:12:13 mysql systemd[1]: Starting MySQL Server...
9月 21 20:12:15 mysql systemd[1]: Started MySQL Server.
- 登录 MySQL:
MySQL 5.7 之后的版本在默认安装时去掉了 root 用户的空密码,初次登录时系统会生成一个临时 root 密码,可以通过 /var/log/mysqld.log 来查看:
[root@mysql ~]# cat /var/log/mysqld.log | grep "password"
2020-09-21T12:12:13.721817Z 1 [Note] A temporary password is generated for root@localhost: Qqfpoxet1+p(
# 初始密码为:Qqfpoxet1+p(
[root@mysql ~]# 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.31
Copyright (c) 2000, 2020, 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 代表了客户端命令,“-u” 后面接的是数据库用户,“-p” 表示需要输入的密码。同时,在欢迎界面还说明了以下几部分的内容:
- Commands end with ; or \g. 命令以 “;” 或 “\g” 来结束
- Your MySQL connection id is 2 客户端的连接 ID,这个数字记录了 MySQL 服务到目前为止的连接次数;每一个新连接都会自动加 1
- Server version: 5.7.31 MySQL 服务器的版本与类型
- Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement. 通过 “help;” 或 “\h” 来显示帮助内容;通过 “\c” 来清除命令行缓存
但临时密码无法进行大多数的操作,比如:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
#提示要使用 ALTER USER 来修改初始密码
- 修改密码:
mysql> alter user 'root'@'localhost' identified by 'Opfordream@0518';
Query OK, 0 rows affected (0.00 sec)
- 设置 root 用户可以远程登录:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Opfordream@0518' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
- 配置 MySQL:
[root@mysql ~]# vim /etc/my.cnf
#增加如下内容
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
[root@mysql ~]# systemctl restart mysqld #重启 MySQL
- 测试远程连接:
建议先关闭服务器端的防火墙:
[root@mysql ~]# systemctl stop firewalld
[root@mysql ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
或者开启 3306 端口:
[root@mysql ~]# firewall-cmd --add-port=3306/tcp --permanent
[root@mysql ~]# firewall-cmd --list-ports --permanent
[root@mysql ~]# firewall-cmd --reload
测试远程连接: