当先锋百科网

首页 1 2 3 4 5 6 7

Mariadb 数据库管理系统是 Mysql 的一个分支,完全兼容于 Mysql 数据库

1.Mariadb 的安装与使用

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

Copyright (c) , , 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    ##查看与 mysql 有关的端口信息,从而校验 mariadb 的监听端口
tcp               :            :*               LISTEN                     /mysqld         
[root@localhost ~]# systemctl stop firewalld  
[root@localhost ~]# vim /etc/my.cnf    ##编辑主配文件 /etc/my.cnf
# instructions in http://fedoraproject.org/wiki/Systemd
skip-networking=                   #3跳过网络开接口,阻断所有来自网络的 tcp/ip 连接
安全初始化
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[root@localhost ~]# systemctl restart mariadb.service 
[root@localhost ~]# netstat -antlpe | grep mysql     
Mysql_secure_installation  ##设置密码,脚本
[root@localhost ~]# mysql_secure_installation     ##使用该工具进行数据库安全设置
/usr/bin/mysql_secure_installation: line : 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): ##输入 root 当前密码,如果没有,直接 enter
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] y ##设定 root 密码
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] y ##移除当前匿名用户
 ... 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] y ##不允许 root 用户远程登录
 ... 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] y ##移除测试数据库
  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] y ##重新加载数据库的表
 ... 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@localhost ~]# mysql         ##设置密码后不能直接进入数据库
ERROR  (): Access denied for user 'root'@'localhost' (using password: NO)
Mysql -uroot -p
[root@localhost ~]# mysql -uroot -p    ##需密码登录
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 
Server version: -MariaDB MariaDB Server
Copyright (c) , , Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

2. 查询 sql

show databases;               ##显示数据库

use mysql;                     ##进入 mysql 库

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

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

desc user;                      ##查询 user 表中的结构(显示所有字段的名称)
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 
Server version: -MariaDB MariaDB Server

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

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

MariaDB [(none)]> SHOW DATABASES;    ##显示数据库    
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
 rows in set ( sec)

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]> SHOW TABLES;          ## 显示当前库中表的名称
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
 rows in set ( sec)




MariaDB [mysql]>  SELECT * FROM user;         ## 查询 user 表中的所有内容(* 可以用表中任何字段代替)
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| Host      | User | Password                                  | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string |
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| localhost | root | *BB5DF4823DA319BBF86C99624479A198E6EEE9 | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |              |            |                |                     |        |                       |
|  | root | *BB5DF4823DA319BBF86C99624479A198E6EEE9 | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |              |            |                |                     |        |                       |
| ::       | root | *BB5DF4823DA319BBF86C99624479A198E6EEE9 | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |              |            |                |                     |        |                       |
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
 rows in set ( sec)

MariaDB [mysql]>  SELECT User,Host,Password FROM user;      
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root |  | *BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | ::       | *BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+------+-----------+-------------------------------------------+
 rows in set ( sec)

MariaDB [mysql]> SELECT User,Host,Password,Select_priv FROM user Where User='root';
+------+-----------+-------------------------------------------+-------------+
| User | Host      | Password                                  | Select_priv |
+------+-----------+-------------------------------------------+-------------+
| root | localhost | *BB5DF4823DA319BBF86C99624479A198E6EEE9 | Y           |
| root |  | *BB5DF4823DA319BBF86C99624479A198E6EEE9 | Y           |
| root | ::       | *BB5DF4823DA319BBF86C99624479A198E6EEE9 | Y           |
+------+-----------+-------------------------------------------+-------------+
 rows in set ( sec)

MariaDB [mysql]> SELECT User,Host,Password,Select_priv FROM user Where User='root' AND Host='localhost';
+------+-----------+-------------------------------------------+-------------+
| User | Host      | Password                                  | Select_priv |
+------+-----------+-------------------------------------------+-------------+
| root | localhost | *BB5DF4823DA319BBF86C99624479A198E6EEE9 | Y           |
+------+-----------+-------------------------------------------+-------------+
 row in set ( sec)

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

