Mariadb的简单使用

本文详细介绍了如何在CentOS 7中安装、配置和管理MariaDB数据库,包括配置文件管理、服务启动、基本操作如创建数据库、用户和表,以及备份与恢复。重点展示了数据库的初始化、权限设置和实用命令演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

Mariadb数据库管理系统是mysql的一个分支,原因:甲骨文公司收购mysql后,有将mysq闭源的潜在风险,因此社区采用分支的方式来避开这个风险。

源配置

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vim mariadb.repo
[mariadb]
name=MariaDB
baseurl=https://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

[root@localhost yum.repos.d]# yum clean all
已加载插件:fastestmirror
正在清理软件源: base extras mariadb updates
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum makecache

安装mariadb

[root@localhost yum.repos.d]# yum install -y mariadb-server mariadb-client

开启mariadb并设置开机自启

[root@localhost yum.repos.d]# systemctl start mariadb
[root@localhost yum.repos.d]# systemctl enable mariadb
查看端口开放
[root@localhost ~]# netstat -ntlp |grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      41821/mysqld   

初始化数据库

[root@localhost yum.repos.d]# mysql_secure_installation
直接回车
y 设置root密码
y 删除匿名用户
n 禁用root远程登录
y 删除test数据库
y 重新加载权限表
[root@localhost yum.repos.d]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.4.21-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

修改数据库的密码

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

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> set password = PASSWORD('newpassword');

简单了解数据库

创建数据库mytestdb
MariaDB [(none)]> create database mytestdb;
Query OK, 1 row affected (0.000 sec)

进入数据库
MariaDB [(none)]> use mytestdb;
Database changed

创建一个带字段的表
MariaDB [mytestdb]> create table mytable(id int,name char(32));
Query OK, 0 rows affected (0.007 sec)

查看表结构
MariaDB [mytestdb]> desc mytable;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| name  | char(32) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.001 sec)

表内插入值
MariaDB [mytestdb]> insert into mytable(id,name) values(1,"zero"),(2,"one");
Query OK, 2 rows affected (0.003 sec)
Records: 2  Duplicates: 0  Warnings: 0

筛选查看表数据
MariaDB [mytestdb]> select id,name from mytable;
+------+------+
| id   | name |
+------+------+
|    1 | zero |
|    2 | one  |
+------+------+
2 rows in set (0.001 sec)

根据id删除表内的值
MariaDB [mytestdb]> delete from mytable where id=2;
Query OK, 1 row affected (0.002 sec)

MariaDB [mytestdb]> select id,name from mytable;
+------+------+
| id   | name |
+------+------+
|    1 | zero |
+------+------+
1 row in set (0.001 sec)

根据id更新表内的值
MariaDB [mytestdb]> update mytable set name="ten" where id=1;
Query OK, 1 row affected (0.002 sec)
Rows matched: 1  Changed: 1  Warnings: 0

数据库的备份与恢复

新建数据库测试
MariaDB [mytestdb]> create database testdb;
Query OK, 1 row affected (0.000 sec)

MariaDB [mytestdb]> create database webdb;
Query OK, 1 row affected (0.000 sec)

查看所有数据库
MariaDB [mytestdb]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mytestdb           |
| performance_schema |
| testdb             |
| webdb              |
+--------------------+
7 rows in set (0.000 sec)

MariaDB [mytestdb]> exit
Bye

备份

单个数据库备份
[root@localhost ~]# mysqldump -u root -p testdb > /tmp/db.dump
Enter password: 

全部数据库备份
[root@localhost ~]# mysqldump -u root -p --all-databases > /tmp/db.dump

进入数据库将数据库删除

MariaDB [(none)]> drop database mytestdb;
Query OK, 1 row affected (0.008 sec)

MariaDB [(none)]> drop database webdb;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> drop database testdb;
Query OK, 1 row affected (0.004 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)

恢复

[root@localhost ~]# mysql -uroot -p < /tmp/db.dump
Enter password: 

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

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation 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              |
| mytestdb           |
| mytestdb1          |
| performance_schema |
| testdb             |
| webdb              |
+--------------------+
7 rows in set (0.001 sec)

作者:月光染衣袂
转发请加上该地址 : https://blog.youkuaiyun.com/weixin_46860149/article/details/119841594
如果笔记对您有用,请帮忙点个赞!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值