Linux--RH254---unit 8 mariadb数据库

来源:互联网 发布:影音后期制作软件大全 编辑:程序博客网 时间:2024/05/18 20:34
unit 8 Mariadb

一、数据库的基本sql语句操作
1.安装

yum install mariadb-server -y      ###安装mariadb


systemctl start mariadb            ###启动mariadb服务
netstat -antlpe | grep mysql       ###校验mariadb的监听端口
vim /etc/my.cnf                    ###编辑/etc/my.cnf文件

skip-networking=1                  ###阻断监听

netstat -antlpe | grep mysql       ###mariadb的监听端口不存在,此时只允许通过套接字文件进行本地连接,阻断所有来自网络的tcp/ip连接。



mysql_secure_installation          ###第一次安装mysql以后通过这条命令可以对mysql进行设置

Enter current password for root (enter for none) 按Enter
Set root password? [Y/n] y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y



2.登陆
mysql -uroot -pwestos                   ###u表示指定登陆用户,p表示指定此用户密码(密码为明文)
mysql -uroot -p                         ###密码为加密

Enter password:



3.查询

MariaDB [(none)]> show databases;       ###显示数据库


MariaDB [(none)]> use mysql;            ###进入mtsql库


MariaDB [mysql]> show tables;           ###显示当前库中表的名字


MariaDB [mysql]> select * from user;    ###查询user表中的所有内容(*可以用此表中的任何字段来代替,多个字段用逗号分隔)


MariaDB [mysql]> desc user;             ###查询user表的结构(显示所有字段的名称)


MariaDB [mysql]> quit                   ###退出

4.数据库及表的建立

create database westos;          ###创建westos库


create table linux(         ###创建linux表,含有username,password两个字段

-> username varchar(15) not null,   ###添加username字段,字符长度最大15,且不能为空
-> password varchar(15) not null    ###添加password字段,字符长度最大15,且不能为空

-> );


insert into linux values ('user1','123');              ###在linux表中插入数据,username字段数据为user1,password字段数据为123

insert into linux values ('user1',password('123'));   ###插入password字段的数据是用password加密过的



5.更新数据库信息
update linux set password=password('456') where username='user2';                           ###更新user2的密码

update linux set password=password('456') where ( username='user1' or username='user2' );  ###更新user1和user2的密码


delete from linux where username='user3';                                                   ###删除user3的信息

alter table linux add age varchar(4);                                                      ###添加age字段到linux表的最后一列

alter table linux add class varchar(4) after password;                                       ###添加class字段到password字段后


alter table linux drop age;                                                                  ###删除age字段



6.删除数据库

delete from linux where username='user2';     ###从linux表中删除user2的数据


drop table linux;                             ###删除linux表


drop database westos;                         ###删除westos库



7.数据库的备份    

mysqldump -uroot -pwestos --all-database                ###备份所有表中的左右数据


mysqldump -uroot -pwestos --all-database --no-data      ###备份所有表,但不备份数据


mysqldump -uroot -pwestos westos                        ###备份westos库


mysqldump -uroot -pwestos westos > /mnt/westos.sql      ###备份westos库并把数据保存到westos.sql中

mysqldump -uroot -pwestos westos linux > /mnt/linux.sql ###备份westos库中的linux表


mysql -uroot -pwestos -e "create database westos;"      ###建立westos库
mysql -uroot -pwestos westos < /mnt/westos.sql      ###把数据导入westos库

mysql -uroot -pwestos -e "select * from westos.linux;"  ###查看westos库中linux表所有内容



8.用户授权
mysql -uroot -pwestos
select User,Host from mysql.user;                    ###查看mysql库中user表的User,Host字段
create user redhat@localhost identified by 'redhat';                   ###建立用户redhat,此用户只能通过本机登陆

create user redhat@'%' identified by 'redhat';                         ###建立用户redhat,此用户可以通过网络登陆


mysql -uredhat -predhat -h localhost                                   ###本机登陆


vim /etc/my.cnf                                                        ###更改配置文件
skip-networking=0                                                      ###开启监听端口,此时本地连接和网络连接皆可
systemctl restart mariadb                                              ###重启mariadb

mysql -uredhat -predhat -h 172.25.254.131                              ###网络登陆


mysql -uroot -pwestos
grant insert,update,delete,select on westos.linux to redhat@localhost; ###本机登陆用户授权
grant select on westos.linux to redhat@'%';                            ###网络登陆用户授权

show grants for redhat@localhost;                                       ###查看用户授权


revoke delete on westos.linux from redhat@localhost;                   ###删去用户授权权力


drop user redhat@'%';                                                  ###删除用户



9.密码修改

mysqladmin -uroot -pwestos password linux   ##在知道密码的情况下修改超级用户密码


##当超级用户密码忘记
systemctl stop mariadb                      ##关闭Screenshot from 2017-05-16 12-29-41
mysqld_safe --skip-grant-tables &           ##开启mysql登陆接口并忽略授权表
mysql                                       ##直接登陆不用密码

update mysql.user set Password=password('123') where User='root';  ##更新超级用户密码信息


ps aux | grep mysql         ##过滤mysql的所有进程
kill -9 mysqlpid            ##结束进程
systemctl start mariadb     ##重新开启mysql

mysql -uroot -p123          ##登陆测试



二、数据库的网页管理工具
1.安装

yum install httpd php php-mysql -y      ##安装httpd php php-mysql


systemctl start httpd                   ##开启httpd

systemctl enable httpd                  ##开机自启动
systemctl stop firewalld                ##关闭防火墙

systemctl disable firewalld             ##开机自关闭


tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/  ##解压phpMyAdmin-3.4.0-all-languages.tar.bz2到/var/www/html/
cd /var/www/html/                       ##切换至/var/www/html/               
mv phpMyAdmin-3.4.0-all-languages/ mysqladmin    ##移动phpMyAdmin-3.4.0-all-languages下所有文件至mysqladmin
cd mysqladmin                           ##切换至mysqladmin
cp -p config.sample.inc.php config.inc.php   ##添加配置文件
vim config.inc.php                      ##修改配置文件
$cfg['blowfish_secret'] = 'mysql'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

systemctl restart httpd                 ##重启httpd



2.访问测试:172.25.254.131/mysqladmin


数据库及表的建立:

数据库-->新建数据库redhat-->创建     ##创建redhat库



点击redhat库-->新建linux表,字段2-->执行   ##创建linux表


字段user、password--类型VARCHAR--长度15--保存   ##添加linux表的字段


数据库及表的删除:

点击redhat库-->点击linux2表后面的删除-->DROP TABLE linux2(确定)  ##删除linux2表



数据库-->勾上redhat-->点击删除-->DROP DATABASE 'redhat'(是)   ##删除redhat库



原创粉丝点击