mariadb

1.数据库部署

yum install mariadb-server -y
systemctl start mariadb

2.安全初始化

vim /etc/my.cnf
skip-networking=1     ##关闭数据库开启的网络接口;默认情况下,数据库网络接口是打开的,为了数据库的安全需要关闭网络接口

systemctl restart mariadb

测试:ss -antlupe

这里写图片描述

这里写图片描述

[root@localhost yum.repos.d]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   ##第一次修改密码时因不知道当前mysql的密码可直接按回车键
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y              ##是否要设定数据库超级用户密码
New password:                           ##输入要设定的超级用户密码
Re-enter new password:                  ##重新输入
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y     ##是否删除匿名用户访问权限
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y    ##是否禁止超级用户通过远程登陆
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y     ##刷新数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y     ##加载数据库中表格的权限
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

3.数据库的基本sql语句操作
(1)登陆

mysql -uroot -p  ##-u表示用户,后面紧接用户名;-p表示密码,后面可紧跟密码,但是容易造成服务器密码泄露,故建议使用mysql -uroot -p命令登陆服务器

这里写图片描述

(2)数据库及表的建立

CREATE DATABASE westos;              ##新建westos库
CREATE TABLE linux (                 ##新建表格linux
username varchar(15) not null,       ##表格linux的表头为username,字符长度为15,不能为空
password varchar(15) not null        ##表格linux的表头为password,字符长度为15,不能为空
 );
INSERT INTO linux VALUES ('user1','passwd1');  ##向表格linux插入用户为user1,密码为pasword1

这里写图片描述

这里写图片描述

(3)查询

SHOW DATABASES;             ##查询数据库
USE mysql                    ##进入数据库mysql
SHOW  TABLES;               ##显示数据库mysql中的表格
SELECT *  FROM westos;      ##查询user表中的所有内容(*可以用此表中的任何字段来代替)
DESC  linux;                ##查看数据库westos表格linux的表头 

这里写图片描述

(4)更新数据库信息

UPDATE  linux SET password='666' where username='li';      
##更新用户li的密码为666
UPDATE linux SET password='456' where ( username='user1' or username='user2' );
##更新user1和user2的密码
UPDATE linux SET password='666' where username='li' and age='22'; ##更新用户为liqie年纪为22的用户密码为666

这里写图片描述

DELETE FROM linux where username='li';               ##删除表格Linux中用户li的数据
ALTER TABLE linux ADD age varchar(4);                ##在表格linux中添加表头age
ALTER TABLE linux ADD class varchar(5) after password;   ##在表格linux的password后面添加class

这里写图片描述

(5)删除数据库

DELETE FROM linux where username='xu';   ##删除xu的数据从linux表中
DROP TABLE linux;                        ##删除linux表
DROP DATABASE westos;                    ##删除westos库

这里写图片描述

(6)数据库的备份

mysqldump -u root -predhat --all-database       
##备份所有表中的左右数据
mysqldump -u root -predhat --all-database --no-data 
##备份所有表,但不备份数据
mysqldump -u root -predhat westos                  ##备份westos库
mysqldump -u root -predhat westos  > /mnt/westos.sql    
##备份westos库并把数据保存到westos.sql中
mysqldump -uroot -predhat westos linux > /mnt/linux.sql 
##备份westos库中的linux表
mysqldump -uroot -predhat westos test > /mnt/test.sql   
##备份westos库中的test表
mysql -uroot -predhat -e "create database westos;"  ##建立westos库
mysql -uroot -predhat westos < /mnt/westos.sql 
##把数据导入westos库(westos库必须存在)

这里写图片描述

这里写图片描述

(7)用户授权

CREATE USER westos@'localhost' identified by 'westos';  
##添加本地用户
CREATE USER lee@'%' identified by 'lee';        
##建立用户lee,此用户可以通过网络登陆
SELECT Host,User FROM mysql.user;       ##查看mysql的用户
GRANT SELECT ON WESTOS.* TO westos@localhost;           
##授予本地用户在WESTOS库拥有SELECT权限
GRANT SELECT ON WESTOS.* TO westos@'%';
SHOW GRANTS FOR westos@localhost;                       
##查看本地用户westos的权限
SHOW GRANTS FOR westos@'%';
REVOKE SELECT ON WESTOS.* FROM westos@localhost;        
##去掉用户本地westos在WESTOS库的SELECT权限
drop user lee@'%'                        ##删除用户
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章