基于linux下mariadb数据库的管理

mariadb(数据库)

  一、数据库的安装及使用前的一些设置
   141   yum install mariadb-server -y    安装mariadb软件
  142  systemctl start mariadb             开启服务
  143  netstat -antlpe | grep mysql        查看端
  144  vim /etc/my.cnf      编辑配置文件将其网络端口设置为跳过
                              添加skip-networking=1

  145  systemctl restart mariadb        重启服务

  146  netstat -antlpe | grep mysql     此时无法查到端口


147  mysql_secure_installation        为mariadb设置安全问题为root用户设置密码,其他的都是y

148  systemctl status firewalld            查看火墙状态         systemctl stop firewalld            关闭防火墙

149  mysql -uroot -p          使用超级用户登陆


二、初进数据库浏览

登陆数据库     mysql -uroot -p
(1)MariaDB [(none)]> SHOW DATABASES;    查看数据库里面都有什么数据
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+


(2)MariaDB [(none)]> USE mysql;       使用mysql这个数据库



(3)MariaDB [mysql]> SHOW TABLES;     显示mysql里面有什么表格
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
|  ......
| time_zone                 |


(4)MariaDB [mysql]> SELECT User FROM user;   从表格user里面显示user这个字段的内容
+------+
| User |
+------+
| root |
| root |
| root |
+------+
3 rows in set (0.00 sec)


(5)MariaDB [mysql]> DESCRIBE  user;   显示user这个表格的框架数据结构
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field                  | Type                              | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host                   | char(60)                          | NO   | PRI |         |       |
| User                   | char(16)                          | NO   | PRI |         |       |
| Password               | char(41)                          | NO   |     |         |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N       |       |
                      .......                     .......
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N       |       


(6)MariaDB [mysql]> SELECT User,Host,Password FROM user Where User='root';    
             从user表格中显示出user=root的中有关User,Host,Password 的信息
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | ::1       | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+------+-----------+-------------------------------------------+


(7)MariaDB [mysql]> SELECT User,Host,Password FROM user Where User='root' AND Host='localhost';
    从user表格中显示出User=root并且Host=localhost的中有关User,Host,Password 的信息

+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

三、对数据库内容实施处理(新建,删除、更改)
mysql -uroot      -p                 登陆mysql


(1)MariaDB [(none)]> SHOW DATABASES;   显示有哪些数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)



(2)MariaDB [(none)]> CREATE DATABASE westos;    创建数据库westos
Query OK, 1 row affected (0.00 sec)



(3)MariaDB [(none)]> SHOW DATABASES;    显示有哪些数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
4 rows in set (0.00 sec)



(4)MariaDB [(none)]> USE westos;     使用westos数据
Database changed


(5)MariaDB [westos]> SHOW TABLES;     显示westos数据库中有哪些表格
Empty set (0.00 sec)



(6)MariaDB [westos]> CREATE TABLE linux(   (创建表格linux并写入内容)
    -> username varchar(6) not null,      (名字  可变长字符串6个字符  不能为空)
    -> password varchar(50) not null);    (密码  可变长字符串50个字符  不能为空)
    Query OK, 0 rows affected (0.27 sec)



(7)MariaDB [westos]> SHOW TABLES;      显示westos数据库中有哪些表格
+------------------+
| Tables_in_westos |
+------------------+
| linux            |
+------------------+
1 row in set (0.00 sec)



(8)MariaDB [westos]> DESC linux;     显示linux表格的数据结构
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(6)  | NO   |     | NULL    |       |
| password | varchar(50) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)



(9)MariaDB [westos]> INSERT INTO  linux values ('xd','123');  添加内容username为xd,password为123
Query OK, 1 row affected (0.03 sec)



(10)MariaDB [westos]> SELECT * FROM linux;     显示linux表格里所有内容
+----------+----------+
| username | password |
+----------+----------+
| xd       | 123      |
+----------+----------+
1 row in set (0.00 sec)




(1)MariaDB [westos]> SHOW TABLES;        显示westos数据库中有哪些表格
+------------------+
| Tables_in_westos |
+------------------+
| linux            |
+------------------+
1 row in set (0.00 sec)


(2)MariaDB [westos]> ALTER TABLE linux RENAME messages;    更改表格名linux为messages
Query OK, 0 rows affected (0.75 sec)


(3)MariaDB [westos]> SHOW TABLES;      显示westos数据库中有哪些表格
+------------------+
| Tables_in_westos |
+------------------+
| messages         |
+------------------+
1 row in set (0.00 sec)




