linux——数据库mariadb的基础操作
一、数据库的安装及初始安全配置
[root@localhost ~]# yum install mariadb-server -y
##安装数据库服务软件
##安装过程略
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# netstat -antlpe | grep mysql ##列出数据库的工作端口
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27 50330 2327/mysqld
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# vim /etc/my.cnf10 skip-networking=1 ##添加第10行,作用直接跳过端口设置,不生成端口
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# netstat -antlpe | grep mysql
[root@localhost ~]# mysql ##不需密码可以直接进入数据库,不安全
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
[root@localhost ~]# mysql_secure_installation ##第一次安装mysql,通过这条命令对mysql进行设置
[root@localhost ~]# mysql -uroot -predhat
##从本机登录mysql数据库(不建议这种方式,密码可见,不安全)
##进入数据库,-u是登陆用户,-p是用户密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
[root@localhost ~]# mysql -uroot -p ##从本机登录mysql数据库,此种方式较为安全
Enter password: ##输入密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
二、数据库的基本SQL语句
以单行或多行书写,以分号结尾
可使用空格和缩进来增强语句的可读性
书写不区分大小写,建议使用大写,如:SELECT User,Host,Password FROM user;
同样可以使用/* */的方式完成注释
1、查询语句
查询语句介绍
SHOW DATABASES;
##查看数据库MySQL服务器中所有的数据库
USE mysql;
##切换数据库:USE 数据库名
SHOW TABLES;
##查看数据库中的所有表
SELECT * FROM user;
##查询user表中的所有字段
SELECT User,Host,Password FROM user;
##查询user表中的host,user,password字段
SELECT User,Host,Password,Select_priv FROM user;
##查询user表中的host,user,password,Select_priv字段
DESC user;
##查看user表的数据结构
SELECT User,Host,Password,Select_priv FROM user Where User='root';
##查询user表中的User为root的host,user,password,Select_priv字段
##Where [条件]
SELECT User,Host,Password,Select_priv FROM user Where User='root' AND Host='localhost';
##查询user表中的User为root、Host为localhost的host,user,password,Select_priv字段
查询语句效果显示
SHOW DATABASES; ##查看数据库MySQL服务器中所有的数据库
USE mysql; ##切换数据库:USE 数据库名
SHOW TABLES; ##查看数据库中的所有表
SELECT * FROM user; ##查询user表中的所有字段(命令执行后内容较多,省略不显示)
SELECT User,Host,Password FROM user; ##查询user表中的host,user,password字段
SELECT User,Host,Password,Select_priv FROM user; ##查询user表中的host,user,password,Select_priv字段
DESC user; ##查看user表的数据结构
SELECT User,Host,Password,Select_priv FROM user Where User='root';
##查询user表中的User为root的host,user,password,Select_priv字段
##Where [条件]
SELECT User,Host,Password,Select_priv FROM user Where User='root' AND Host='localhost';
##查询user表中的User为root、Host为localhost的host,user,password,Select_priv字段
2、数据库以及表的创建
CREATE DATABASE westos; ##创建数据库westos
CREATE TABLE linux(-> username varchar(6) not null,-> password varchar(50) not null); ##创建表,username,password字段
CREATE TABLE linux(username varchar(6) not null,password varchar(50) not null); ##创建表,username,password字段
##上面两条命令一样,第一条换行,第二条没有换行
INSERT INTO linux value ('tutu','123'); ##在linux表中插入值为username = tutu,password = 123
INSERT INTO linux value ('butter','234'); ##在linux表中插入值为username = butter,password = 234
3、数据库名的更改
一般不做更改,更改后,可能会影响数据库的使用
[root@localhost ~]# cd /var/lib/mysql/
[root@localhost mysql]# ls
aria_log.00000001 ibdata1 ib_logfile1 mysql.sock westos
aria_log_control ib_logfile0 mysql performance_schema
[root@localhost mysql]# cd westos/
[root@localhost westos]# ls
db.opt linux.frm
[root@localhost westos]# file linux.frm
linux.frm: MySQL table definition file Version 10
[root@localhost westos]# file db.opt
db.opt: ASCII text
[root@localhost westos]# cd ..
[root@localhost mysql]# mv westos/ hello ##将数据库名称westos更改为hello
[root@localhost mysql]# ls
aria_log.00000001 hello ib_logfile0 mysql performance_schema
aria_log_control ibdata1 ib_logfile1 mysql.sock
[root@localhost mysql]# systemctl restart mariadb
- 结果验证
- 刷新数据库信息——FLUSH PRIVILEGES;
[root@localhost mysql]# ls
aria_log.00000001 hello ib_logfile0 mysql performance_schema
aria_log_control ibdata1 ib_logfile1 mysql.sock
[root@localhost mysql]# mv hello/ westos
4、表的修改、添加、删除及数据库的删除
表名称的修改(一般不做修改)
ALTER TABLE linux RENAME messages; ##将表的名称linux改为messages
ALTER TABLE messages RENAME linux; ##将表的名称messages改回为linux
ALTER TABLE linux ADD age varchar(5); ##添加age字段到linux表中(默认加到最后一列)ALTER TABLE linux DROP age; ##删除字段age
ALTER TABLE linux ADD age varchar(5) AFTER username; ##在username字段后添加字段age
ALTER TABLE linux DROP age; ##删除字段age
UPDATE linux SET username='hello' WHERE password='123'; ##更新linux表中password是123的username信息为hello
DELETE FROM linux WHERE username='hello'; ##将linux表中hello的所有内容
DROP TABLE linux; ##将linux表删除
DROP DATABASE westos; ##将数据库westos删除
4、HTTP 和 PHP 对于数据库的管理
HTTP 和 PHP 的前期准备
[root@localhost ~]# yum install httpd php -y
##安装httpd和php,安装过程略
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages.tar.bz2 ##从网上可以下载该压缩包
[root@localhost html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# rm -fr phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# mv phpMyAdmin-3.4.0-all-languages mysqladmin ##更改目录名称
[root@localhost html]# ls
mysqladmin
[root@localhost html]# cd mysqladmin/
[root@localhost mysqladmin]# ls | grep config.sample.
config.sample.inc.php
[root@localhost mysqladmin]# cp config.sample.inc.php config.inc.php
[root@localhost mysqladmin]# ls | grep config
config.inc.php
config.sample.inc.php
show_config_errors.php
此时用浏览器访问http://172.25.254.127/mysqladmin ##访问失败,因为 php 中不存在 mysql
[root@localhost mysqladmin]# php -m | grep mysql
[root@localhost mysqladmin]# yum search php
Loaded plugins: langpacks
=========================================== N/S matched: php ===========================================
php.x86_64 : PHP scripting language for creating dynamic web sites
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-ldap.x86_64 : A module for PHP applications that use LDAP
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
php-odbc.x86_64 : A module for PHP applications that use ODBC databases
php-pdo.x86_64 : A database access abstraction module for PHP applications
php-pear.noarch : PHP Extension and Application Repository framework
php-pgsql.x86_64 : A PostgreSQL database module for PHP
php-process.x86_64 : Modules for PHP script using system process interfaces
php-recode.x86_64 : A module for PHP applications for using the recode library
php-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php-xml.x86_64 : A module for PHP applications which use XML
php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemonName and summary matches only, use "search all" for everything.
[root@localhost mysqladmin]# yum install php-mysql.x86_64 -y
##安装过程略
[root@localhost mysqladmin]# php -m | grep mysql
mysql
mysqli
pdo_mysql
[root@localhost mysqladmin]# systemctl restart httpd
此时用浏览器访问http://172.25.254.127/mysqladmin
MariaDB [(none)]> SHOW DATABASES; ##此时还没有数据库westos
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> SHOW DATABASES; ##在浏览器中创建数据库westoos成功后
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> USE westos;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MariaDB [westos]> SHOW TABLES; ##此时还没有新建数据表
Empty set (0.00 sec)
MariaDB [westos]> SHOW TABLES; ##数据表创建成功后
+------------------+
| Tables_in_westos |
+------------------+
| linux |
+------------------+
1 row in set (0.00 sec)
MariaDB [westos]> SELECT * FROM linux; ##创建字段前
Empty set (0.00 sec)
MariaDB [westos]> SELECT * FROM linux; ##创建字段后
+----------+-----------+-------+
| username | password | class |
+----------+-----------+-------+
| tutu | tutu123 | linux |
| root | redhat123 | linux |
+----------+-----------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> SELECT * FROM linux; ##修改字段后
+----------+-----------+-------+
| username | password | class |
+----------+-----------+-------+
| tutu | tutu456 | linux |
| root | redhat123 | linux |
+----------+-----------+-------+
2 rows in set (0.00 sec)
5、用户的创建、授权、删除等
CREATE USER tutu@'localhost' identified by 'tutu';
##创建用户tutu,'localhost'代表用户tutu只能在本地登陆,'%'代表用户tutu能远程登陆,identified by 'tutu'设置登陆密码为tutu
[root@localhost mysqladmin]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 72
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
GRANT SELECT on westos.* to tutu@localhost;
##添加用户tutu对于数据库westos的查看权限
GRANT UPDATE on westos.* to tutu@localhost;
##添加用户tutu对于数据库westos的写权限(此处不进行效果演示)
##此权限是让用户tutu可以对数据库westos进行编辑
用户权限及用户的删除
6、密码修改
mysqladmin -uroot -predhat password 'tutu' ##修改本地 mysql root 密码 redhat 为 westos
[root@localhost mysqladmin]# mysql -uroot -predhat password 'tutu'
[root@localhost mysqladmin]# mysql -uroot -predhat
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
mysqladmin -uroot -predhat -h ip password westos ##修改远程ip mysql服务器 root密码
###当超级用户密码忘记时###
[root@localhost mysqladmin]# systemctl stop mariadb
[root@localhost mysqladmin]# mysqld_safe --skip-grant-tables & ##跳过grant-tables授权表 不需要认证登录本地mysql数据库
[1] 6141
[root@localhost mysqladmin]# 180526 02:42:44 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
180526 02:42:44 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@localhost mysqladmin]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user;
+-----------+------+-------------------------------------------+
| Host | User | Password |
+-----------+------+-------------------------------------------+
| localhost | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| 127.0.0.1 | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| ::1 | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> update mysql.user set Password=password('123') where User='root';
##更新mysql.user 表中条件为root用户的密码为加密tutu
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user;
+-----------+------+-------------------------------------------+
| Host | User | Password |
+-----------+------+-------------------------------------------+
| localhost | root | *6D80C637B84CD462FED1542FD0425E3ABF536C90 |
| 127.0.0.1 | root | *6D80C637B84CD462FED1542FD0425E3ABF536C90 |
| ::1 | root | *6D80C637B84CD462FED1542FD0425E3ABF536C90 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@localhost mysqladmin]# fg
mysqld_safe --skip-grant-tables
^Z
[1]+ Stopped mysqld_safe --skip-grant-tables
[root@localhost mysqladmin]# killall -9 mysqld_safe
[1]+ Killed mysqld_safe --skip-grant-tables
[root@localhost mysqladmin]# ps aux | grep mysql
mysql 6296 0.0 10.1 859068 98256 pts/1 Sl 02:42 0:00 /usr/libexec/mysqld···(比较长,删除了一些)
root 6371 0.0 0.0 112640 936 pts/1 R+ 02:48 0:00 grep --color=auto mysql
[root@localhost mysqladmin]# kill -9 6296
[root@localhost mysqladmin]# ps aux | grep mysql
root 6373 0.0 0.0 112640 940 pts/1 R+ 02:48 0:00 grep --color=auto mysql
[root@localhost mysqladmin]# systemctl start mariadb
[root@localhost mysqladmin]# mysql -uroot -ptutu
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
[root@localhost mysqladmin]#
7、数据库的备份
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
[root@localhost mysqladmin]# mysql -uroot -ptutu
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 107
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
[root@localhost mysqladmin]# mysqldump -uroot -ptutu --all-database
##内容略,备份框架以及内容
[root@localhost mysqladmin]# mysqldump -uroot -ptutu --all-database --no-data
##内容略,只备份框架
[root@localhost mysqladmin]# mysqldump -uroot -ptutu westos > /mnt/westos.sql ##备份数据库westos到/mnt/下,命名为westos.sql
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "drop database westos;" ##删除数据库westos
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[root@localhost mysqladmin]# ls /mnt/
westos.sql
[root@localhost mysqladmin]# vim /mnt/westos.sql 21 CREATE DATABASE westos; ##添加第21、22行22 USE westos;
[root@localhost mysqladmin]# mysql -uroot -ptutu < /mnt/westos.sql
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "select * from westos.linux;"
+----------+-----------+-------+
| username | password | class |
+----------+-----------+-------+
| tutu | tutu456 | linux |
| root | redhat123 | linux |
+----------+-----------+-------+
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "drop database westos;"
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "select * from westos.linux;"
ERROR 1146 (42S02) at line 1: Table 'westos.linux' doesn't exist
[root@localhost mysqladmin]# vim /mnt/westos.sql 21 /*CREATE DATABASE westos;*/ ##注释掉第21、22行22 /*USE westos;*/
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "CREATE DATABASE westos;"
[root@localhost mysqladmin]# mysql -uroot -ptutu westos < /mnt/westos.sql
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "select * from westos.linux;"
+----------+-----------+-------+
| username | password | class |
+----------+-----------+-------+
| tutu | tutu456 | linux |
| root | redhat123 | linux |
+----------+-----------+-------+
[root@localhost mysqladmin]#
linux——数据库mariadb的基础操作相关推荐
- 【Linux 操作系统】Ubuntu 基础操作 基础命令 热键 man手册使用 关机 重启等命令使用
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21056029 . 1. Linux运行等级介绍 Lin ...
- SQL(一)- 数据库介绍与基础操作
数据库介绍 一.常用的数据库分为两大类: 关系型数据库 非关系型数据库(NoSql) 关系型数据库 概念:是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据. 关系型数 ...
- Linux(centos7)基础操作
Linux(CentOS7) CentOS7 进入单用户修改root密码方法: 1.重启CentOS选择界面 按 e 进入 grub 编辑界面,找到Linux16这行 把 ro (只读readonly ...
- mysql数据库的命令基础操作
#mysql ###mysql工具使用 //语法:mysql [OPTIONS] [database] //常用的OPTIONS: -uUSERNAME //指定用户名,默认为root -hHOST ...
- 非关系型数据库mongoDB以及基础操作
我们都知道,关系型数据库,例如Mysql.Oracle拥有四个特性(ADIS),分别为原子性.隔离性.一致性.连续性.而今天带来的,是非关系型数据库mongoDB,它与关系型数据库不同的是mongoD ...
- Linux服务管理-OpenSSH基础操作
文章目录 1. 使用 SSH 访问远程命令行 1.1 OpenSSH 简介 1.2 SSH 版本 1.3 SSH 认证方式 1.4 openSSH 的工作模式 1.4 Secure Shell 示例 ...
- Linux入门学习——ssh基础操作
接下来的学习中我们就会需要用到两个虚拟机互相的互动,如果想让两个不同的主机能相互连接互动的话,首先就需要调整两个主机的ip地址,那什么是ip呢,ip也叫网络之间互联的协议.也就是为计算机网络相互连接进 ...
- Python笔记_34_MySQL基础操作
文章目录 MySQL基础操作 mysql 客户端 登录 mysql 服务器 mysql 设置密码 创建账户 用户授权(创建用户的同时,也授予权限) SQl语句 配置linux my.cnf MySQL ...
- Linux C网络编程基础
目录 Linux套接字结构定义 Linux C的网络基础操作函数 字节顺序转换函数族 IP地址转换函数族 域名转换函数 Linux网络套接字操作函数 创建套接字描述符函数 绑定套接字函数 建立连接函数 ...
最新文章
- 关于动态规划,你想知道的都在这里了!
- python路1---variable
- python的数字转汉字金额的不完善代码
- 算法--生成m个指定范围的不重复随机数的三种方法分析(Java实现)
- java动态规划算阶乘_动态规划算法
- react demo
- vb 数组属性_VB中菜单编辑器的使用讲解及实际应用
- 2022年中国母婴新消费白皮书
- condition实现通知部分线程
- 整个电脑键盘被锁住了_轻巧便携:罗技Pebble鹅卵石轻薄鼠标+K380蓝牙键盘上手体验...
- arcgis制作超链接
- eTS UI开发学习
- JSP设置Excel表格换行_Excel中快速将阿拉伯数字转化为大写文字的妙招
- linux 硬盘对拷,比ghost好用
- 图解Esp32/ESP8266进行组网开发所需了解的那些小概念
- 网络工程基础- -mac地址以及端口安全
- [C++] [MinGW] MinGW下载与使用
- RocketDock 重启之后又回到默认样式的问题
- 【黑马程序员西安中心】一匹“黑马”的自述
- layuiAdmin安装
热门文章
- 数量和/或“交货结束”指示符或最终发货指示符丢失
- 调用BAPI_MATERIAL_SAVEDATA批量创建/修改物料
- 如何查看长文本的标识和对象(读长文本)。
- SAP销售发票同步产生会计凭证的两种做法
- 察看ABAP程序各项参数限制的情况
- 2020正收益女性占比高达 58.9%,数据揭秘男女谁更会理财?
- 细分领域继续进击,海尔电器寻退市增效?
- php编译成二进制文件_JVM字节码文件概述
- iphone屏幕镜像如何全屏_苹果系统截屏录屏+标记剪辑功能详解( iPhone/iPad/Mac)
- Python零基础教程:函数和类内建魔法属性