mysql8.0基础操作
一、安装和配置数据库:
下载mysql地址:https://dev.mysql.com/downloads/mysql/
windows下载的版本是installer msi版本8.0:https://dev.mysql.com/downloads/windows/installer/8.0.html
环境变量的配置:
路径:C:\Program Files\MySQL\MySQL Server 8.0\bin
环境变量配置地址:电脑——》属性——》系统属性——》高级——》环境变量——》(系统变量)Path \ 或
win10地址:此电脑-》属性-》高级系统设置-》高级-》环境变量-》(系统变量)Path
启动命令行:windows键+R键,输入CMD
管理员身份启动才能执行如下操作:
启动数据库:net start mysql80
关闭数据库:net stop mysql80
二、连接数据库的方式:
1、连接本地数据库:
# 语法
MySQL -h <服务器主机名或主机地址> \ # 访问本机数据库可不写
-P <端口号> \ # 默认3306端口可不写
-u <用户名> \ # 必须写
-p <密码> # 必须写
# 几种登录方式
mysql -h localhost -u root -p 回车后输入密码
mysql -h 127.0.0.1 -u root -p1234
mysql -uroot -p1234
mysql -h 39.100.75.16 -uroot -p 回车后输入密码
# 注意事项
# 对于本机操作,可以省略-h <服务器主机名或主机地址>
# -u与<用户名>之间可以有空格,也可以没有空格
# -p后面可以不接密码,按【Enter】键后系统会提示输入密码
# 进去mysql后,可以输入quit或者exit来退出登录状态。
# 如果是连接远程的数据库的话,将localhost 换为数据库服务器的ip地址。
2、修改MySQL数据库服务器的登录密码
# 登录mysql后
mysql> set password for root@localhost='123456';
mysql> quit
Bye
# 重新登录数据库时,必须使用密码123456才能登录
3、navicat连接数据库:
双击桌面快捷方式【Navicat 15 for MySQL】,启动图形管理工具Navicat for MySQL;
在【Navicat for MySQL】窗口的工具栏的【连接】下拉列表中选择“MySQL”;
对话框中设置连接参数,在“连接名”文本框中输入“MyConn”,然后分别输入主机名或IP地址、端口号、用户名和登录密码;
输入完成后单击【测试连接】按钮,弹出“连接成功”的提示信息对话框,表示连接创建成功,单击【确定】按钮保存所创建的连接。
在【Navicat for MySQL】窗口中的【文件】菜单中选择【打开连接】命令。
三、关于数据库的操作
每一条SQL语句都以半角分号“;”或“\g”或“\G”作为结束标志.
# 查看当前所有的数据库:show databases;
mysql、information_schema、perfermance_schema、sys这四个是系统自带的数据库,不用动它。
# 创建数据库:
Create { Database | Schema } [ If Not Exists ] <数据库名称> [ [ Default ] Character Set <字符集名称> | [ Default ] Collate <排序规则名称> ]
# 创建示例
mysql> CREATE DATABASE `MallDB` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
mysql> create database schooldb charset ascii;
# If Not Exists为可选项,创建数据库的语句中包含了“If Not Exists”,表示如果待创建的数据库不存在则创建,存在则不创建,作用是避免存在同名的数据库时,出现错误提示信息的情况。
mysql> create database if not exists studentdb;
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> create database studentdb;
ERROR 1007 (HY000): Cant create database 'studentdb'; database exists
# 打开数据库:use 数据库名;
mysql> use studentdb;
Database changed
# 查看单个数据库信息: show create database 数据库名;
mysql> show create database studentdb;
+-----------+------------------------------------------+
Database Create Database
+-----------+-------------------------------------------+
studentdb | CREATE DATABASE `studentdb` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
1 row in set (0.00 sec)
# 查看当前所用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| studentdb |
+------------+
1 row in set (0.00 sec)
# 数据库“StudentDB”使用的端口
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)
# 数据库文件的存放路径
mysql> show variables like '%datadir%';
+---------------+---------------------------------------------+
| Variable_name | Value |
+---------------+---------------------------------------------+
| datadir | C:\ProgramData\MySQL\MySQL Server 8.0\Data\ |
+---------------+---------------------------------------------+
1 row in set, 1 warning (0.00 sec)
# 查看“StudentDB”默认字符集:
mysql> show variables like 'character%';
+--------------------------+-------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\ |
+--------------------------+--------------------------------------------+
# 数据库“StudentDB”默认的排序规则
mysql> show variables like 'collation%';
+----------------------+--------------------+
| Variable_name | Value |
+----------------------+--------------------+
| collation_connection | gbk_chinese_ci |
| collation_database | utf8mb4_0900_ai_ci |
| collation_server | utf8mb4_0900_ai_ci |
+----------------------+--------------------+
3 rows in set, 1 warning (0.00 sec)
# 修改数据库语法
Alter { Database | Schema } <数据库名称> [ [ Default ] Character Set <字符集名称> | [ Default ] Collate <排序规则名称> ]
# 修改数据库“StudentDB”的默认字符集和排序规则
mysql> alter database studentdb character set gb2312 collate gb2312_chinese_ci;
Query OK, 1 row affected (0.01 sec)
# 查看数据库“StudentDB”修改后的字符集和排序规则
mysql> show variables like 'character%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | gb2312 # 发生变化 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)
mysql> show variables like 'collation%';
+----------------------+--------------------+
| Variable_name | Value |
+----------------------+--------------------+
| collation_connection | gbk_chinese_ci |
| collation_database | gb2312_chinese_ci | #发生变化
| collation_server | utf8mb4_0900_ai_ci |
+----------------------+--------------------+
3 rows in set, 1 warning (0.00 sec)
# 查看数据库的状态:
mysql> status;
--------------
mysql Ver 8.0.28 for Win64 on x86_64 (MySQL Community Server - GPL)
Connection id: 21
Current database:
Current user: root@localhost
SSL: Cipher in use is TLS_AES_256_GCM_SHA384
Using delimiter: ;
Server version: 8.0.28 MySQL Community Server - GPL
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Binary data as: Hexadecimal
Uptime: 53 min 41 sec
--------------
# 查看MySQL的版本信息和连接用户名
mysql> select version(),user();
+-----------+----------------+
| version() | user() |
+-----------+----------------+
| 8.0.28 | root@localhost |
+-----------+----------------+
# 删除数据库:drop database 数据库名;
mysql> drop database studentdb;
Query OK, 0 rows affected (0.00 sec)
# 查看系统支持的存储引擎类型:
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
# “Engine”表示存储引擎名称;
# “Support”表示MySQL是否支持该类存储引擎;
# “Comment”表示对该存储引擎的简要说明;
# “Transactions”表示是否支持事务处理;
# “XA”表示是否支持分布式交易处理;
# “Savepoints”表示是否支持保存点,以便事务回滚到保存点。
四、安装和配置centos7-mysql8.0
安装数据库
1)创建虚拟机
安装vmware软件。安装secureCRT或者xshell软件。
下载centos7.6地址:https://mirrors.aliyun.com/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso
创建虚拟机。网络为nat模式。
2)配置网络
修改网卡:/etc/sysconfig/network-scripts/ifcfg-ens33
修改和添加如下配置:
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.88.101
NETMASK=255.255.255.0
GATEWAY=192.168.88.2
DNS=8.8.8.8
修改dns:/etc/resolv.conf
添加如下信息:
nameserver 8.8.8.8
重启网络: systemctl restart network
测试网络:ping www.baidu.com
3)关闭防火墙和selinux
systemctl disable firewalld
systemctl stop firewalld
修改selinux配置文件:/etc/selinux/config
改为:SELINUX=disabled
让修改生效:setenforce 0
4)更新yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 清理yum
yum clean all
yum makecache
5)安装MySQL
yum install wget
wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
yum install mysql-community-server
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server
systemctl enable mysqld
systemctl start mysqld
配置数据库
# 查看默认密码连接数据库
[root@localhost ~]# grep 'password' /var/log/mysqld.log
2022-03-30T10:23:15.029618Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: yuD>0*FmwwtB
[root@localhost ~]# mysql -uroot -p
# 修改默认密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'TestBicon@123';
Query OK, 0 rows affected (0.00 sec)
# 修改复杂密码规则
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)
# 开启root远程访问
mysql> use mysql
mysql> update user set Host='%' where User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 重置root的密码
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456789';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 后面就可以用navicat软件登陆了