3. 字符的插入

MariaDB [mysql]> CREATE DATABASE westos;      ##创建 westos 库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> SHOW DATABASES;             ##显示数据库,有新建的westos
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
4 rows in set (0.00 sec)

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

MariaDB [westos]> CREATE TABLE liu(     ##创建 liu 表,并且 liu 表含有两个字段 username,password
    -> username varchar(6) not null,          ##username字段字符长度为6,且不能为空
    -> password varchar(50) not null);        ##password字段字符长度为50,且不能为空
Query OK, 0 rows affected (0.33 sec)

MariaDB [westos]> SHOW TABLES;     ###显示表的名称
+------------------+
| Tables_in_westos |
+------------------+
| liu              |
+------------------+
1 row in set (0.00 sec)

MariaDB [westos]> DESC liu;    ##查询 liu 表中的结构(显示所有字段的名称)
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(6)  | NO   |     | NULL    |       |
| password | varchar(50) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

MariaDB [westos]> INSERT INTO liu values ('qiang','123'); ##向liu表中插入数据'qiang','123'
Query OK, 1 row affected (0.31 sec)

MariaDB [westos]> SELECT *FROM liu;
+----------+----------+
| username | password |
+----------+----------+
| qiang    | 123      |
+----------+----------+
1 row in set (0.00 sec)

MariaDB [westos]> INSERT INTO liu values ('qin','456');  ##向liu表中插入数据'qin','456'
Query OK, 1 row affected (0.31 sec)

MariaDB [westos]> SELECT *FROM liu;         ##查看liu表中的所有内容
+----------+----------+
| username | password |
+----------+----------+
| qiang    | 123      |
| qin      | 456      |
+----------+----------+
2 rows in set (0.00 sec)

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

4. 表及内容的修改

Database changed
MariaDB [westos]> SHOW TABLES;
+------------------+
| Tables_in_westos |
+------------------+
| liu              |
+------------------+
1 row in set (0.00 sec)

MariaDB [westos]> ALTER TABLE liu RENAME messages;  ##重命名‘liu’表为‘message’
Query OK, 0 rows affected (0.31 sec)

MariaDB [westos]> SHOW TABLES;
+------------------+
| Tables_in_westos |
+------------------+
| messages         |
+------------------+
1 row in set (0.00 sec)



MariaDB [westos]> ALTER TABLE messages ADD AGE varchar(4); ##向messages表增加age字段,字符长度为4,可为空
Query OK, 2 rows affected (0.36 sec)               
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]> SELECT * FROM messages;
+----------+----------+------+
| username | password | AGE  |
+----------+----------+------+
| qiang    | 123      | NULL |
| qin      | 456      | NULL |
+----------+----------+------+
2 rows in set (0.00 sec)

MariaDB [westos]> ALTER TABLE messages DROP AGE ;  ##删除age表
Query OK, 2 rows affected (0.11 sec)               
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]> ALTER TABLE messages ADD AGE varchar(4) after username;##向messages表增加age字段,字符长度为4,可为空,并指明插入位置为username字段之后
Query OK, 2 rows affected (0.36 sec)               
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]> SELECT * FROM messages;
+----------+------+----------+
| username | AGE  | password |
+----------+------+----------+
| qiang    | NULL | 123      |
| qin      | NULL | 456      |
+----------+------+----------+
2 rows in set (0.00 sec)

MariaDB [westos]> ALTER TABLE messages DROP AGE ;     删除字段age
Query OK, 2 rows affected (0.35 sec)               
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]> SELECT * FROM messages;
+----------+----------+
| username | password |
+----------+----------+
| qiang    | 123      |
| qin      | 456      |
+----------+----------+
2 rows in set (0.00 sec)

