linux-数据库的基本管理

##数据库由很多的库组成,库由表格组成


1.安装

配置yum源(详情请看yum源配置)

 yum search mariadb (查询mariabd服务的软件)

  yum install mariadb-server.x86_64 -y (下载软件)

 

systemctl start mariadb(打开软件)

mysql (默认情况下是所有用户都可以进入)


 netstat -antlpe | grep mysql (查看mysql是否由端口,由端口知道mysql的端口所有网段都可以进入)


 systemctl stop firewalld(关闭防火墙



2.安全初始化

安全初始化:

  vim /etc/my.cnf (打开myaql主配置文件)

 skip-networking=1 (跳过端口设置密码登录) ##关闭网络接口

  

systemctl restart mariadb.service  (刷新配置文件)

netstat -antlpe | grep mysql (再次查看mysql是否有端口)


mysql_secure_installation  (安全初始化脚本直接运行)





 mysql -uroot -p (密码登录 这个需要你设置的密码来进行登录)##较为安全的登陆方法


 mysql -uroot -pxixixi (直接用你的密码xixixi来登录)##安全性能不高,暴露密码,慎用

 


3.查询SQL

MariaDB [(none)]> SHOW DATABASES; (显示这个数据库中的库

+--------------------+

| Database           |   

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.00 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

 


MariaDB [mysql]>  SHOW TABLES; (显示mysql数据库中的所有表格)

+---------------------------+

| 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                      |

+---------------------------+

24 rows in set (0.00 sec)

SELECT * FROM user; (显示来自user表中的所有数据,) 仅截取了一部分

+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+

 

MariaDB [mysql]> SELECT User,Host,password FROM user; (显示user表中的User ,Host ,password 可以清晰的看出他是一个表格的形式)

+------+-----------+-------------------------------------------+

| User | Host      | password                                  |

+------+-----------+-------------------------------------------+

| root | localhost | *29FD948874580361D92BDBAE4A7482F535006F31 |

| root | 127.0.0.1 | *29FD948874580361D92BDBAE4A7482F535006F31 |

| root | ::1       | *29FD948874580361D92BDBAE4A7482F535006F31 |

+------+-----------+-------------------------------------------+

3 rows in set (0.00 sec)

 

MariaDB [mysql]> SELECT User,Host,password,Select_priv FROM user Where User='root';    (显示user表中的User,Host,password,Select_priv(功能开启) Where User='root'字段段User中带有‘root’的)

+------+-----------+-------------------------------------------+-------------+

| User | Host      | password                                  | Select_priv |

+------+-----------+-------------------------------------------+-------------+

| root | localhost | *29FD948874580361D92BDBAE4A7482F535006F31 | Y           |

| root | 127.0.0.1 | *29FD948874580361D92BDBAE4A7482F535006F31 | Y           |

| root | ::1       | *29FD948874580361D92BDBAE4A7482F535006F31 | Y           |

+------+-----------+-------------------------------------------+-------------+

3 rows in set (0.00 sec)


4.数据库的建立

查看数据库中所拥有的库


MariaDB [(none)]> CREATE DATABASE westos; (在数据库中建立一个名字为westos的库)

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES; (显示数据库中的库)

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| westos             |

+--------------------+

 

MariaDB [(none)]> USE westos; (进入westos库)

Database changed

MariaDB [westos]> SHOW TABLES; (查询当前库中的表)

Empty set (0.00 sec)

 

MariaDB [westos]> CREATE TABLE linux(  (建立一个名字最大为6字节,密码最大为50字节的表linux)

    -> username varchar(6) not null,

    -> password varchar(50) not null);

MariaDB [westos]> CREATE TABLE linux( username varchar(6) not null, password varchar(50) not null);

Query OK, 0 rows affected (0.06 sec)

 


 MariaDB [westos]>  SHOW TABLES;

+------------------+

| Tables_in_westos |

+------------------+

| linux            |

+------------------+

1 row in set (0.00 sec)

 

MariaDB [westos]> DESC linux; (查看表格字节


+----------+-------------+------+-----+---------+-------+

| 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 linux values ('lee','123'); (在表格中加入一个名字叫lee 密码为123的

Query OK, 1 row affected (0.06 sec)

MariaDB [westos]> SELECT * FROM linux

    -> ;

+----------+----------+

| username | password |

+----------+----------+

| lee      | 123      |

+----------+----------+

1 row in set (0.00 sec)


在加入一个名字为pipi,密码为777的表格



5.数据库的管理

MariaDB [westos]> ALTER TABLE linux RENAME messages; (改库中表格的名字为messages)

Query OK, 0 rows affected (0.29 sec)

MariaDB [westos]> SHOW TABLES;

+------------------+

| Tables_in_westos |

+------------------+

| messages         |

+------------------+

1 row in set (0.00 sec

 

MariaDB [westos]> ALTER TABLE messages ADD  AGE varchar(50); (建立一个名字为AGE,字长为50的表)

Query OK, 2 rows affected (0.47 sec)               

Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]> DESC messages;

+----------+-------------+------+-----+---------+-------+

| Field    | Type        | Null | Key | Default | Extra |

+----------+-------------+------+-----+---------+-------+

| username | varchar(6)  | NO   |     | NULL    |       |

| password | varchar(50) | NO   |     | NULL    |       |

| AGE      | varchar(50) | YES  |     | NULL    |       |

+----------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

 

MariaDB [westos]> ALTER TABLE messages ADD hehe varchar(50) AFTER username; (创建一个名字为hehe,最大字节为50的,把这个字段排在username后边)

Query OK, 2 rows affected (0.12 sec)               

Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]>  DESC messages;

+----------+-------------+------+-----+---------+-------+

| Field    | Type        | Null | Key | Default | Extra |

+----------+-------------+------+-----+---------+-------+

| username | varchar(6)  | NO   |     | NULL    |       |

| hehe     | varchar(50) | YES  |     | NULL    |       |

| password | varchar(50) | NO   |     | NULL    |       |

| AGE      | varchar(50) | YES  |     | NULL    |       |

+----------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

 

MariaDB [westos]> ALTER TABLE messages DROP AGE; (删除字段AGE)

Query OK, 2 rows affected (0.32 sec)               

Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]>  DESC messages;

+----------+-------------+------+-----+---------+-------+

| Field    | Type        | Null | Key | Default | Extra |

+----------+-------------+------+-----+---------+-------+

| username | varchar(6)  | NO   |     | NULL    |       |

| hehe     | varchar(50) | YES  |     | NULL    |       |

| password | varchar(50) | NO   |     | NULL    |       |

+----------+-------------+------+-----+---------+-------+

3 rows in set (0.01 sec)

 

MariaDB [westos]> UPDATE messages SET password=‘123’ WHERE username=‘lee’; (加一个表里的数据

ERROR 1054 (42S22): Unknown column '‘lee’' in 'where clause'

MariaDB [westos]> SELECT * FROM messages

    -> ;

+----------+----------+

| username | password |

+----------+----------+

| lee      | 123      |

| pipi     | 777      |

+----------+----------+

2 rows in set (0.00 sec)

 

MariaDB [westos]> DELETE FROM messages WHERE username='lee';

Query OK, 1 row affected (0.03 sec)

MariaDB [westos]> SELECT * FROM messages (删除表里的数据)

    -> ;

+----------+----------+

| username | password |

+----------+----------+

| pipi     | 777      |

+----------+----------+

1 row in set (0.00 sec)

 

MariaDB [westos]> DROP TABLE messages; (删除库)

Query OK, 0 rows affected (0.30 sec)

MariaDB [westos]> SELECT * FROM messages

    -> ;

ERROR 1146 (42S02): Table 'westos.messages' doesn't exis

 

MariaDB [westos]> DROP DATABASE westos;(删除数据库)

Query OK, 0 rows affected (0.00 sec)


6.用phpMyAdmin管理数据库

yum install php httpd -y (安装php数据库图形管理和httpd服务 )



systemctl start httpd(打开httpd)

systemctl stop firewalld(关闭防火墙)

 cd /var/www/html/



tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 (解压文件)

ls  

mv phpMyAdmin-3.4.0-all-languages mysqladmin (改名字为mysqladmin )



 cd mysqladmin

 php -m(列出php支持的任务)


 yum search php(查看关于php-mysql的文件)


 yum install php-mysql.x86_64 (下载php)


 systemctl restart httpd



测试: firefox 输入ip:172.25.254.135/mysqlmin来测试

登陆



创建一个数据库


在建立一个数据表



在虚拟机中命令查看是否创建成功

MariaDB [(none)]> SHOW DATABASES; (查看数据库)

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

MariaDB [pipigui]> SHOW TABLES; (查看数据表)


MariaDB [pipigui]> DESC hehe; (查看数据表中的数据是否正确)


7.用户管理

@localhost(为只允许本地用户登录)  @'%' 任意远程用户

MariaDB [(none)]> CREATE USER  yitu@localhost identified by 'xixixi'(建立一个名字叫做yitu的本地用户

MariaDB [(none)]> SELECT User FROM mysql.user;(查看用户)

MariaDB [(none)]> GRANT SELECT on pipigui.* to yitu@localhost;(给yitu用户加一个SELECT权限对于pipigui表格,可以看到你写的表格)


MariaDB [(none)]> GRANT UPDATE on pipigui.* to yitu@localhost;(编辑表格内容的权力)

 MariaDB [(none)]> GRANT DELETE on pipigui.* to yitu@localhost;(删除的权力,编辑删除权限都是编辑的内容)

 


MariaDB [(none)]> REVOKE UPDATE on pipigui.* from yitu@localhost(移走权限)

 MariaDB [(none)]> SHOW GRANTS FOR yitu@localhost; (刷新)




MariaDB [(none)]> REVOKE SELECT,DELETE on pipigui.* from yitu@localhost;

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




MariaDB [(none)]> DROP USER yitu

    -> ;

ERROR 1396 (HY000): Operation DROP USER failed for 'yitu'@'%'

MariaDB [(none)]> DROP USER yitu@localhost

    -> ;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SELECT user from mysql.user(删除用户)



8.超级用户密码的破译

[root@localhost ~]# systemctl stop mariadb.service

[root@localhost ~]# mysqld_safe --skip-grant-tables & (跳过后台直接登录并且把它打入后台)

[root@localhost ~]# mysql 可以直接进入

MariaDB [(none)]> update mysql.user set Password='hehehe' where User='root' (把mysqluserb表中的密码后面改称‘hehehe’ 在用户root中)


MariaDB [(none)]> select * from mysql.user; (可以查看到没有加密的密码已经有了但没有加密)


MariaDB [(none)]> update mysql.user set Password=password('hehehe') where User='root';(给密码加密)

MariaDB [(none)]> select * from mysql.user; (可以查看到密码已经有了加密)


[root@localhost ~]# fg(打入前台)

[root@localhost ~]# killall -9 mysqld_safe (关闭)

[1]+  Killed                  mysqld_safe --skip-grant-tables

root@localhost ~]# ps aux | grep mysql (查看mysql的端口)


[root@localhost ~]# kill -9  5730(关闭端口)


root@localhost ~]# systemctl start mariadb (开启服务)

[root@localhost ~]# mysql -uroot -phehehe  (可以直接登录,密码修改成功)


9.数据库的备份和恢复

[root@localhost ~]# mysqldump -uroot -phehehe --all-database (备份的数据库)

[root@localhost ~]# mysqldump -uroot -phehehe --all-database --no-data (备份框架)

[root@localhost ~]# mysqldump -uroot -phehehe -e pipigui > /mnt/pipigui.sql (把pipigui库备份到/mnt/pipigui.sql中)

[root@localhost ~]# mysql -uroot -phehehe -e "show databases;"(-e查看数据库中的库)

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| pipigui            |

+--------------------+

将库备份到/mnt/pipigui.sql


删除库


恢复的第一种方法:

root@localhost ~]# cd /mnt/

[root@localhost mnt]# ls

hehe.com.zone  Khehe.+157+51046.key      pipigui.sql

hehehe.sql     Khehe.+157+51046.private

[root@localhost mnt]# vim pipigui.sql (打开配置文件,加上命名的前两部)


hehe < /mnt/pipigui.sql (将备份的pipigui库导入回来)

第二种恢复方法:

root@localhost ~]# mysql -uroot -phehehe -e "drop database pipigui;" (删除pipiggui库

root@localhost mnt]# mysql -uroot -phehehe  -e "CREATE DATABASE pipigui;" (创建一个pipigui库)

ERROR 1007 (HY000) at line 1: Can't create database 'pipigui'; database exists

[root@localhost mnt]# mysql -uroot -phehehe pipigui < /mnt/pipigui.sql (在将备份的pipigui库导回来)

 

[root@localhost mnt]# mysql -uroot -phehehe -e "show databases;"

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| pipigui            |

+--------------------+


猜你喜欢

转载自blog.csdn.net/a939029674/article/details/80488089