centos8安装mysql5.7
前言
Centos8默认是安装MySQL8的,因为Centos8的AppStream仓库只包含MySQL8的包。由于MySQL版本不兼容所以我们需要安装MySQL5.7
添加MySQL5.7仓库
关闭Centos8中MySQL默认的AppStream仓库:
[root@localhost ~]# sudo dnf remove @mysql
Last metadata expiration check: 6:01:29 ago on Wed 28 Apr 2021 04:02:00 PM CST.
Unable to match profile in argument mysql
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost ~]# sudo dnf module reset mysql && sudo dnf module disable mysql
Last metadata expiration check: 6:01:37 ago on Wed 28 Apr 2021 04:02:00 PM CST.
Dependencies resolved.
Nothing to do.
Complete!
Last metadata expiration check: 6:01:38 ago on Wed 28 Apr 2021 04:02:00 PM CST.
Dependencies resolved.
==================================================================
Package Architecture Version Repository Size
==================================================================
Disabling modules:
mysql
Transaction Summary
==================================================================
Is this ok [y/N]: y
Complete!
创建一个新的仓库文件:
[root@localhost ~]# sudo vi /etc/yum.repos.d/mysql-community.repo
然后将以下内容粘贴到新建的仓库文件中:
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=0
[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=0
开始安装MySQL5.7
安装MySQL5.7:
[root@localhost ~]# sudo dnf --enablerepo=mysql57-community install mysql-community-server
//安装过程省略
第一次安装好MySQL5.7后,需要开启服务,设置开机自动启动:
[root@localhost ~]# sudo systemctl enable --now mysqld.service
然后获取mysql初始密码,用于后续安装配置操作
[root@localhost ~]# grep 'A temporary password' /var/log/mysqld.lo
你会发现,最后会有一个红色的temporary password。冒号后面的就是临时密码
使用获取到的临时密码登录mysql
[root@localhost ~]# mysql -uroot -p
Enter password: //此处输入密码,可以直接复制你的密码粘贴至此处,也可手动输入
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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> //看到有这样的标识符则表示成功登录了
修改mysql登录密码
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '输入你要设置的密码';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
数据库操作
数据库连接
[root@localhost ~]# mysql -uroot -p
Enter password: //输入你刚刚设置的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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> //登录成功
查看当前实例有哪些数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
创建数据库和表
mysql> CREATE DATABASE bwxh; //创建数据库bwxh
Query OK, 1 row affected (0.00 sec)
mysql> show databases; //查看当前实例有哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| bwxh |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use bwxh; //进入bwxh数据库
Database changed
mysql> CREATE TABLE school (id int NOT NULL,name VARCHAR(100) NOT NULL,,age tinyint); //创建school表
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW TABLES; //查看当前数据库有哪些表
+----------------+
| Tables_in_bwxh |
+----------------+
| school |
+----------------+
1 row in set (0.00 sec)
删除表和数据库
mysql> use bwxh; //进入bwxh数据库
Database changed
mysql> SHOW TABLES; //查看当前数据库有哪些表
+----------------+
| Tables_in_bwxh |
+----------------+
| school |
+----------------+
1 row in set (0.00 sec)
mysql> DROP TABLE school; //删除school表
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW TABLES; //查看当前数据库有哪些表
Empty set (0.00 sec) //可以看到已经没了证明删除成功
mysql> SHOW DATABASES; //查看当前实例有哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| bwxh |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> DROP DATABASE bwxh; //删除数据库bwxh
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW DATABASES; //查看当前实例有哪些数据库,可以看到bwxh这个数据库已经没了
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)