/路径/文件名.sql】information_schema :【默认】:系统运行的状态信息,性能信息的存储库;2.-B 数据库名 #备份指定数据库的数据信息。-B 数据库名 #备份指定数据库的数据信息。注意:需要先关闭数据库进行操作。">
当前位置:   article > 正文

Linux~MySQL数据库具体操作

Linux~MySQL数据库具体操作

一、数据库的字符集编码设置

 (一)查看数据库默认的字符集

  1. MariaDB [(none)]> show variables like '%character%';
  2. +--------------------------+----------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+----------------------------+
  5. | character_set_client | utf8mb4 |
  6. | character_set_connection | utf8mb4 |
  7. | character_set_database | utf8mb4 |
  8. | character_set_filesystem | binary |
  9. | character_set_results | utf8mb4 |
  10. | character_set_server | utf8mb4 |
  11. | character_set_system | utf8 |
  12. | character_sets_dir | /usr/share/mysql/charsets/ |
  13. +--------------------------+----------------------------+
  14. 8 rows in set (0.00 sec)
  15. MariaDB [(none)]>

(二)永久设置字符集

  1. #1,服务端配置文件配置字符集
  2. [root@10 code]# vim /etc/my.cnf
  3. 。。。。。
  4. [mysqld]
  5. character-set-server=utf8mb4
  6. 。。。。。
  7. #2,客户端配置文件配置字符集
  8. [root@10 ~]# vim /etc/my.cnf.d/client.cnf
  9. ......
  10. [client]
  11. default-character-set=utf8mb4
  12. ......
  13. [client-mariadb]
  14. default-character-set=utf8mb4
  15. ......
  16. #3,重启数据库服务
  17. [root@10 ~]# systemctl restart mariadb.service
  18. #4,验证是否配置成功
  19. MariaDB [(none)]> create database newDataTest01
  20. -> ;
  21. Query OK, 1 row affected (0.00 sec)
  22. MariaDB [(none)]> show create database newDataTest01;
  23. +---------------+---------------------------------------------------------------------------+
  24. | Database | Create Database |
  25. +---------------+---------------------------------------------------------------------------+
  26. | newDataTest01 | CREATE DATABASE `newDataTest01` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
  27. +---------------+---------------------------------------------------------------------------+
  28. 1 row in set (0.00 sec)
  29. MariaDB [(none)]>

二、数据库的CURD

(一)创建数据数据库

1.创建数据库方式一(内部):
  1. MariaDB [(none)]> create database newDataTest02 character set utf8;
  2. Query OK, 1 row affected (0.00 sec)
  3. MariaDB [(none)]> show databases;
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | mysql |
  9. | mysqlTest03 |
  10. | mysqlTest04 |
  11. | mysqlTest05 |
  12. | newDataTest01 |
  13. | newDataTest02 |
  14. | performance_schema |
  15. | phpshop |
  16. | test |
  17. | wordpress |
  18. | world |
  19. +--------------------+
  20. 12 rows in set (0.00 sec)
  21. MariaDB [(none)]>
2.创建数据库方式二(外部):

第一种:mysqladmin -u root -p create my_new_database

  1. [root@10 code]# mysqladmin -uroot -p1 create newDataTest03
  2. [root@10 code]# mysqladmin -uroot -p1 create newDataTest04
  3. [root@10 code]#
  4. MariaDB [(none)]> show databases;
  5. +--------------------+
  6. | Database |
  7. +--------------------+
  8. | information_schema |
  9. | mysql |
  10. | mysqlTest03 |
  11. | mysqlTest04 |
  12. | mysqlTest05 |
  13. | newDataTest01 |
  14. | newDataTest02 |
  15. | newDataTest03 |
  16. | newDataTest04

第二种:mysql -u root -p -e "CREATE DATABASE my_new_database;"

  1. [root@10 code]# mysql -uroot -p1 -e "create database newDataTest05"
  2. [root@10 code]# mysql -uroot -p1 -e "create database newDataTest06"
  3. [root@10 code]#
  4. MariaDB [(none)]> show databases;
  5. +--------------------+
  6. | Database |
  7. +--------------------+
  8. | information_schema |
  9. | mysql |
  10. | mysqlTest03 |
  11. | mysqlTest04 |
  12. | mysqlTest05 |
  13. | newDataTest01 |
  14. | newDataTest02 |
  15. | newDataTest03 |
  16. | newDataTest04 |
  17. | newDataTest05 |
  18. | newDataTest06

(二)修改数据库

1.修改数据库字符集

第一种方法:

  1. MariaDB [(none)]> create database newDataTest03 character set utf8;
  2. MariaDB [(none)]> show create database newDataTest03 ;
  3. +----------+-----------------------------------------------------------------+
  4. | Database | Create Database |
  5. +----------+-----------------------------------------------------------------+
  6. | newDataTest03 | CREATE DATABASE `newDataTest03 ` /*!40100 DEFAULT CHARACTER SET utf8 */ |
  7. +----------+-----------------------------------------------------------------+