(4)MariaDB [westos]> ALTER TABLE messages RENAME  linux;     更改表格名messages为linux
Query OK, 0 rows affected (2.39 sec)

(5)MariaDB [westos]> SHOW TABLES;    显示westos数据库中有哪些表格
+------------------+
| Tables_in_westos |
+------------------+
| linux            |
+------------------+
1 row in set (0.00 sec)

(6)MariaDB [westos]> SELECT * FROM linux;      
+----------+----------+
| username | password |
+----------+----------+
| xd       | 123      |
| lee      | 456      |
+----------+----------+
2 rows in set (0.00 sec)

(7)MariaDB [westos]> ALTER TABLE linux ADD age varchar(4); 
给表格添加age一栏 设定其为可变长字符串4个字符
 Query OK, 2 rows affected (0.90 sec) Records: 2 Duplicates: 0 Warnings: 0




MariaDB [westos]> SELECT * FROM linux;        显示表格linux里的所有内容  
+----------+----------+------+
| username | password | age  |
+----------+----------+------+
| xd       | 123      | NULL |
| lee      | 456      | NULL |
+----------+----------+------+
2 rows in set (0.01 sec)



(8)MariaDB [westos]> ALTER TABLE linux DROP age;   删除linux表格中age一栏  
Query OK, 2 rows affected (0.32 sec)               
Records: 2  Duplicates: 0  Warnings: 0



(9)MariaDB [westos]> SELECT * FROM linux;            显示表格linux里的所有内容     
+----------+----------+
| username | password |
+----------+----------+
| xd       | 123      |
| lee      | 456      |
+----------+----------+
2 rows in set (0.00 sec)



(10)MariaDB [westos]> ALTER TABLE linux ADD age varchar(4) after username; 
      添加age一栏放在username后   
Query OK, 2 rows affected (0.32 sec)               
Records: 2  Duplicates: 0  Warnings: 0



(11)MariaDB [westos]> SELECT * FROM linux;           显示表格linux里的所有内容     
+----------+------+----------+
| username | age  | password |
+----------+------+----------+
| xd       | NULL | 123      |
| lee      | NULL | 456      |
+----------+------+----------+
2 rows in set (0.00 sec)



(11)MariaDB [westos]> ALTER TABLE linux DROP age;      删除linux表格中age一栏     
Query OK, 2 rows affected (0.79 sec)               
Records: 2  Duplicates: 0  Warnings: 0



(12)MariaDB [westos]> SELECT * FROM linux;            显示表格linux里的所有内容    
+----------+----------+
| username | password |
+----------+----------+
| xd       | 123      |
| lee      | 456      |
+----------+----------+
2 rows in set (0.00 sec)



(13)MariaDB [westos]> UPDATE linux SET  username='hello' WHERE password='123';
 将linux表格中password为123的username更改为hello
Query OK, 1 row affected (1.37 sec)
Rows matched: 1  Changed: 1  Warnings: 0



(14)MariaDB [westos]> SELECT * FROM linux;           显示表格linux里的所有内容

+----------+----------+
| username | password |
+----------+----------+
| hello    | 123      |
| lee      | 456      |
+----------+----------+
2 rows in set (0.00 sec)



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


(16)MariaDB [westos]> SELECT * FROM linux;      显示表格linux里的所有内容

+----------+----------+
| username | password |
+----------+----------+
| lee      | 456      |
+----------+----------+
1 row in set (0.00 sec)


(17)MariaDB [westos]> DROP  table linux;                   删除表linux
Query OK, 0 rows affected (2.27 sec)



