Mariadb
许多表格放在一起,组成一个库,若干个库放在一起组成一个数据库
mysql是目前应用最为广泛,mariadb是其中一个分支.它们存在着这样的关系:mysql > 数据库 > 数据表 > 数据
以图书馆为例,mysql相当于图书馆,数据库相当于各类书库,如计算机类、文学类等;数据表相当于每个书库的书架;数据就是书架上的书籍。
接下来介绍怎么安装和使用mariadb?
安装和安全初始化设置
[root@localhost ~]# yum install mariadb-server -y
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql //默认可以直接登陆,不安全
[root@localhost ~]# netstat -antlpe | grep mysql //可以看到默认端口,表示数据库安装后默认裸露在互联网上
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27 71311 3058/mysqld
[root@localhost ~]# vim /etc/my.cnf //编辑配置文件
skip-networking=1 //添加这一行,表示跳过数据库在网络上开启端口的功能
[root@localhost ~]# systemctl restart mariadb
//重启服务
[root@localhost ~]# netstat -antlpe | gr //无开启端口
[root@localhost ~# mysql_secure_installation #安全初始化脚本,进行如下设置:
添加密码
关闭匿名用户登陆
关闭超级用户远程登陆
删除测试数据库和访问
重新加载权限列表
[root@localhost ~]# mysql -uroot -p #登陆数据库,输入密码,进入
数据库的查询
首先,进入数据库:
MariaDB [(none)]> SHOW DATABASES; #查询数据库里有什么库
MariaDB [(none)]> USE mysql; #使用mysql库
MariaDB [mysql]> SHOW tables; #查询库里有什么表格
MariaDB [mysql]> SELECT * FROM user; #查询user表的所有内容
MariaDB [mysql]> SELECT User,Host,Password FROM user; #查询user表里的User字段、Host字段和Password字段
+------+-----------+-------------------------------------------+
| User | Host | Password |
+------+-----------+-------------------------------------------+
| root | localhost | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| root | 127.0.0.1 | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| root | ::1 | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+------+-----------+-------------------------------------------+
MariaDB [mysql]> SELECT User,Host,Password FROM user Where User='root' AND Host='localhost';
# 查询user表里User=root且Host=localhost的User字段、Host字段和Password字段;其中,Where 后面gender是查询的条件,格式要用‘’
数据库的创建、插入
MariaDB [(none)]> CREATE DATABASE linux; //创建linux库
MariaDB [(none)]> USE linux; //使用linux库
Database changed
MariaDB [linux]> SHOW tables; //查看linux库里有什么表格
Empty set (0.00 sec) //空的
MariaDB [linux]> CREATE TABLE yee(
-> username varchar(6) not null,
-> password varchar(50) not null); //在linux库里创建yee表格,varchar表示可变长字符
Query OK, 0 rows affected (0.40 sec)
MariaDB [linux]> DESC yee; #查询yee表的结构
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(6) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [linux]> INSERT INTO yee values ('tom',123); //在linux库的yee表里插入内容:username为tom,password为123
MariaDB [linux]> INSERT INTO yee values ('jerry',123);
Query OK, 1 row affected (0.33 sec)
MariaDB [linux]> SELECT * FROM yee; #查看yee表里的内容
+----------+----------+
| username | password |
+----------+----------+
| tom | 123 |
| jerry | 123 |
+----------+----------+
2 rows in set (0.00 sec)
数据库信息的修改
[root@localhost yum.repos.d]# cd /var/lib/mysql/ //默认路径
[root@localhost mysql]# ls //可以查看到建立的库linux
[root@localhost mysql]# mv linux/ westos //修改库名linux为westos
[root@localhost mysql]# systemctl restart mariadb
上述的操作为库名的修改,但是一般情况下,库的名字不建议修改,修改后可能会出错,如下图所示;
我们把库名重新改回来(linux),继续操作:
MariaDB [(none)]> flush privileges; //数据库的刷新
Query OK, 0 rows affected (0.00 sec)
MariaDB [linux]> ALTER TABLE yee RENAME hahaha; //修改表的名字
Query OK, 0 rows affected (0.38 sec)
MariaDB [linux]> SHOW TABLES; //显示linux库的表
+-----------------+
| Tables_in_linux |
+-----------------+
| hahaha |
+-----------------+
1 row in set (0.00 sec)
MariaDB [linux]> ALTER TABLE hahaha ADD age varchar(10); //添加表的字段,默认添加在最后
Query OK, 2 rows affected (0.09 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [linux]> SELECT * FROM hahaha;//查看表hahaha的内容
+----------+----------+------+
| username | password | age |
+----------+----------+------+
| tom | 123 | NULL |
| jerry | 123 | NULL |
+----------+----------+------+
2 rows in set (0.00 sec)
MariaDB [linux]> ALTER TABLE hahaha DROP age; //删除age字段
Query OK, 2 rows affected (0.42 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [linux]> ALTER TABLE hahaha ADD age varchar(10) AFTER username; //添加字段到某个位置,不能加到最前面,有错乱的可能
Query OK, 2 rows affected (0.38 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [linux]> SELECT * FROM hahaha; //查看表的内容
+----------+------+----------+
| username | age | password |
+----------+------+----------+
| tom | NULL | 123 |
| jerry | NULL | 123 |
+----------+------+----------+
2 rows in set (0.00 sec)
MariaDB [linux]> UPDATE hahaha SET username='frank' WHERE username='tom'; //修改表的内容
Query OK, 1 row affected (0.35 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [linux]> SELECT * FROM hahaha;
+----------+------+----------+
| username | age | password |
+----------+------+----------+
| frank | NULL | 123 |
| jerry | NULL | 123 |
+----------+------+----------+
2 rows in set (0.00 sec)
MariaDB [linux]> DELETE FROM hahaha WHERE username='jerry'; //删除表的内容
Query OK, 1 row affected (0.66 sec)
MariaDB [linux]> SELECT * FROM hahaha;
+----------+------+----------+
| username | age | password |
+----------+------+----------+
| frank | NULL | 123 |
+----------+------+----------+
1 row in set (0.00 sec)
MariaDB [linux]> DROP TABLE hahaha; //删除表
Query OK, 0 rows affected (0.33 sec)
MariaDB [linux]> DROP DATABASE linux; //删除库
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
网页模式管理数据库
准备环境
[root@localhost mysql]# yum install php httpd -y
[root@localhost mysql]# yum search php //搜索支持php的相关软件
[root@localhost mysql]# yum install php-mysql.x86_64 -y
[root@localhost mysql]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# systemctl start httpd //开启http服务
[root@localhost html]# systemctl stop firewalld
在网络上下载安装包:phpMyAdmin-3.4.0-all-languages.tar.bz2
[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]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin //重命名为mysqladmin
[root@localhost html]# ls
[root@localhost mysqladmin]# cp config.sample.inc.php config.inc.php //复制生成config.inc.php文件(根据下载页面快速安装的说明)
浏览器:172.25.254.174/mysqladmin,登陆进入网页模式,下图为进入网页的登陆界面(root用户登录):
创建一个新的数据库:
创建表:
也可以通过命令查看是否已经建立;
用户的建立
MariaDB [(none)]> create user admin@localhost identified by '123';
MariaDB [(none)]> select user from mysql.user;
用户授权
MariaDB [(none)]> granted INSERT,SELECT on westos.* admin@localhost; //授予admin用户插入字段,查看的权利
MariaDB [(none)]> show grants for admin@localhost;
登陆admin用户进行权利测试
MariaDB [(none)]> show databases; //查看库
MariaDB [(none)]> insert into westos.linux values(10); //插入字段
MariaDB [(none)]> select * from westos.linux; //查看linux表的内容
MariaDB [(none)]> delete from westos.linux where username='10'; //删除
这里没有删除成功的原因是没有给admin用户删除权限,所以被拒绝;
撤销权限
需要切换root进入数据库管理:
MariaDB [(none)]>revoke insert on westos.* from admin@localhost;
MariaDB [(none)]> show grants for admin@localhost;
切换至admin用户:
同样,撤销了插入权限,自然操作失败;
删除用户
切换至root用户:
MariaDB [(none)]> select user from mysql.user;
MariaDB [(none)]> drop user admin@localhost; //删除用户
MariaDB [(none)]> select user from mysql.user;
数据库的备份
mysqldump -uroot -p123 westos > /mnt/westos.sql
mysqldump -uroot -p123 --all-database > /mnt/westos.sql //备份所有的数据
mysqldump -uroot -p123 --all-database --no-data > /mnt/westos.sql //只备份框架,不备份数据
实验:
mysqldump -uroot -p123 westos > /mnt/westos.sql //备份
假设现在我们不小心删除了数据库:
mysql -uroot -p123 -e "drop database westos;" //删除
mysql -uroot -p123 -e "show database;" //显示库列表
恢复有两种方法:
A:`vim /mnt/westos.sql
添加内容: CREATE DATABASE westos;
USE westos;
mysql -uroot -p123 < /mnt/westos.sql //检测westos库是否恢复
mysql -uroot -p123 -e "select * from westos.linux;"
文件添加内容:
B:首先注释刚刚添加的内容,以免影响实验效果;
mysql -uroot -p123 -e "drop database westos;" //删除
mysql -uroot -p123 -e "show database;" //显示库列表
//恢复
mysql -uroot -plee -e "CREATE DATABASE westos;"
mysql -uroot -plee westos < /mnt/westos.sql //westos这里不能省略
超级用户登陆数据库的密码忘记怎么解决?
[root@foundation182 mysql]# systemctl stop mariadb //停止服务
[root@foundation182 mysql]# mysqld_safe --skip-grant-tables &
[1] 491
180526 02:47:13 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
180526 02:47:13 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@foundation182 mysql]# mysql //进入mysql环境
MariaDB [(none)]> update mysql.user set Password='123' where User='root'; //更新root的密码
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
MariaDB [(none)]> USE mysql //进入mysql库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> SELECT * FROM user; //查看user表的内容,root的密码是明文;
用户密码为明文,不安全;现在它加密;
MariaDB [mysql]> update mysql.user set Password=password('123') where User='root';
Query OK, 4 rows affected (0.00 sec) //加密更改密码
Rows matched: 4 Changed: 4 Warnings: 0
MariaDB [mysql]> SELECT * FROM user; //root的密码是加密状态
[root@foundation182 mysql]# fg //查看进程,此时ctr+c结束进程失败
mysqld_safe --skip-grant-tables
^C^C^Z
[1]+ Stopped mysqld_safe --skip-grant-tables
[root@foundation182 mysql]# killall -9 mysqld_safe //结束这个进程
[1]+ Killed mysqld_safe --skip-grant-tables
现在的密码是加密状态,设置成功。
因为进程占用内存,最好操作结束后结束掉相关进程,以免影响其他相关操作。