第二种方法:

  1. #2,创建数据库,指定字符集
  2. MariaDB [(none)]> create database newDataTest05 character set utf8;
  3. MariaDB [(none)]> show create database newDataTest05 ;
  4. +----------+-------------------------------------------------------------------+
  5. | Database | Create Database |
  6. +----------+-------------------------------------------------------------------+
  7. | newDataTest05 | CREATE DATABASE `newDataTest05 ` /*!40100 DEFAULT CHARACTER SET utf8 */ |
  8. +----------+-------------------------------------------------------------------+

(三)查询数据库

1.查看数据库方式一:

information_schema :【默认】:系统运行的状态信息,性能信息的存储库;

mysql :  #【默认】:授权权限,用户管理的数据库

performance_schema : #【默认】:系统运行的状态信息,性能信息的存储库;

test : #测试库,让用户先测试使用的;

  1. MariaDB [(none)]> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | mysqlTest03 |
  8. | mysqlTest04 |
  9. | mysqlTest05 |
  10. | newDataTest01 |
2.查看指定的数据库(模糊查询):
  1. ##查看指定的库
  2. MariaDB [(none)]> show databases like "%newData%";
  3. +----------------------+
  4. | Database (%newData%) |
  5. +----------------------+
  6. | newDataTest01 |
  7. | newDataTest02 |
  8. | newDataTest03 |
  9. | newDataTest04 |
  10. | newDataTest05 |
  11. | newDataTest06 |
  12. +----------------------+
  13. 6 rows in set (0.00 sec)
  14. MariaDB [(none)]>
3.查看创建数据时的SQL语句信息
  1. MariaDB [(none)]> show create database newDataTest01
  2. -> ;
  3. +---------------+---------------------------------------------------------------------------+
  4. | Database | Create Database |
  5. +---------------+---------------------------------------------------------------------------+
  6. | newDataTest01 | CREATE DATABASE `newDataTest01` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
  7. +---------------+---------------------------------------------------------------------------+
  8. 1 row in set (0.00 sec)
  9. MariaDB [(none)]>

(四)删除数据库

drop database test;

三、数据库表结构CURD

(一)表结构新增字段

语法:【alter  table 表名  add  新增字段名  新增字段类型  not  null  comment '此用户比较好'】

(二)查看数据库表结构

  1. MariaDB [newDataTest01]> desc demoUser
  2. -> ;
  3. +-------+-------------+------+-----+---------+-------+
  4. | Field | Type | Null | Key | Default | Extra |
  5. +-------+-------------+------+-----+---------+-------+
  6. | id | int(10) | YES | | NULL | |
  7. | name | varchar(20) | YES | | NULL | |
  8. | age | tinyint(3) | YES | | NULL | |
  9. | sex | varchar(3) | YES | | NULL | |
  10. +-------+-------------+------+-----+---------+-------+
  11. 4 rows in set (0.00 sec)
  12. MariaDB [newDataTest01]>

(三)修改表名称

  1. MariaDB [newDataTest01]> rename table demoUser to demo01
  2. -> ;
  3. Query OK, 0 rows affected (0.00 sec)
  4. MariaDB [newDataTest01]> show tables;
  5. +-------------------------+
  6. | Tables_in_newDataTest01 |
  7. +-------------------------+
  8. | demo01 |
  9. +-------------------------+
  10. 1 row in set (0.00 sec)
  11. MariaDB [newDataTest01]>

四、数据库表数据CURD

(一)创建数据库表

  1. MariaDB [newDataTest01]> create table demoUser(
  2. id int(10),
  3. name varchar(20),
  4. age tinyint(3),
  5. sex varchar(3)
  6. );
  7. Query OK, 0 rows affected (0.00 sec)
  8. MariaDB [newDataTest01]>
  1. #1,语法一插入方式:
  2. MariaDB [newDataTest01]> insert into demo01(id,name,age,sex) value (1,"name",22,'男');
  3. Query OK, 1 row affected (0.01 sec)

(二)更新表数据

  1. MariaDB [newDataTest01]> select * from demo01;
  2. +------+------+------+------+
  3. | id | name | age | sex |
  4. +------+------+------+------+
  5. | 1 | name | 22 | 男 |
  6. +------+------+------+------+
  7. 1 row in set (0.00 sec)
  8. MariaDB [newDataTest01]> update demo01 set age = 2222 where id = 1;
  9. Query OK, 1 row affected, 1 warning (0.00 sec)
  10. Rows matched: 1 Changed: 1 Warnings: 1
  11. MariaDB [newDataTest01]> select * from demo01;
  12. +------+------+------+------+
  13. | id | name | age | sex |
  14. +------+------+------+------+
  15. | 1 | name | 127 | 男 |
  16. +------+------+------+------+
  17. 1 row in set (0.00 sec)
  18. MariaDB [newDataTest01]>

