linux中mariadb基本用法详解(企业级)

数据库
表的每一个列名字的头   叫做字段
是高级的exel表格软件

数据库种类
sqlserver  sqllite  db2  
oracle  > mysql   比较多  


其中mysql  分支中有一个  mariadb

 

yum install mariadb-server -y
systemctl start mariadb

linux中mariadb基本用法详解(企业级)

mysql_secure_installation
设定密码

linux中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!

 


mysql -uroot   -p
登陆

linux中mariadb基本用法详解(企业级)


去掉端口
 netstat  -antlupe  |grep mysql

linux中mariadb基本用法详解(企业级)
vim /etc/my.cnf.d
skip-networking=1

linux中mariadb基本用法详解(企业级)
systemctl restart mariadb.service

就去掉了端口

 

数据库管理

mysql  -uroot -p

> show databases;   (不区分大小写)

linux中mariadb基本用法详解(企业级)

> use  mysql;   进去库  一定要用分号结尾

 

linux中mariadb基本用法详解(企业级)

 

>show tables;  看到里面的东西

 

linux中mariadb基本用法详解(企业级)

 

> select * from user  查看user库中的所有东西

linux中mariadb基本用法详解(企业级)

 

> select  Host,User,Password from user  选择字段查看部分内容

 

> create database  westos;  建立库

linux中mariadb基本用法详解(企业级)

mysql -uroot -p

show databases  ;

linux中mariadb基本用法详解(企业级)

use  westos ;

linux中mariadb基本用法详解(企业级)

create  table  linux  (

   ->  username  varchar(10)  not null,    长度十个字节不能为空

   -> password  varchar(50) not null

   ->  );

这样就可以

desc linux;   查看表的结构

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

select * from linux 就可以看到了

 

linux中mariadb基本用法详解(企业级)

 

linux中mariadb基本用法详解(企业级)

这样就可以插入数据了

 

修改

alter table linux rename userdata ;

 

linux中mariadb基本用法详解(企业级)

将表linux改名为 userdata

表中继续增加字段

alter table linux  add  age  varchar(4);

linux中mariadb基本用法详解(企业级)

 

默认在表中最后添加

alter table linux  drop age;

linux中mariadb基本用法详解(企业级)

删除 age 

alter table linux  add  age  varchar(4) after username ;

添加到  username  之后  不是最后一列

linux中mariadb基本用法详解(企业级)

 

update linux set age='20' ;

都会改称 20岁

linux中mariadb基本用法详解(企业级)

 

updpate  linux set age='18' where username='lee' ;

updpate  linux set age='22' where username='lee'  and password='123';

增加限定条件地修改

linux中mariadb基本用法详解(企业级)

 

删除数据库

delete  from linux  where username='westos';

从linux 中删除  westos

linux中mariadb基本用法详解(企业级)

drop  table  linux ;

删除  表

drop  database westos;

删除 库

linux中mariadb基本用法详解(企业级)

 

用户授权

 

select User,Host   from mysql.user;

create  user  [email protected]'%'   表示可以远程登陆

create  user  [email protected] identified by 'lee';     后面的by里面的内容是密码

linux中mariadb基本用法详解(企业级)

select User,Host   from mysql.user;

 

mysql -ulee -plee   帐号密码登陆就可以测试了

 

show grants  for  [email protected];  查看权力

grant  select  on westos.*  to  [email protected];

可以允许查询westos库中的所有的表

grant create on westos to [email protected];

允许lee可以在westos库中创建表

 

linux中mariadb基本用法详解(企业级)

 

显示权力

show grants for [email protected] ;

linux中mariadb基本用法详解(企业级)

revoke delete  on westos to [email protected];

删掉权力

 

linux中mariadb基本用法详解(企业级)

flush privileges;  刷新权力

也可以直接写命令  比如

mysel -ulee -plee -e "drop table westos.linux;"

 

linux中mariadb基本用法详解(企业级)

 

删除授权用户

drop user [email protected]'%'   /  localhost

linux中mariadb基本用法详解(企业级)

 

数据库的备份
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表

linux中mariadb基本用法详解(企业级)

 

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

linux中mariadb基本用法详解(企业级)

 

恢复方式2

vim /mnt/westos.sql

linux中mariadb基本用法详解(企业级)

create database westos;

use westos;

mysql -uroot -pwestos  < /mnt/westos.sql 

这时候直接导入就好

linux中mariadb基本用法详解(企业级)

 

更改

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

linux中mariadb基本用法详解(企业级)


##当超级用户密码忘记

systemctl stop mariadb
mysqld_safe --skip-grant-tables &            ##开启mysql登陆接口并忽略授权表
mysql                            ##直接不用密码可以登陆
update mysql.user set Password=password('westos') where User='root';    ##更新超级用户密码信息

linux中mariadb基本用法详解(企业级)

 

 

ps aux | grep mysql                    ##过滤mysql的所有进程并结束这些进程
kill -9 mysqlpid

 

linux中mariadb基本用法详解(企业级)


systemctl start mariadb                    ##重新开启mysql

linux中mariadb基本用法详解(企业级)


mysql -uroot -pwestos                    ##登陆测试

linux中mariadb基本用法详解(企业级)

成功

 

网页管理mysql

 

1.安装
yum install httpd php php-mysql  mariadb -y
systemctl start httpd

systemctl start mariadb
systemctl enable httpd
systemctl stop firewalld
systemctl disable firewalld
需要下载

linux中mariadb基本用法详解(企业级)

linux中mariadb基本用法详解(企业级)

linux中mariadb基本用法详解(企业级)
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

 

linux中mariadb基本用法详解(企业级)
vim config.inc.php
17 $cfg['blowfish_secret'] = 'mysql'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

linux中mariadb基本用法详解(企业级)
systemctl restart httpd

测试:
访问
http://172.25.254.113/mysqladmin

 

linux中mariadb基本用法详解(企业级)

 

linux中mariadb基本用法详解(企业级)

如果不能访问 

setenfoce 0

systemctl stop firewalld