欢迎投稿

今日深度:

Linux:数据库的管理,Linux:数据库管理

Linux:数据库的管理,Linux:数据库管理


mariadb 简介:

MariaDB 数据库管理系统是 MySQL 的一个分支,主要由开源社区在维护,采用 GPL 授权许可 MariaDB 的目的是完全兼容MySQL ,包括 API 和命令行,是 MySQL 的代替品MariaDB 由 MySQL 的创始人 Michael Widenius (英语:Michael Widenius )主导开发,他早前曾以 10 亿美元的价格,将自己创建的公司 MySQL AB 卖给了 SUN ,此后,随着SUN 被甲骨文收购, MySQL 的所有权也落入 Oracle 的手中MariaDB 名称来自 Michael Widenius 的女儿 Maria 的名字。

数据库的类型:

db2 oracle mysql(mariadb) sqlserver数据库相当于高级的excel表格,其中的字段相当于列。

一·安装数据库部署

服务器desktop
设置ip:

配置yum源:

[root@localhost ~]# yum install mariadb-server -y     ##安装mariadb
[root@localhost ~]# systemctl start mariadb     ##启动mariadb服务
[root@localhost ~]# mysql     ##进入数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

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

MariaDB [(none)]> quit
Bye
[root@localhost ~]# netstat -antlpe |grep mysql    ##校验mariadb的监听端口
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      27         51499      2667/mysqld         
[root@localhost ~]# systemctl stop firewalld    ##关闭火墙
[root@localhost ~]# vim /etc/my.cnf     ##mariadb主配置文件
skip-networking=1   ##跳过网路服务
[root@localhost ~]# systemctl restart  mariadb    
[root@localhost ~]# netstat -antlpe |grep mysql   
[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

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

MariaDB [(none)]> quit
Bye
[root@localhost ~]# mysql_secure_installation    ##安全初始化脚本 
第一步:按回车
第二步:输入y,输入密码
第三步:一直按回车至结束
[root@localhost ~]# mysql -uroot -p   ##进入数据库(需要输入密码)
-u:登陆用户
-p:该用户密码



在/etc/my.cnf mariadb主配置文件中写入内容:
安全初始化脚本:

再次进入数据库:

注意:进入数据库时,-p后面不写密码

二·数据库的基本SQL语句

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW DATABASES;    ##查询数据库

MariaDB [(none)]> USE mysql;     ##进入mysql数据库

MariaDB [mysql]> SHOW TABLES;     ##查询当前库中的名称

MariaDB [mysql]> DESC user;    ##查询user表的结构(字段)

 MariaDB [mysql]> SELECT * FROM user;    ##查询user表中的所有内容 

MariaDB [mysql]> SELECT User,Host,Password FROM user;    ##查询user表中的user、host、password的信息

MariaDB [mysql]> SELECT  User,Host,Password FROM user WHERE User='root';    ##查询user表中的user、host、password的使用名为root的用户的信息

注意:在输入每个命令时必须在输完后加“;“,否则命令执行不成功

三·数据库的建立

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE westos;     ##建立数据库westos
MariaDB [(none)]> SHOW DATABASES;    ##查看数据库

MariaDB [(none)]> USE westos;     ##进入westos库中
Database changed
MariaDB [westos]> SHOW TABLES;    ##查看表
Empty set (0.00 sec)

MariaDB [westos]> CREATE TABLE linux(       ##创建表linux
    -> username varchar(6) not null,      ##字段,字符长度不超过6,且不能为空
    -> password varchar(50) not null);
MariaDB [westos]> SHOW TABLES;     ##查看表    

MariaDB [westos]> DESC linux;     ##显示表的结构

MariaDB [westos]> INSERT INTO linux values ('lzj','123');    ##给linux表中添加数据lzj以及密码123
MariaDB [westos]> SELECT * FROM linux;     ##显示表内所有内容

四·数据管理、结构管理

1、更改数据库结构

MariaDB [(none)]> USE westos;
MariaDB [westos]> SHOW TABLES;

MariaDB [westos]> ALTER TABLE linux RENAME messages;    ##修改表格名字,将linux修改为messages
MariaDB [westos]> SHOW TABLES;
MariaDB [westos]> ALTER TABLE messages RENAME linux;   ##修改表格名字,将messages修改为linux

MariaDB [westos]> ALTER TABLE linux ADD age varchar(50);     ##在linux表格中添加age字段,随意位置
MariaDB [westos]> ALTER TABLE linux DROP age;      ##在linux表格中移除age字段
MariaDB [westos]> select * from linux;      

MariaDB [westos]> ALTER TABLE linux ADD age varchar(50) AFTER username;    ##在linux表格中添加age字段,位置在username后
MariaDB [westos]> select * from linux;
MariaDB [westos]> ALTER TABLE linux DROP age;     ##在linux表格中移除age字段

2、更改数据库数据:

MariaDB [westos]> UPDATE linux SET password='222' WHERE username='lzj';     修改linux表格中的password信息
MariaDB [westos]> select * from linux;
MariaDB [westos]> DELETE FROM linux WHERE username='lzj';     ##删除表格中的某一行
MariaDB [westos]> select * from linux;
MariaDB [westos]> DROP TABLE linux;    ##删除表格
MariaDB [westos]> DROP DATABASE westos;    删除数据库

五·在网页中创建数据库

[root@localhost ~]# yum install httpd php php-mysql  -y   ##安装阿帕奇、php-mysql、php
[root@localhost ~]# systemctl start httpd
下载php安装包
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2   ##解压
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages  phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# rm -fr phpMyAdmin-3.4.0-all-languages.tar.bz2   ##删除安装包
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages
[root@localhost html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin    ##重命名
[root@localhost html]# ls
mysqladmin
[root@localhost html]# cd /mysqladmin
[root@localhost mysqladmin]# cp config.sample.inc.php config.inc.php 
进入网页输入172.25.254.119/mysqladmin


网页:可以选择语言为中文,添入用户名以及密码

登陆成功后界面:

点击数据库–新建数据框中写入数据库名westos–写入数据表名字linux以及字段5–新建数据表–点击插入,写用户lzj、密码123、班级linux、文件名admin




执行成功:

六·创建用户和访问权限

在shell中执行命令,在网页中检测

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> CREATE USER lzj@localhost identified by 'lzj';     创建用户(只能是本地)
第一个lzj:用户
第二个lzj:密码
MariaDB [(none)]> SELECT User FROM mysql.user;    ##查看用户


在网页中选择退出后,再次登陆,用户名和密码是刚才创建的
没有给予任何权力时的数据库:

MariaDB [(none)]> GRANT  SELECT on westos.* to lzj@localhost;   ##赋予用户查看权限,可以查看,但是不能修改
MariaDB [(none)]> SHOW GRANTS FOR lzj@localhost;     ##查看权限


网页中:

MariaDB [(none)]> GRANT UPDATE on westos.* to lzj@localhost;    ##赋予用户修改权限
MariaDB [(none)]> SHOW GRANTS FOR lzj@localhost;     ##查看权限


网页中:

MariaDB [(none)]> REVOKE UPDATE on westos.* from lzj@localhost;   ##撤销用户修改权限

MariaDB [(none)]> DROP USER lzj@localhost;    ##删除新建用户
MariaDB [(none)]> SELECT User FROM mysql.user;   ##查看用户

注意:要给存在的用户的数据库授权,有建立的westos,所以要在westos数据库中赋予权限

七·超级用户忘记数据库密码

[root@localhost ~]# systemctl stop mariadb.service       ##关闭
[root@localhost ~]# mysqld_safe --skip-grant-tables &    ##跳过授权表并打入后台
[1] 1751
[root@localhost ~]# 180526 02:40:53 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.   ##后台运行
180526 02:40:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@localhost ~]# mysql
MariaDB [(none)]> update mysql.user set Password=password('123')  where User='root';    ##更改密码
MariaDB [(none)]> select * from mysql.user;     ##查看内容
MariaDB [(none)]> quit
[root@localhost ~]# fg       ##调入后台
mysqld_safe --skip-grant-tables
^Z        ##按Ctrl+z退出
[1]+  Stopped                 mysqld_safe --skip-grant-tables
[root@localhost ~]# killall -9 mysqld_safe          ##结束后台
[1]+  Killed                  mysqld_safe --skip-grant-tables
[root@localhost ~]# ps aux | grep mysql           ##过滤所有mysql的进程
[root@localhost ~]# kill -9 32342        ##结束进程
[root@localhost ~]# ps aux | grep mysql
[root@localhost ~]# systemctl start mariadb      ##开启
[root@localhost ~]# mysql -uroot -p      ##再次登陆
Enter password: 


修改密码:

查看:

结束后台以及结束进程:

修改成功后再次登陆:

八·备份

首先要有建立好的数据库westos,数据库表linux。

mysqldump -uroot -p123 –all-database > /mnt/westos.all 所有数据库资料备份,此处为自己设置的密码
mysqldump -uroot -p123 –all-database –no-data > /mnt/westos.err 只备份数据结构,不备份数据
mysqldump -uroot -p123 westos > /mnt/westos.sql 指定westos数据库的资料备份

[root@localhost mnt]# mysqldump -uroot -p123 --all-database > /mnt/westos.all      
[root@localhost mnt]# mysqldump -uroot -p123 --all-database --no-data > /mnt/westos.err
[root@localhost mnt]# mysqldump -uroot -p123 westos > /mnt/westos.sql
[root@localhost mnt]# ls
westos.all  westos.err  westos.sql
[root@localhost mnt]# mysql -uroot -p123 -e "drop database westos;"    ##删除数据库westos
[root@localhost mnt]# mysql -uroot -p123 -e "show databases;"    ##查看数据库
[root@localhost mnt]# mysql -uroot -p123 < /mnt/westos.sql    ##恢复不成功,会报错


第一种恢复方法:

[root@localhost mnt]# vim /etc/my.cnf
CREATE DATABASE westos;
USE westos;                  ##编写内容
[root@localhost mnt]# mysql -uroot -p123 < /mnt/westos.sql     ##恢复
[root@localhost mnt]# mysql -uroot -p123 -e "show databases;"     ##查看数据库
[root@localhost mnt]# mysql -uroot -p123 -e "select * from westos.linux;"    ##查看数据库表

编写内容:


恢复成功:

第二种恢复方法:
要将第一种方法编写内容删除

[root@localhost mnt]# vim /mnt/westos.sql    ##删除编写内容
[root@localhost mnt]# mysql -uroot -p123 -e "drop database westos;"   ##删除数据库westos
[root@localhost mnt]# mysql -uroot -p123 -e "show databases;"     ##查看数据库
[root@localhost mnt]# mysql -uroot -p123  -e "CREATE DATABASE westos;"    ##创建数据库westos
[root@localhost mnt]# mysql -uroot -p123 westos < /mnt/westos.sql    ##恢复
[root@localhost mnt]# mysql -uroot -p123 -e "show databases;"    ##查看数据库


做完实验需要恢复环境

[root@localhost mnt]# rpm -e httpd php php-mysql    ##卸载
[root@localhost mnt]# cd /var/www/html
[root@localhost html]# ls
mysqladmin
[root@localhost html]# rm -fr *     ##删除
[root@localhost html]# ls

www.htsjk.Com true http://www.htsjk.com/mariadb/33683.html NewsArticle Linux:数据库的管理,Linux:数据库管理 mariadb 简介: MariaDB 数据库管理系统是 MySQL 的一个分支,主要由开源社区在维护,采用 GPL 授权许可 MariaDB 的目的是完全兼容MySQL ,包括 API 和命令行,是...
相关文章
    暂无相关文章
评论暂时关闭