linux之mariadb 数据库

1、环境设置

安装包: mariadb-server.x86_64
yum install mariadb-server.x86_64 #安装软件包
systemctl start mariadb #打开服务
systemctl enable mariadb.service #设置开机自启
systemctl status mariadb.service #查看状态
linux之mariadb 数据库_第1张图片
linux之mariadb 数据库_第2张图片
linux之mariadb 数据库_第3张图片

2. 安全密码设定

[root@zhang ~]# mysql_secure_installation        # 进入密码设置

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):          #之前没有密码
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]          #禁止匿名用户登录 默认禁止
 ... 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]       #禁止超级用户远程登录 默认禁止
 ... 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]           #默认删除测试数据库
 - 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]              #立即重新加载权限列表
 ... 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!
[root@zhang ~]# 

2 建立
show DATABASES; #列出库
SHOW DATABASES; # 查看库
CREATE DATABASE haha; # 新建一个库名称为haha
USE lala; # 进入新建的库
CREATE TABLE haha ( #在库中创建一个新的表
-> username varchar(20) not null, #表中包含的字段以及字段的要求
-> password varchar(30) not null
-> );
DESC toto; # 查看表结构
INSERT INTO hahaVALUES(‘nini’,‘123456’); #给表中插入内容
INSERT INTO haha VALUES(‘hao’,‘123456’);
SELECT * FROM toto;
SELECT username FROM toto; 查看表中某一字段的内容
linux之mariadb 数据库_第4张图片
linux之mariadb 数据库_第5张图片
linux之mariadb 数据库_第6张图片
3)更改

ALTER TABLE nini ADD class varchar(15) not null; #给表中添加一个字段,默认在最后添加。
ALTER TABLE nini DROP class; #移除表中某一字段
ALTER TABLEnini ADD class varchar(15) not null AFTER username;
在确定的位置添加某一字段
linux之mariadb 数据库_第7张图片
linux之mariadb 数据库_第8张图片
linux之mariadb 数据库_第9张图片
UPDATE nini SET password=(‘123’) ; #修改某一字段的值,该字段所有值都会改变
UPDATE nini SET password=(‘456’) WHERE username=‘zhang’; #在确定的位置修改
ALTER TABLE nini RENAME westos; #更改表的名称
linux之mariadb 数据库_第10张图片
linux之mariadb 数据库_第11张图片
linux之mariadb 数据库_第12张图片

4) 删除

DELETE FROM redhat WHERE username=‘zhang’; # 删除表中的某一行
DROP TABLE redhat; # 删除整个表
DROP DATABASE nini; #删除整个库
linux之mariadb 数据库_第13张图片
linux之mariadb 数据库_第14张图片
linux之mariadb 数据库_第15张图片

5)用户授权

CREATE USER wei@‘localhost’ identified by ‘123456’;
linux之mariadb 数据库_第16张图片
新建立的用户,虽然可以登陆该数据库,但是对数据库没有任何权限,看不到内容。
linux之mariadb 数据库_第17张图片
GRANT SELECT,INSERT ON wei. TO haha@localhost; # 进行授权
SHOW GRANTS FOR wei@localhost; #显示授给的权利*
linux之mariadb 数据库_第18张图片

[root@zhang ~]# mysql -uwei -p       #新建用户登陆
Enter password:       #密码123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;         #有查看权力
+--------------------+
| Database           |
+--------------------+
| information_schema |
| haha               |

REVOKE INSERT ON lala.* FROM TOTO@localhost; #权力回收**
DROP USER TOTO@localhost; #删除用户

4、数据库的备份

mysqldump -uroot -predhat haha > /opt/lala.sql #将库lala备份到/opt/lala.sql 文件
mysqldump -uroot -predhat haha --no-date # 对指定的库不备份数据只备份结构
mysqldump -uroot -predhat --all-database # 对所有的库进行备份
mysqldump -uroot -predhat --all-database --no-data # 对所有的库不备份数据只备份结构
然后删除数据库haha。

恢复方式1:
mysql -uroot -predhat -e “CREATE DATABASEhaha;” #在数据库中先新建一个库
mysql -uroot -predhat lala < /opt/lala.sql #再将备份的数据放进新建的库中

恢复方式2:
vim /opt/lala.sql #直接打开备份的库文件
CREATE DATABASEhaha;
USE lala;

mysql -uroot -predhat < /opt/lala.sql #由于已经在文件中指定出新建的库 直接将数据到进即可

5、修改root用户密码

1)知道密码需要进行修改时:
update mysql.user set Password=password(‘westos’) where User=‘root’;
进入数据库直接使用命令对密码进行修改
systemctl restart mariadb.service #重启服务

2)当忘记 密码的时候重新进行设置密码
systemctl stop mariadb.service #关闭数据库服务
mysqld_safe --skip-grant-tables & #跳过验证列表进行登录 并后台运行
mysql #使用该命令直接进入数据库
update mysql.user set Password=password(‘redhat’) where User=‘root’;
输入命令对超级用户密码进行设置

killall -9 mysql #结束进程 相当于关闭服务
ps aux | grep mysql #查看没有结束的进程
kill -9 11383 #一次关闭所有没有停止的进程
systemctl start mariadb.service #开启服务
mysql -uroot -predhat #使用新密码成功登陆

你可能感兴趣的:(linux之mariadb 数据库)