Mariadb基础

本文详细介绍了如何在Redhat系统中安装和配置Mariadb,包括使用yum命令安装,启动数据库,设置登录密码等步骤。此外,还讲解了MySQL客户端工具的使用,如mysql命令行客户端,安全初始化,以及连接数据库的各种选项。同时,概述了SQL语句的三大类型:DDL、DML和DCL。
摘要由CSDN通过智能技术生成

1、Mariadb安装与配置


1.1Mariadb安装

  • Mariadb和MySQL操作语法是通用的,但是现在MySQL是收费的,Mariadb免费的。所以这里我们安装Mariadb

  • Mariadb安装的方式有三种:

    1.源代码:编译安装
    2. 二进制格式的程序包:展开至特定 路径, 并经过简单配置后即可使用
    3.程序包管理器管理的程序包

//在Redhat 8中本地源中已经存在mariadb安装包,所以可以直接使用yum命令安装
[root@localhost ~]# yum -y install mariadb mariadb-common mariadb-devel mariadb-server

在Redhat7中安装MySQL

#配置mysql的yum源
wget -O /usr/src/mysql57-community-release-el7-10.noarch.rpm http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
rpm -Uvh /usr/src/mysql57-community-release-el7-10.noarch.rpm

#安装mysql5.7
yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel

1.2 Mariadb配置

//启动数据库
[root@localhost ~]# systemctl enable --now mariadb

//确保3306端口已经处于监听状态
[root@localhost ~]# ss -antl|grep 3306
LISTEN   0         80                        *:3306                   *:*  

//登录mariadb数据库
[root@localhost ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-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)]> 

//修改Mariadb登录密码
MariaDB [(none)]> set password = password('redhat123!');

//重新使用密码登,以下两种方法都可以登录
[root@localhost ~]# mysql -uroot -p'redhat123!'
[root@localhost ~]# mysql -uroot -p 
Enter password: 

1.3 SQL语句

SQL语句有三种类型:

  • DDL:Data Defination Language,数据定义语言
  • DML:Data Manipulation Language,数据操纵语言
  • DCL:Data Control Language,数据控制语言
SQL语句类型对应操作
DDLCREATE:创建
DROP:删除
ALTER:修改
DMLINSERT:向表中插入数据
DELETE:删除表中数据
UPDATE:更新表中数据
SELECT:查询表中数据
DCLGRANT:授权
REVOKE:移除授权

2、MySQL 客户端工具使用


2.1 MySQL程序组成

客户端

  • mysql:CLI交互式客户端程序
  • mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
  • mysqldump:mysql备份工具
  • mysqladmin

服务端

  • mysqld

2.2 MySQL监听地址

socket类型说明
ip socket默认监听在tcp的3306端口,支持远程通信
unix sock监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock)

仅支持本地通信
server地址只能是:localhost,127.0.0.1 |

2.3 客户端工具使用

//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
    -uUSERNAME      //指定用户名,默认为root
    -hHOST          //指定服务器主机,默认为localhost,推荐使用ip地址
    -pPASSWORD      //指定用户的密码
    -P#             //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
    -V              //查看当前使用的mysql版本
    -e              //不登录mysql执行sql语句后退出,常用于脚本


[root@localhost ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper


[root@localhost ~]# mysql -uroot -predhat123! -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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


//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码
[root@localhost ~]# mysql -uroot -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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


//不进入数据库执行sql语句
[root@localhost ~]# mysql -uroot -p -h 127.0.0.1 -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值