MySQL系列复习(1)MySQL简介,安装,基础使用
1、简介
1.1、概述
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。
MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
Mysql是开源的,所以你不需要支付额外的费用。 Mysql是可以定制的,采用了GPL协议,你可以修改源码来开发自己的Mysql系统。
Mysql支持大型的数据库。可以处理拥有上千万条记录的大型数据库。 MySQL使用标准的SQL数据语言形式。
Mysql可以允许于多个系统上,并且支持多种语言。这些编程语言包括C、C++、Python、Java、Perl、PHP、Eiffel、Ruby和Tcl等。
MySQL支持大型数据库,支持5000万条记录的数据仓库,32位系统表文件最大可支持4GB,64位系统支持最大的表文件为8TB。
1.2、背景介绍
我们每天都在访问各种⽹站、APP,如微信、QQ、抖⾳、今⽇头条、腾讯新闻等,这些东西上⾯都存在⼤量的信息,这些信息都需要有地⽅存储,存储在哪呢?数据库。
所以如果我们需要开发⼀个⽹站、app,数据库我们必须掌握的技术,常⽤的数据库有mysql、oracle、sqlserver、db2等。
上⾯介绍的⼏个数据库,oracle性能排名第⼀,服务也是相当到位的,但是收费也是⾮常⾼的,⾦融公司对数据库稳定性要求⽐较⾼,⼀般会选择oracle。
mysql是免费的,其他⼏个⽬前暂时收费的,mysql在互联⽹公司使⽤率也是排名第⼀,资料也⾮常完善,社区也⾮常活跃,所以我们主要学习mysql
2、数据库基础知识介绍
2.1、数据库常⻅的概念
- DB:数据库,存储数据的容器。
- DBMS:数据库管理系统,又称为数据库软件或数据库产品,⽤于创建或管理DB。
- SQL:结构化查询语⾔,⽤于和数据库通信的语⾔,不是某个数据库软件持有的,⽽是⼏乎所有的主流数据库软件通⽤的语⾔。中国⼈之间交流需要说汉语,和美国⼈之间交流需要说英语,和数据库沟通需要说SQL语⾔。
2.2、数据库存储数据的⼀些特点
- 数据存放在表中,然后表存放在数据库中
- ⼀个库中可以有多张表,每张表具有唯⼀的名称(表名)来标识⾃⼰
- 表中有⼀个或多个列,列又称为“字段”,相当于java中的“属性”
- 表中每⼀⾏数据,相当于java中的“对象”
3、window中安装mysql
官⽹下载mysql5.7.30: https://dev.mysql.com/downloads/mysql/:5.7.html#downloads
3.1、解压zip安装包,创建my.ini配置文件
my.ini文件中添加如下内容,添加到安装包下修改如下配置路径:
#设置mysql的安装目录
basedir = D:\develop\MySQL\mysql-5.7.30-winx64
#设置mysql数据库的数据的存放目录
datadir = D:\develop\MySQL\mysql-5.7.30-winx64\mysqldata
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir = D:\develop\MySQL\mysql-5.7.30-winx64
# 设置mysql数据库的数据的存放目录
datadir = D:\develop\MySQL\mysql-5.7.30-winx64\mysqldata
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
3.2、配置环境变量
新建环境变量:MYSQL_HOME D:\develop\MySQL\mysql-5.7.30-winx64
添加到Path中:%MYSQL_HOME%\bin
3.3、安装MySQL
一定要以管理员运行,否则在安装过程中会出现因为管理权限不够而导致的Install/Remove of the Service Denied!(安装/卸载服务被拒绝)
1)管理员身份打开CMD:
2)cmd中进入到MySQL的bin目录下
PS D:\develop\MySQL\mysql-5.7.30-winx64\bin>
3)输入安装命令: mysqld -install(若出现Service successfully installed,证明安装成功;如出现Install of the Service Denied,则说明没有以管理员权限来运行cmd)
PS D:\develop\MySQL\mysql-5.7.30-winx64\bin> mysqld --install
Service successfully installed.
4)然后继续输入命令: mysqld --initialize ,此时不会有任何提示:(若报错请删除datadir 下的文件)
PS D:\develop\MySQL\mysql-5.7.30-winx64\bin> mysqld --initialize
5)在输入启动命令: net start mysql ,出现以下提示证明MySQL启动成功:
PS D:\develop\MySQL\mysql-5.7.30-winx64\bin> net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
3.4、设置MySQL密码
当成功进入开启服务以后,用
mysql -u root -p 回车
输入密码
出现 ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO)
方式一:设置MySQL无密码登录
1)关闭服务
PS C:\Users\myron> net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。
2)用安全模式打开
PS C:\Users\> mysqld --skip-grant-tables
这个时候,光标会一直闪。注意,不要动,打开另一个命令行窗口。
3)登录MySQL
PS C:\Windows\system32> mysql -uroot -p
Enter password: 密码为空,直接回车就可以进入到MySQL客户端。
4)修改密码和赋权限:
mysql> use mysql;
// (可能先前的版本密码的抬头是password,5.7.30是 authentication_string,可以select * from user,查看一下)
//(老版本)update user set password=password("123456") where user="root";
mysql> update user set authentication_string=password("root") where user="root";
mysql> flush privileges;
5)退出MySQL安全模式,重新启动MySQL用新密码root登录
PS C:\Windows\system32> net stop mysql
PS C:\Windows\system32> net start mysql
PS C:\Windows\system32> mysql -uroot -p"root"
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER
USER statement before executing this statement.
//需要再次修改密码
mysql> alter user 'root'@'localhost' identified by 'root';
//刷新权限
mysql> flush privileges;
//退出
mysql> quit
方式二:找到 datadir 目录中找到初始密码
第一次进入mysql会由 mysql initialize 初始化一个密码,
在mysqldata/.err这个文件中搜索password会看到生成的密码. [Note] A temporary password
is generated for root@localhost:<yyjDCXl1vy
用初始密码登录
PS C:\Windows\system32> mysql -uroot -p"<yyjD*CXl1vy"
//修改密码
mysql> alter user 'root'@'localhost' identified by 'root';
//刷新权限
mysql> flush privileges;
//退出
mysql> quit
3.5、其他
启动: net start MySQL
停止: net stop MySQL
卸载: sc delete MySQL,mysqld -remove
4、MySQL常⽤的⼀些命令介绍
4.1、mysql启动2种⽅式
4.1.2、⽅式1:cmd中运⾏ services.ms
会打开服务窗口,找到mysql服务,点击右键可以启动或者停⽌。
4.1.2、⽅式2:以管理员⾝份运⾏cmd命令
win+A打开管理员窗口:
停⽌命令:net stop mysql
启动命令:net start mysql
C:\Windows\system32>net stop mysql
mysql 服务正在停⽌.
mysql 服务已成功停⽌。
C:\Windows\system32>net start mysql
mysql 服务正在启动 .
mysql 服务已经启动成功。
注意:命令后⾯没有结束符号。
4.2、MySQL登录命令
== mysql -h ip地址 -P 端口 -u ⽤户名 -p ==
说明:
1)-P ⼤写的P后⾯跟上端口
2) 如果是登录本机ip和端口可以省略,如:mysql -u ⽤户名 -p
可以通过上⾯的命令连接原创机器的mysql
4.3、查看数据库版本
4.3.1、方式一:未登录MySQL查看
mysql --version 或者mysql -V ⽤于在未登录情况下,查看本机mysql版本:
PS C:\Windows\system32> mysql --version
D:\develop\MySQL\mysql-5.7.30-winx64\bin\mysql.exe Ver 14.14 Distrib 5.7.30, for Win64 (x86_64)
PS C:\Windows\system32> mysql -V
D:\develop\MySQL\mysql-5.7.30-winx64\bin\mysql.exe Ver 14.14 Distrib 5.7.30, for Win64 (x86_64)
4.3.2、方式二:登录MySQL查看
select version(); :登录情况下,查看链接的库版本:
PS C:\Windows\system32> mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> select version();
+-----------+
| version() |
+-----------+
| 5.7.30 |
+-----------+
1 row in set (0.00 sec)
mysql>
4.4、显示所有数据库:show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| apolloconfigdbpro |
| apolloportaldb |
| mysql |
| performance_schema |
| sys |
| test01 |
| test02 |
+--------------------+
8 rows in set (0.00 sec)
4.5、进⼊指定的库:use 库名;
mysql> use apolloconfigdbpro;
Database changed
4.6、显示当前库中所有的表:show tables;
mysql> show tables;
+-----------------------------+
| Tables_in_apolloconfigdbpro |
+-----------------------------+
| app |
| appnamespace |
| audit |
| cluster |
| commit |
| grayreleaserule |
| instance |
| instanceconfig |
| item |
| namespace |
| namespacelock |
| release |
| releasehistory |
| releasemessage |
| serverconfig |
+-----------------------------+
15 rows in set (0.00 sec)
4.7、查看其他库中所有的表:show tables from 库名;
mysql> show tables from mysql;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
4.8、查看表的创建语句:show create table 表名;
mysql> show create table app;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| app | CREATE TABLE `app` (
`Id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`AppId` varchar(500) NOT NULL DEFAULT 'default' COMMENT 'AppID',
`Name` varchar(500) NOT NULL DEFAULT 'default' COMMENT '应用名',
`OrgId` varchar(32) NOT NULL DEFAULT 'default' COMMENT '部门Id',
`OrgName` varchar(64) NOT NULL DEFAULT 'default' COMMENT '部门名字',
`OwnerName` varchar(500) NOT NULL DEFAULT 'default' COMMENT 'ownerName',
`OwnerEmail` varchar(500) NOT NULL DEFAULT 'default' COMMENT 'ownerEmail',
`IsDeleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '1: deleted, 0: normal',
`DataChange_CreatedBy` varchar(32) NOT NULL DEFAULT 'default' COMMENT '创建人邮箱前缀',
`DataChange_CreatedTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`DataChange_LastModifiedBy` varchar(32) DEFAULT '' COMMENT '最后修改人邮箱前缀',
`DataChange_LastTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
PRIMARY KEY (`Id`),
KEY `AppId` (`AppId`(191)),
KEY `DataChange_LastTime` (`DataChange_LastTime`),
KEY `IX_Name` (`Name`(191))
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COMMENT='应用表' |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
4.9、查看表结构:desc 表名;
mysql> desc app;
+---------------------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+------------------+------+-----+-------------------+-----------------------------+
| Id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| AppId | varchar(500) | NO | MUL | default | |
| Name | varchar(500) | NO | MUL | default | |
| OrgId | varchar(32) | NO | | default | |
| OrgName | varchar(64) | NO | | default | |
| OwnerName | varchar(500) | NO | | default | |
| OwnerEmail | varchar(500) | NO | | default | |
| IsDeleted | bit(1) | NO | | b'0' | |
| DataChange_CreatedBy | varchar(32) | NO | | default | |
| DataChange_CreatedTime | timestamp | NO | | CURRENT_TIMESTAMP | |
| DataChange_LastModifiedBy | varchar(32) | YES | | | |
| DataChange_LastTime | timestamp | YES | MUL | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+---------------------------+------------------+------+-----+-------------------+-----------------------------+
12 rows in set (0.02 sec)
mysql>
4.10、查看当前所在库:select database();
mysql> select database();
+-------------------+
| database() |
+-------------------+
| apolloconfigdbpro |
+-------------------+
1 row in set (0.00 sec)
4.11、查看当前mysql⽀持的存储引擎:SHOW ENGINES;
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
mysql>
4.12、查看系统变量及其值:SHOW VARIABLES;
mysql> show variables;
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| auto_generate_certs | ON |
| auto_increment_increment | 1
............
............
............
| version | 5.7.30 |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | Win64 |
| wait_timeout | 28800 |
| warning_count | 0 |
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
517 rows in set, 1 warning (0.05 sec)
mysql>
4.13、查看某个系统变量:SHOW VARIABLES like ‘变量名’;
mysql> show variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)
mysql> show variables like '%wait_timeou%t';
+--------------------------+----------+
| Variable_name | Value |
+--------------------------+----------+
| innodb_lock_wait_timeout | 50 |
| lock_wait_timeout | 31536000 |
| wait_timeout | 28800 |
+--------------------------+----------+
3 rows in set, 1 warning (0.00 sec)
mysql>
5、MySQL语法规范
- 不区分⼤⼩写,但建议关键字⼤写,表名、列名⼩写
- 每条命令最好⽤英⽂分号结尾
- 每条命令根据需要,可以进⾏缩进或换⾏
- 注释
– 单⾏注释:#注释⽂字
– 单⾏注释:-- 注释⽂字 ,注意, 这⾥需要加空格
– 多⾏注释:/* 注释⽂字 */
6、SQL的语⾔分类
- DQL(Data Query Language):数据查询语⾔ select 相关语句
- DML(Data Manipulate Language):数据操作语⾔ insert 、update、delete 语句
- DDL(Data Define Languge):数据定义语⾔ create、drop、alter 语句
- CL(Transaction Control Language):事务控制语⾔ set autocommit=0、start transaction、savepoint、commit、rollback
本文地址:https://blog.csdn.net/MyronCham/article/details/108991517
上一篇: 为什么我推荐Nginx作为后端服务器代理(原因解析)
下一篇: Mysql 主从基础概念
推荐阅读