(三)查询表数据

  1. MariaDB [newDataTest01]> select * from demo01;
  2. +------+------+------+------+
  3. | id | name | age | sex |
  4. +------+------+------+------+
  5. | 1 | name | 127 | 男 |
  6. +------+------+------+------+
  7. 1 row in set (0.00 sec)
  8. MariaDB [newDataTest01]>

(四)删除表数据

  1. MariaDB [newDataTest01]> select * from demo01;
  2. +------+-------+------+------+
  3. | id | name | age | sex |
  4. +------+-------+------+------+
  5. | 1 | name | 127 | 男 |
  6. | 2 | name2 | 2 | 男 |
  7. +------+-------+------+------+
  8. 2 rows in set (0.00 sec)
  9. MariaDB [newDataTest01]> delete from demo01 where id = 2;
  10. Query OK, 1 row affected (0.00 sec)
  11. MariaDB [newDataTest01]> select * from demo01;
  12. +------+------+------+------+
  13. | id | name | age | sex |
  14. +------+------+------+------+
  15. | 1 | name | 127 | 男 |
  16. +------+------+------+------+
  17. 1 row in set (0.00 sec)
  18. MariaDB [newDataTest01]>

五、数据库服务的备份

(一)查看数据库数据目录

  1. [root@10 code]# ll /var/lib/mysql
  2. 总用量 28712
  3. -rw-rw---- 1 mysql mysql 16384 8月 15 10:55 aria_log.00000001
  4. -rw-rw---- 1 mysql mysql 52 8月 15 10:55 aria_log_control
  5. -rw-rw---- 1 mysql mysql 18874368 8月 15 21:32 ibdata1
  6. -rw-rw---- 1 mysql mysql 5242880 8月 15 21:32 ib_logfile0
  7. -rw-rw---- 1 mysql mysql 5242880 8月 9 10:16 ib_logfile1
  8. drwx------ 2 mysql mysql 4096 8月 13 15:28 mysql
  9. srwxrwxrwx 1 mysql mysql 0 8月 15 13:11 mysql.sock
  10. drwx------ 2 mysql mysql 56 8月 13 11:04 mysqlTest03
  11. drwx------ 2 mysql mysql 20 8月 13 10:40 mysqlTest04
  12. drwx------ 2 mysql mysql 20 8月 13 10:41 mysqlTest05
  13. drwx------ 2 mysql mysql 38 8月 15 21:13 newDataTest01
  14. drwx------ 2 mysql mysql 20 8月 15 20:22 newDataTest02
  15. drwx------ 2 mysql mysql 20 8月 15 20:27 newDataTest03
  16. drwx------ 2 mysql mysql 20 8月 15 20:27 newDataTest04
  17. drwx------ 2 mysql mysql 20 8月 15 20:29 newDataTest05
  18. [root@10 code]#

(二)备份数据库~物理备份

注意:需要先关闭数据库进行操作

  1. [root@10 ~]# tar zcvf sql-`date +%F`.tar.gz /var/lib/mysql
  2. [root@10 ~]# ll
  3. 总用量 1164
  4. -rw-r--r-- 1 root root 1534901 8月 15 21:41 sql-2024-08-15.tar.gz

(三)备份数据库~逻辑备份

mysqldump命令;

语法:【mysqldump -uroot -p1 [备份参数] > /路径/文件名.sql】

  • 参数

    • -A #全备,备份所有数据库的数据信息

    • -B 数据库名 #备份指定数据库的数据信息

    • -F #备份启动之前,自动刷新日志文件(落盘);

 1.-A #全备,备份所有数据库的数据信息

  1. #【-A】全备
  2. [root@10 tmp]# mysqldump -uroot -p1 -A > ./qb.sql
  3. [root@10 tmp]# ll
  4. 总用量 1912
  5. -rw-r--r-- 1 root root 1302576 8月 15 22:02 qb.sql

2.-B 数据库名 #备份指定数据库的数据信息

  1. #【-B】指定库备份
  2. [root@10 tmp]# mysqldump -uroot -p1 -B newDataTest01 newDataTest02 > ./kb.sql
  3. [root@10 tmp]#
  4. [root@wa ~]# ll
  5. 总用量 2156
  6. -rw-r--r-- 1 root root 2269 8月 15 22:04 kb.sql

 3.-F #备份启动之前,自动刷新日志文件(落盘);

  1. #指定数据库中的部分数据表进行备份
  2. [root@10 ~]# mysqldump -uroot -p1 school sc teacher > ./bb.sql
  3. [root@10 ~]# ll
  4. 总用量 2160
  5. -rw-r--r-- 1 root root 2864 8月 13 16:24 bb.sql
  6. -rw-r--r-- 1 root root 247769 8月 13 16:22 kb.sql
  7. -rw-r--r-- 1 root root 762495 8月 13 16:22 qb.sql