(18)MariaDB [westos]> SELECT * FROM linux;                显示表格linux里的所有内容     
ERROR 1146 (42S02): Table 'westos.linux' doesn't exist
MariaDB [westos]> SHOW TABLES;                         显示westos里有什么表格
Empty set (0.00 sec

删除westos数据库

(20)MariaDB [westos]> DROP DATABASE westos;

Query OK, 0 rows affected (0.00 sec)



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

四、用网页管理数据库

   

    

1、 yum install httpd php -y
2 yum install php-mysql.x86_64  -y
3  sysetmctl start httpd
4  cd /var/www/html

5  scp   [email protected]:/home/kiosk/Desktop/phpMyAdmin-3.4.0-all-languages.tar.bz2       /var/www/html
11  tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2
12  ls   
13  rm -fr *.bz2   
14  ls  
15  mv phpMyAdmin-3.4.0-all-languages/  mysqladmin   
16  ls  
17  cd mysqladmin/   
18  ls   
19  cp config.sample.inc.php config.inc.php
浏览器  172.25.254.155/mysqladmin

[root@linux mnt]#



数据库用户管理

用户创建
(1)MariaDB [(none)]> CREATE USER xd@'localhost' identified by 'redhat';    
Query OK, 0 rows affected (0.00 sec)

(2)MariaDB [(none)]> SELECT User FROM mysql.user;              
+------+
| User |
+------+
| root |
| root |
| root |
| xd   |
+------+
4 rows in set (0.00 sec)
用户授权
(3)MariaDB [(none)]> GRANT SELECT on westos.* to xd@localhost;  使xd用户可以查看westos数据库
 
Query OK, 0 rows affected (0.00 sec)



(4)MariaDB [(none)]> SHOW GRANTS FOR xd@localhost;  查看用户授权
+-----------------------------------------------------------------------------------------------------------+
| Grants for xd@localhost                                                                                   |
+-----------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'xd'@'localhost' IDENTIFIED BY PASSWORD '*84BB5DF4823DA319BBF86C99624479A198E6EEE9'                                                           
| GRANT SELECT ON `westos`.* TO 'xd'@'localhost'                                                            |
+--------------------------


(5)MariaDB [(none)]> GRANT UPDATE on westos.* to xd@localhost;
Query OK, 0 rows affected (0.00 sec)



(6)MariaDB [(none)]> GRANT INSERT on westos.* to xd@localhost;
Query OK, 0 rows affected (0.00 sec)



(7)MariaDB [(none)]> REVOKE UPDATE,INSERT on westos.* from  xd@localhost;
    撤销用户权限
Query OK, 0 rows affected (0.00 sec)



(8)MariaDB [(none)]> SHOW GRANTS FOR xd@localhost;


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




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




五、忘记root用户登陆mariadb的密码

root@dns-server mysqladmin]# systemctl stop mariadb          关闭mariadb服务
[root@dns-server mysqladmin]# mysqld_safe  --skip-grant-tables &     跳过授权表
[1] 5362
[root@dns-server mysqladmin]# 180526 02:41:06 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
180526 02:41:06 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

[root@dns-server mysqladmin]# mysql    
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
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.

(1)MariaDB [(none)]> select * from mysql.user;

(2)MariaDB [(none)]> update mysql.user set Password=password('123') where User='root';   更改密码
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

(3)MariaDB [(none)]> select * from mysql.user;

(4)MariaDB [(none)]> quit
Bye
[root@dns-server mysqladmin]# fg
mysqld_safe --skip-grant-tables
^C^Z
[1]+  Stopped                 mysqld_safe --skip-grant-tables
[root@dns-server mysqladmin]# killall -9 mysqld_safe
[1]+  Killed                  mysqld_safe --skip-grant-tables
[root@dns-server mysqladmin]# ps aux | grep mysql
mysql     5517  0.0  9.9 859072 96404 pts/0    Sl   02:41   0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysq --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      5584  0.0  0.0 112640   936 pts/0    R+   02:46   0:00 grep --color=auto mysql
[root@dns-server mysqladmin]# kill -9 5517
[root@dns-server mysqladmin]# ps aux | grep mysql
root      5594  0.0  0.0 112640   936 pts/0    R+   02:47   0:00 grep --color=auto mysql
[root@dns-server mysqladmin]# systemctl start mariadb
[root@dns-server mysqladmin]# mysql -uroot -p123    
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





六、数据库的备份与恢复

 37 mysqladmin -uroot -p123 password 'redhat' 更改密码为redhat

38 mysql -uroot -p123

39 mysql -uroot -predhat

41 mysqldump -uroot -predhat --all-database --no-data

42 mysqldump -uroot -predhat westos > /mnt/westos.sql 备份到/mnt/westos.sql

43 mysql -uroot -predhat

44 mysql -uroot -predhat -e "drop database westos;" 删除数据库westos

45 mysql -uroot -predhat -e "show databases;"

46 ls /mnt


 

第一种恢复 47  vim /mnt/westos.sql
       CREATE DATABASE westos;
       USE westos;
          48  mysql -uroot -predhat < /mnt/westos.sql     
          49  mysql -uroot -predhat -e "show databases;"
 
   51  mysql -uroot -predhat -e "drop database westos;"   删除数据库westos
   52  mysql -uroot -predhat -e "show databases;"



 
第二种恢复    vim /mnt/westos.sql
       /*CREATE DATABASE westos;
       /*USE westos;
   54  mysql -uroot -predhat -e "CREATE DATABASE westos;"
   56  mysql -uroot -predhat westos   < /mnt/westos.sql
   57  mysql -uroot -predhat -e "show databases;"



猜你喜欢

转载自blog.csdn.net/xdmaidou/article/details/80516671