目录
MySQL 8.0安装与操作指南
/  

MySQL 8.0安装与操作指南

MySQL 8.0安装与操作指南

安装

下载: https://dev.mysql.com/downloads/mysql/

McAmTO.png

解压之后, 在这,目录下创建my.ini文件, 把安装路径填一下, 还有创建data目录

[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=G:\win_mysql\mysql-8.0.17-winx64
# 设置mysql数据库的数据的存放目录
datadir=G:\win_mysql\mysql-8.0.17-winx64\Data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=5
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8

目录结构

McAUAS.png

添加环境变量

G:\win_mysql\mysql-8.0.17-winx64\bin

McECHf.png

McEKbV.png

点击确定, 完成。

安装指令

打开cmd命令行, win + r 键 输入cmd 确认 --(以管理员运行)

输入命令, 初始化

mysqld --initialize --console
# 也可以使用 mysqld --initialize-insecure --user=mysql 使用这个命令, 默认没有root密码, 可以自己设置, 比较方便.

耐心等一会, 会输出mysql的默认密码

McVEdK.png

如果是你执行这个命令之后, 不小心关闭了窗口, 可以删除datadir目录, 再执行一遍初始化命令, 又会重新生成默认密码

现在还没安装完, 不要急着去登mysql, 登你也登不上。 mysql服务没开。

安装服务

mysqld --install [服务名]

后面的服务名可以不写,默认的名字为 mysql。当然,如果你的电脑上需要安装多个MySQL服务,就可以用不同的名字区分了,比如 mysql5 和 mysql8。

例如执行

mysqld --install

会出现

C:\Users\Administrator>mysqld --install
Service successfully installed.
C:\Users\Administrator>

显示已经安装成功

现在打开服务

C:\Users\Administrator>net start mysql
MySQL 服务正在启动 ....
MySQL 服务已经启动成功。

更改默认的root密码

使用默认密码登陆mysql

C:\Users\Administrator>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17

Copyright (c) 2000, 2019, 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>

更改密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

执行完之后,安装成功

卸载MySQL

首先关闭MySQL服务

net stop mysql

卸载服务

mysqld --remove

最后删除环境变量, 就ok了。

MySQL的基本操作

创建数据库

mysql> create database test;
Query OK, 1 row affected (0.19 sec)
  • 使用该数据库

    mysql> use test;
    Database changed
    

创建表

mysql> create table user(
    -> id int(11) primary key,
    -> name varchar(10),
    -> age int(3)
    -> );
Query OK, 0 rows affected, 2 warnings (0.35 sec)
  • 单独的命令
create table user(
     id int(11) primary key,
     name varchar(10),
     age int(3)
);

插入数据

mysql> insert into user (id, name, age) values(1, '张三', 22);
Query OK, 1 row affected (0.77 sec)
  • 单独的命令

    insert into user (id, name, age) values(1, '张三', 22);
    

修改数据

mysql> update user set name = '李四' where id = 1;
Query OK, 1 row affected (0.14 sec)
Rows matched: 1  Changed: 1  Warnings: 0
  • 单独的命令

    update user set name = '李四' where id = 1;
    

查询数据

mysql> select * from user;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 李四   |   22 |
+----+--------+------+
1 row in set (0.00 sec)
  • 单独的命令

    select * from user;
    

删除数据

mysql> delete from user where id = 1;
Query OK, 1 row affected (0.13 sec)
  • 单独的命令

    delete from user where id = 1;
    

删除数据库

drop database test;

标题:MySQL 8.0安装与操作指南
作者:gitsilence
地址:https://blog.lacknb.cn/articles/2019/11/21/1577974156347.html