(四)删库恢复数据

(五)使用备份恢复数据

  1. MariaDB [school]> source ~/bb.sql
  2. MariaDB [school]> show tables;
  3. +------------------+
  4. | Tables_in_school |
  5. +------------------+
  6. | course |
  7. | sc |
  8. | student |
  9. | teacher |
  10. +------------------+
  11. 4 rows in set (0.00 sec)

(六)登录前恢复导入

  1. [root@10 ~]# mysql -uroot -p1 < ~/qb.sql
  2. [root@10 ~]# mysql -uroot -p1 -e "show databases"
  3. +--------------------+
  4. | Database |
  5. +--------------------+
  6. | information_schema |
  7. | mysql |
  8. | harbor |
  9. | performance_schema |
  10. | school |
  11. | world |
  12. +--------------------+

(七)删库中的表

六、导入数据数据

(一)将world.sql文件导入到数据库中

  1. #第一种方式:
  2. [root@10 ~]# mysql -uroot -p1 < ./world.sql
  3. [root@10 ~]# mysql -uroot -p1 -e "show databases"
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | mysql |
  9. | harbor |
  10. | performance_schema |
  11. | world |
  12. +--------------------+

(二)在数据库里面导入

MariaDB [(none)]> source ./world.sql;

七、拓展~数据库联表查询

(一)内连拼接查询

  1. #语法:
  2. #第一种:【select * from 表1,表2 where 表1.字段=表2.字段】
  3. MariaDB [school]> select * from teacher,course where teacher.tno=course.tno;
  4. +-----+-----------+------+--------+-----+
  5. | tno | tname | cno | cname | tno |
  6. +-----+-----------+------+--------+-----+
  7. | 101 | 张老师 | 1001 | linux | 101 |
  8. | 102 | 李老师 | 1002 | python | 102 |
  9. | 103 | 王老师 | 1003 | golang | 103 |
  10. | 104 | 赵老师 | 1004 | java | 104 |
  11. +-----+-----------+------+--------+-----+
  12. #第二种:【select * from 表1 join 表2 on 表1.字段=表2.字段】
  13. MariaDB [school]> select * from teacher join course on teacher.tno=course.tno;
  14. +-----+-----------+------+--------+-----+
  15. | tno | tname | cno | cname | tno |
  16. +-----+-----------+------+--------+-----+
  17. | 101 | 张老师 | 1001 | linux | 101 |
  18. | 102 | 李老师 | 1002 | python | 102 |
  19. | 103 | 王老师 | 1003 | golang | 103 |
  20. | 104 | 赵老师 | 1004 | java | 104 |
  21. +-----+-----------+------+--------+-----+

(二)左右外连接查询

  1. #左外连接:以左边的表为基准表,做拼接
  2. MariaDB [school]> select * from teacher left join course on teacher.tno=course.tno;
  3. +-----+-----------+------+--------+------+
  4. | tno | tname | cno | cname | tno |
  5. +-----+-----------+------+--------+------+
  6. | 101 | 张老师 | 1001 | linux | 101 |
  7. | 102 | 李老师 | 1002 | python | 102 |
  8. | 103 | 王老师 | 1003 | golang | 103 |
  9. | 104 | 赵老师 | 1004 | java | 104 |
  10. +-----+-----------+------+--------+------+
  11. #右外连接:以右边的表为基准表,做拼接;
  12. MariaDB [school]> select * from teacher right join course on teacher.tno=course.tno;
  13. +------+-----------+------+--------+-----+
  14. | tno | tname | cno | cname | tno |
  15. +------+-----------+------+--------+-----+
  16. | 101 | 张老师 | 1001 | linux | 101 |
  17. | 102 | 李老师 | 1002 | python | 102 |
  18. | 103 | 王老师 | 1003 | golang | 103 |
  19. | 104 | 赵老师 | 1004 | java | 104 |
  20. | NULL | NULL | 1005 | c++ | 105 |
  21. +------+-----------+------+--------+-----+

(三)联合查询union

  1. MariaDB [school]> select * from sc where sno=1 union select * from sc where sno=9;
  2. +-----+------+-------+
  3. | sno | cno | score |
  4. +-----+------+-------+
  5. | 1 | 1001 | 80 |
  6. | 1 | 1003 | 56 |
  7. | 9 | 1003 | 76 |
  8. +-----+------+-------+

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/1003405
推荐阅读
相关标签
  

闽ICP备14008679号