运维学习之mariadb数据库管理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1010256340/article/details/77186146


mariadb

yum install mariadb-server -y

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

wKioL1mPHd-jcpSaAAEMTFQGRJU928.png

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

wKioL1mPHeKixoFnAABOy7yI3WY386.png

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##关闭数据库开启的网络接口

尝试打开数据库成功

wKioL1mPH0-gpThlAADJRlfT7ug586.png

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

wKioL1mPH1OCboStAACRyUvq8k0088.png

重启服务,查看端口状态

wKiom1mPH1XzVYrqAAAy1VUFe4A803.png

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

wKiom1mPIjCDYq7_AAEs-ETkWVU378.png

尝试用root身份登录成功

wKioL1mPIjHjwyTXAABxzHpLDGw991.png

数据库的基本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的内容


wKiom1mPI43wARwAAADN5sxmmjw330.png

wKiom1mPI47DVpstAADPe8awUI4998.png

wKioL1mPI47TG9ouAAGIsHwupaA015.png

wKioL1mPI6HhSZzOAAC_L_JoDig105.png

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

wKiom1mPJC7CLBtqAACCOE9sG7Y004.png

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

wKiom1mPJC_TcZawAAERz5VwY30035.png

wKioL1mPJDDwPfIsAACixcUVAwk510.png重命名Linux为message

wKioL1mPJDCiNiWvAACLC-t3td0731.png

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

wKiom1mPJDGAX0dPAACMYwWxhv0491.png

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

wKiom1mPJDKBkdgdAADJ3Sr9ABQ940.png

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

wKioL1mPJwawpBq0AABmfkNUuqU132.pngwKiom1mPJwezLRtyAAEchPH-Oe0871.png

wKiom1mPJwiz4UKjAACp6Xq-HiM395.png

5.删除数据库

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

drop table linux;##删除linux表

drop database westos;##删除westos库

wKiom1mPJzyzP3LaAACp6Xq-HiM132.png6.数据库的备份

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表,为后面实验用

wKiom1mPJ7Cxn4cNAADyAZGLcoc418.png

备份westos库中的信息到/mnt

wKioL1mPJ7CS69wfAAA6PmbIGp8309.png

wKiom1mPJ7HBc4MHAAE6dhZ5b1U084.png

删除原有westos库

wKioL1mPJ7KjG5_TAACPUqkYpCk085.png

恢复westos库及其所有信息

wKiom1mPJ7OApddiAAEP_zdN_2s989.png

7.用户授权

create user lee@localhost identified by 'lee';##建立用户lee,此用户只能通过本机登陆

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

grant insert,update,delete,select on westos.test to lee@localhost; ##用户授权

grant select on westos.* to lee@'%'

show grants for lee@'%'##查看用户授权

show grants for lee@localhost

revoke delete on westos.test  from lee@localhost;##去除用户授权权力

drop user lee@'%'##删除用户

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

wKioL1mPKTmgjMAmAABU2Ea7jGo645.png

wKiom1mPKTrgqpPjAAAd-bP52uM282.png

查看lee被授权的功能

wKioL1mPKTryYfryAABT1Tnh53M050.png

给lee用户select权限

wKiom1mPKTuw9qPnAACJTqNddOg623.png

测试权限是否生效

wKioL1mPKTvCdDpcAADNSzl90Ek522.png

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

wKiom1mPKT3hC1kyAAEYSEe0nb4745.png

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登陆接口但忽略授权表

wKioL1mPKH_CyTieAADhZwDD4Tw689.png

wKiom1mPKH-yNcLWAAD9wCas428456.png

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

wKioL1mPKIDSvKdsAAFecUuvh2Q533.png

数据库的网页管理工具

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

安装所需软件

wKiom1mPKinA0FtgAAASvpGNgoA809.png

wKiom1mPKiqgL_qpAAB5asfHLUQ748.png

解压软件包,并更名

wKioL1mPKiqRD7H9AABpwvY9lYY533.png

随意填入

wKioL1mPKivBcth9AAAyXOZmOJg194.png

wKiom1mPKiviXtyyAAAvEc6UoTI975.png


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

wKiom1mPKiyD5twuAAEX9fSQIQk146.png

wKioL1mPKi3BxUmeAAFPBtilDRs963.png

展开阅读全文

没有更多推荐了,返回首页