MariaDB [westos]> UPDATE messages SET username='hello' WHERE password='123';##更新messages用户username为hello,password为123
Query OK, 1 row affected (0.32 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [westos]> SELECT * FROM messages;
+----------+----------+
| username | password |
+----------+----------+
| hello    | 123      |
| qin      | 456      |
+----------+----------+
2 rows in set (0.00 sec)

MariaDB [westos]> DELETE from messages WHERE username='hello';   删除表messages中username为hello的数据
Query OK, 1 row affected (0.31 sec)

MariaDB [westos]> SELECT * FROM messages;
+----------+----------+
| username | password |
+----------+----------+
| qin      | 456      |
+----------+----------+
1 row in set (0.00 sec)

MariaDB [westos]> DROP table messages;         删除库messages
Query OK, 0 rows affected (0.31 sec)

MariaDB [westos]> SELECT * FROM messages;
ERROR 1146 (42S02): Table 'westos.messages' doesn't exist
MariaDB [westos]> DROP DATABASE westos;
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)

5. 搭建图形化界面管理数据库

安装php httpd
[root@localhost ~]# yum install php httpd -y
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl stop firewalld
下载。。。
[root@localhost html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 
[root@localhost html]# ls
phpMyAdmin-.-all-languages  phpMyAdmin-.-all-languages.tar.bz2



[root@localhost html]# ls
phpMyAdmin-.-all-languages.tar.bz2
[root@localhost html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 
[root@localhost html]# ls
phpMyAdmin-.-all-languages  phpMyAdmin-.-all-languages.tar.bz2
[root@localhost html]# rm -rf *.bz2
[root@localhost html]# ls
phpMyAdmin-.-all-languages
[root@localhost html]# mv phpMyAdmin-3.4.0-all-languages/ mysqlamdin
[root@localhost html]# ls
mysqlamdin
[root@localhost html]# cd mysqlamdin/
[root@localhost mysqlamdin]# cp config.sample.inc.php config.inc.php
[root@localhost mysqlamdin]# cd ..
[root@localhost html]# ls
mysqlamdin
[root@localhost html]# mv mysqlamdin/ mysqladmin
[root@localhost html]# ll
total 
drwxr-xr-x  root root  May  : mysqladmin
[root@localhost html]# yum search php
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
[root@localhost html]# yum install php-mysql.x86_64 -y
[root@localhost html]# systemctl restart httpd

浏览器打开 http://172.25.254.136/mysqladmin
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

[root@localhost html]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 45
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;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| qin                |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> USE qin
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 [qin]> SHOW TABLES;
+---------------+
| Tables_in_qin |
+---------------+
| liu           |
+---------------+
1 row in set (0.00 sec)

MariaDB [qin]> DESC liu;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO   |     | NULL    |       |
| passward | varchar(40) | NO   |     | NULL    |       |
| class    | varchar(10) | NO   |     | NULL    |       |
| obj      | varchar(10) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
在浏览器端删除username后

MariaDB [qin]> DESC liu;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| passward | varchar(40) | NO   |     | NULL    |       |
| class    | varchar(10) | NO   |     | NULL    |       |
| obj      | varchar(10) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

6. 用户授权

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

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

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

MariaDB [(none)]> CREATE USER sun@localhost identified by 'sun';
能登陆,但不能查询与更改;
Query OK,  rows affected ( sec)

这里写图片描述
这里写图片描述

MariaDB [(none)]> GRANT SELECT on qin.* to sun@localhost;
加读权限只可读查看,不可更改
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR sun@localhost;
+------------------------------------------------------------------------------------------------------------+
| Grants for sun@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'sun'@'localhost' IDENTIFIED BY PASSWORD '*FAD4198559E3EE2F6CD93FA2DBC008ED8A3E57A5' |
| GRANT SELECT ON `qin`.* TO 'sun'@'localhost'                                                               |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
```![这里写图片描述](https://img-blog.csdn.net/20180528140102683?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2F3b3lhb2M=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)





<div class="se-preview-section-delimiter"></div>

MariaDB [(none)]> GRANT UPDATE on qin.* to sun@localhost; 可查询可更改
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR sun@localhost;
+————————————————————————————————————+
| Grants for sun@localhost |
+————————————————————————————————————+
| GRANT USAGE ON . TO ‘sun’@’localhost’ IDENTIFIED BY PASSWORD ‘*FAD4198559E3EE2F6CD93FA2DBC008ED8A3E57A5’ |
| GRANT SELECT, UPDATE ON qin.* TO ‘sun’@’localhost’ |
+————————————————————————————————————+
2 rows in set (0.00 sec)
“`

MariaDB [(none)]> GRANT UPDATE on qin.* to sun@localhost;  可查询可更改
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR sun@localhost;
+------------------------------------------------------------------------------------------------------------+
| Grants for sun@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'sun'@'localhost' IDENTIFIED BY PASSWORD '*FAD4198559E3EE2F6CD93FA2DBC008ED8A3E57A5' |
| GRANT SELECT, UPDATE ON `qin`.* TO 'sun'@'localhost'                                                       |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

这里写图片描述

撤销权限
MariaDB [(none)]> REVOKE UPDATE on qin.* from sun@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR sun@localhost;
+------------------------------------------------------------------------------------------------------------+
| Grants for sun@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'sun'@'localhost' IDENTIFIED BY PASSWORD '*FAD4198559E3EE2F6CD93FA2DBC008ED8A3E57A5' |
| GRANT SELECT ON `qin`.* TO 'sun'@'localhost'                                                               |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)


删除用户
MariaDB [(none)]> DROP USER sun@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SELECT User FROM mysql.user;
+------+
| User |
+------+
| root |
| root |
| root |
+------+
3 rows in set (0.00 sec)

7. 更改密码

[root@localhost ~]# systemctl stop mariadb                 关闭mariadb
[root@localhost ~]# mysqld_safe --skip-grant-tables &           执行并打入后台
[] 
[root@localhost ~]# 180526 02:40:45 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
 :: mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
^C
[root@localhost ~]# mysql                      不用密码可以直接登陆
Welcome to the MariaDB monitor.  Commands end with ; or \g.

MariaDB [(none)]> select * from mysql.user;
| localhost | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 
| .. | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9| ::       
| root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 
| localhost | sun  | *FAD4198559E3EE2F6CD93FA2DBC008ED8A3E57A5 

MariaDB [(none)]> update mysql.user set Password='123' where User='root';
给root用户更改密码为    此为明文方式
Query OK,  rows affected (. sec)
Rows matched:   Changed:   Warnings: 

MariaDB [(none)]> select * from mysql.user;
| localhost | root |    
| .. | root |       
| ::       | root |          
| localhost | sun  | *FAD4198559E3EE2F6CD93FA2DBC008ED8A3E57A5 

MariaDB [(none)]> update mysql.user set Password=password('123') where User='root';
给root用户更改密码为   加密字符方式

Query OK,  rows affected (. sec)
Rows matched:   Changed:   Warnings: 
| localhost | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
| .. | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | ::          | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
| localhost | sun  | *FAD4198559E3EE2F6CD93FA2DBC008ED8A3E57A5

MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@localhost ~]# fg
mysqld_safe --skip-grant-tables
^C^C^C^C^C^C^C^C^C^C^C^C^C^Z
[]+  Stopped                 mysqld_safe --skip-grant-tables
[root@localhost ~]# killall -9 mysqld_safe
[]+  Killed                  mysqld_safe --skip-grant-tables
[root@localhost ~]# ps aux | grep mysql
mysql       .     pts/    Sl   :   : /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root        .  .     pts/    R+   :   : grep --color=auto mysql
[root@localhost ~]# kill -9 5892               将mysql进程都关掉
[root@localhost ~]# ps aux | grep mysql
root        .  .     pts/    R+   :   : grep --color=auto mysql
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql           等不上去
ERROR  (): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost ~]# mysql -uroot -p             用更改好的密码可以登陆
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.

8. 数据库的备份与恢复

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

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

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

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

mysqldump -uroot -pwestos -e “create database westos;” ##建立 westos 库

mysqldump -uroot -pwestos westos < /mnt/westos.sql ##把数据倒入 westos 库

mysqldump -uroot -pwestos -e “select * from westos.linux;”