題目:
管理員準備在MySQL資料庫中創建課程管理網站需要的資料庫(couman)和相關表,資料表包括學生資訊表(student)、課程描述表(course)、學生選課表(stucou),各個表的結構如下:
學生資訊表(student)欄位:學號(sno)、姓名:(sname)、性別(sex)、班級(class),其中序號(sid)欄位為主鍵;
學生資訊表(course)欄位:學號(cno)、姓名:(cname),其中序號(cid)欄位為主鍵;
學生資訊表(stucou)欄位:學號(sno)、姓名:(cno)、成績(score),其中序號(scid)欄位為主鍵,學號(sno)和課程號(cno)為外鍵;
資料表創建成功后,向每個表中至少插入三條合法記錄;
賦予guestuser1用戶對資料庫的遠程登錄、資料查詢(select)權限;
賦予guestuser2用戶對資料庫的遠程登錄、資料庫(couman)權限;
賦予guestuser3用戶對資料庫的遠程登錄、資料的增、刪、改、查(insert,delete,update,select)權限;
賦予guestuser4用戶對資料庫的所有權限;
同時管理員對couman資料庫進行備份,
代碼結果如下:
一、安裝MySQL資料庫
- 下載yum源
yum -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
- 檢查MySql的YUM倉庫安裝是否正確
yum repolist enable|grep "mysql"
- 將自帶的資料庫卸載
yum -y remove mariadb-libs
- 安裝MySql服務器
yum install mysql-community-server
二、修改MySQL服務器密碼
- 開啟MySql服務器
systemctl start mysqld
- 查看MySql服務器中root用戶的默認密碼
cat /var/log/mysqld.log | grep password
- 使用默認密碼登錄MySQL服務器
Mysql -u root -p
-
輸入默認密碼
-
修改root登錄MySQL服務器的密碼
alter user 'root'@'localhost' identified with mysql_native_password by "Mysql123!";
三、創建資料庫
- 創建couman資料庫
create database couman;
- 查看所有資料庫
show database;
- 選擇couman資料庫
use couman;
- 創建資料表:學生資訊表(student)
create table student (
sno varchar(10) not null,
sname varchar(50) not null,
sex int(10) not null,
class varchar(20) not null,
primary key(sno)
);
- 查看student資料表結構
describe student;
- 創建資料表:描述表(course)
create table course(
cno varchar(10) not null,
cname varchar(50) not null,
primary key(cno)
);
- 查看course資料表結構
describe course;
- 創建資料表:學生選課表(stucou)
create table stucou(
scid int(10) not null auto_increment,
sno varchar(10) not null,
cno varchar(10) not null,
score int(5),
primary key(scid),
constraint foreign key(sno) references student(sno),
constraint foreign key(cno) references course(cno)
);
- 查看stucou資料表結構
describe stucou;
- 向這三個表中分別插入三條資料
insert into student(sno,sname,sex,class) values('1001','zhangsan',1,'a1');
insert into student(sno,sname,sex,class) values('1002','zhangsi',2,'a1');
insert into student(sno,sname,sex,class) values('1003','zhangwu',1,'a1');
insert into course(cno,cname) values('4','lisan');
insert into course(cno,cname) values('5','lisi');
insert into course(cno,cname) values('6','liwu');
insert into stucou(1,'1001','4',7);
insert into stucou(2,'1002','5',8);
insert into stucou(3,'1003','6',9);
- 退出MySQL
exit;
四、創建用戶并賦權
- 創建用戶
create user 'guestuser1'@'localhost' identified by 'Mysql123!';
create user 'guestuser2'@'localhost' identified by 'Mysql123!';
create user 'guestuser3'@'localhost' identified by 'Mysql123!';
create user 'guestuser4'@'localhost' identified by 'Mysql123!';
- 賦予guestuser1對資料庫遠程登錄,資料查詢(select)權限
grant select on *.* to 'guestuser1'@'%' identified by 'Mysql123!' with grant option;
- 賦予guestuser2對資料庫遠程登錄,資料庫(couman)的所有權限
grant all privileges on couman.* to 'guestuser2'@'%’ identified by 'Mysql123!' with grant option;
- 賦予guestuser3對資料庫遠程登錄,資料的增,刪,改,查(insert,delete,update,select)權限
grant insert,delete,update,select on *.* to 'guestuser3'@'%' identified by 'Mysql123!' with grant option;
- 賦予guestuser4對資料庫的所有權限
grant all privileges on *.* to 'guestuser4'@'%' identified by 'Mysql123!' with grant option;
- 退出資料庫
quit;
五、備份資料庫
- 備份資料庫
cd /user/bin
mysqldump -u root -p couman>couman.sql
- 輸入密碼
Mysql123!
- 查看已備份的資料庫
cd /user/bin
ls
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/276263.html
標籤:其他
上一篇:資料庫備份
下一篇:求助大佬們!!!