Mariadb基础
- 1、Mariadb安装与配置
- 1.1Mariadb安装
- 1.2 Mariadb配置
- 1.3 SQL语句
- 2、MySQL 客户端工具使用
- 2.1 MySQL程序组成
- 2.2 MySQL监听地址
- 2.3 客户端工具使用
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语句类型 | 对应操作 |
DDL | CREATE:创建 DROP:删除 ALTER:修改 |
DML | INSERT:向表中插入数据 DELETE:删除表中数据 UPDATE:更新表中数据 SELECT:查询表中数据 |
DCL | GRANT:授权 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 |
+--------------------+