运维学习之mariadb数据库管理



mariadb

yum install mariadb-server -y

配置好yum源以后,查找并安装mariadb服务

运维学习之mariadb数据库管理

启动mariadb服务,并设置开机自启动

运维学习之mariadb数据库管理

vim /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

skip-networking=1##关闭数据库开启的网络接口

尝试打开数据库成功

运维学习之mariadb数据库管理

关闭数据库开启的网络接口

运维学习之mariadb数据库管理

重启服务,查看端口状态

运维学习之mariadb数据库管理

systemctl start mariadb

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): ##数据库原始密码(默认没有直接回车)

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] ##是否要设定数据库超级用户密码

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密码

运维学习之mariadb数据库管理

尝试用root身份登录成功

运维学习之mariadb数据库管理

数据库的基本sql语句操作

1.登陆

mysql -uroot -pwestos###-u表示指定登陆用户,-p 表示指定此用户密码

2.查询

show databases;##显示数据库

use mysql;##进入mysql库

show tables;##显示当前库中表的名称

select * from user;##查询user表中的所有内容(*可以用此表中的任何字段来代替)

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

select * from user where Host='127.0.0.1';##查询user表中地址为127.0.0.1的内容


运维学习之mariadb数据库管理

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理

3.数据库及表的建立

create database westos;

use westos;

create table linux(

username varchar(15) not null,

password varchar(15) not null,

age varchar(4)

);

insert into linux values ('user1','passwd1','age1'); 

insert into linux values ('lee','123','20');


创建westos库,并进入westos

运维学习之mariadb数据库管理

创建Linux数据表,加入表中信息

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理重命名Linux为message

运维学习之mariadb数据库管理

在表中添加class项,查看表格信息

运维学习之mariadb数据库管理

先删除以前的class,然后重新添加class到制定位置

运维学习之mariadb数据库管理

4.更新数据库信息

update linux set password=password('passwd2') where username=user1;

update linux set password=password('456') where ( username='user1' or username='user2' );

##更新user1和user2的密码

alter table linux rename message;#修改名称

delete from linux where username=user1;

alter table linux add class varchar(50);加入class列

alter table linux add age  varchar(5)  after name;在name后面加age列

alter table linux drop age;删除age列

update linux set class='linux';把class列更改为linux

update linux set class='java' where username='lee';把lee所在的行的linux内容改为java

运维学习之mariadb数据库管理运维学习之mariadb数据库管理

运维学习之mariadb数据库管理

5.删除数据库

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

drop table linux;##删除linux表

drop database westos;##删除westos库

运维学习之mariadb数据库管理6.数据库的备份

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

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

mysqldump -u root -pwestos westos##备份westos库

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

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

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

mysql -uroot -pwestos -e "create database westos;"##建立westos库

mysql -uroot -pwestos westos < /mnt/westos.sql##把数据导入westos库

重新创建westos数据库和westos中的Linux表,为后面实验用

运维学习之mariadb数据库管理

备份westos库中的信息到/mnt

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理

删除原有westos库

运维学习之mariadb数据库管理

恢复westos库及其所有信息

运维学习之mariadb数据库管理

7.用户授权

create user [email protected] identified by 'lee';##建立用户lee,此用户只能通过本机登陆

create user [email protected]'%' identified by 'lee';##建立用户lee,此用户可以通过网络登陆

grant insert,update,delete,select on westos.test to [email protected]; ##用户授权

grant select on westos.* to [email protected]'%'

show grants for [email protected]'%'##查看用户授权

show grants for [email protected]

revoke delete on westos.test  from [email protected];##去除用户授权权力

drop user [email protected]'%'##删除用户

添加用户lee并设定密码为lee

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理

查看lee被授权的功能

运维学习之mariadb数据库管理

给lee用户select权限

运维学习之mariadb数据库管理

测试权限是否生效

运维学习之mariadb数据库管理

删除用户(用root用户删除)lee,不能登陆

运维学习之mariadb数据库管理

8.密码修改

mysqladmin -uroot -pwestos password lee##修该超级用户密码

##当超级用户密码忘记

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##登陆测试

停止服务,开启mysql登陆接口但忽略授权表

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理

停止和mysql有关的进程,可以用密码登陆

运维学习之mariadb数据库管理

数据库的网页管理工具

1.安装

yum install httpd php php-mysql -y

systemctl start httpd 

systemctl enable httpd 

systemctl stop firewalld 

systemctl disable firewalld 

需要下载

phpMyAdmin-3.4.0-all-languages.tar.bz2

tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html

mv phpMyAdmin-3.4.0-all-languages/ mysqladmin

cd mysqladmin

cp -p  config.sample.inc.php config.inc.php

vim config.inc.php

17 $cfg['blowfish_secret'] = 'mysql'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

systemctl restart httpd 

测试:

访问

http://172.25.254.100/mysqladmin

安装所需软件

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理

解压软件包,并更名

运维学习之mariadb数据库管理

随意填入

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理


在主机进行登陆测试,成功!

运维学习之mariadb数据库管理

运维学习之mariadb数据库管理