一、安装MySQL数据库
linux—yum安装指定版本mysql
1、下载mysql rpm 包
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
2、安装下载的rpm包
yum localinstall mysql80-community-release-el7-3.noarch.rpm
或
sudo rpm-Uvh mysql80-community-release-el6-n.noarch.rpm
3、查看yum存储库中的mysql版本,默认最新的enabled,需要改成需要的版本
yum repolist all | grep mysql
手动编辑
vi /etc/yum.repos.d/mysql-community.repo
文件来选择自己需要的版本进行安装,把版本的enabled=1改为0,需要版本的enabled=1 ,注:对应的gpgcheck的值也需要改为0
4、安装mysql
yum install mysql-community-server
systemctl start mysqld
5、mysql第一次启动自动生成临时密码,可通过查看日志找到
grep ‘temporary password’ /var/log/mysqld.log
注:如果mysqld.log日志里面的时间不对,修改my.cnf文件在【mysqld】下增加log_timestamps=system保存后重启MySQL服务
6、第一次登录数据库操作
#登录数据库
[root@mha-master ~]# mysql -uroot -p'8ql6,yhY'
#设置全局密码策略=0
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
#验证全局密码长度=1
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.01 sec)
#第一次登录需要重置密码
mysql> alter user 'root'@'localhost' identified by '8ql6,yhY';
Query OK, 0 rows affected (5.00 sec)
#刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
#外部授权,使用Navicat连接
mysql> select * from user;
ERROR 1046 (3D000): No database selected
mysql> create user 'root'@'%' identified by '8ql6,yhY' ;
Query OK, 0 rows affected (0.03 sec)
mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
7、创建数据库、表添加数据
#创建数据库
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
#查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
#切换库
mysql> use test;
Database changed
#查看当前所在库
mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
8、插入数据
#新建表
create table wqq(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male',
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int,
depart_id int
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
#表插入数据
insert into t2(name,sex,age,hire_date,post,salary,office,depart_id) values
('小信信','male',18,'20170301','宇宙驻地球办事处外交大使',7300.33,401,1), #以下是系统部,全都是老师
('小妍','male',78,'20230101','teacher',10001.31,401,1),
('小霏','male',81,'20130305','teacher',8300,401,1),
('小久','male',73,'20230101','teacher',3500,401,1),
('小芸','male',28,'20121101','teacher',2100,401,1),
('小沫','female',18,'20230101','teacher',9010,401,1),
('小珠','male',18,'19000301','teacher',30000,401,1),
('小灵','female',48,'20230101','teacher',10000,401,1),
('小珊','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('小彤','female',38,'20101101','sale',2000.35,402,2),
('小莹','female',18,'20230101','sale',1000.37,402,2),
('小星','female',18,'20160513','sale',3000.29,402,2),
('小鸭','female',28,'20230101','sale',4000.33,402,2),
('小钢','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('小金','male',18,'19970312','operation',20000,403,3),
('小银','female',18,'20230101','operation',19000,403,3),
('小铜','male',18,'20230101','operation',18000,403,3),
('小铁','female',18,'20140512','operation',17000,403,3);