下载
到官网下载压缩包 https://dev.mysql.com/downloads/mysql/
解压到 D盘或其他位置
安装
使用管理员身份运行 cmd
初始化
在mysql目录下添加初始化文件my.ini
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=D:\DataBase\mysql-8.0.15-winx64 # 切记此处一定要用双斜杠\\,单斜杠我这里会出错,不过看别人的教程,有的是单斜杠。自己尝试吧
# 设置mysql数据库的数据的存放目录
datadir=D:\\DataBase\mysql-8.0.15-winx64\\Data # 此处同上
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为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
D:\DataBase\mysql-8.0.15-winx64\bin>mysqld --initialize --console
会出现一些初始化信息,里面给出了root用户的初始化密码“A temporary password is generated for root@localhost: 7RIyfWG03M=r”即“7RIyfWG03M=r”
D:\DataBase\mysql-8.0.15-winx64\bin>mysqld --initialize --console
2019-03-11T06:34:01.942867Z 0 [System] [MY-013169] [Server] D:\DataBase\mysql-8.0.15-winx64\bin\mysqld.exe (mysqld 8.0.15) initializing of server in progress as process 25188
2019-03-11T06:34:01.944087Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2019-03-11T06:34:49.874462Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 7RIyfWG03M=r
2019-03-11T06:35:11.403333Z 0 [System] [MY-013170] [Server] D:\DataBase\mysql-8.0.15-winx64\bin\mysqld.exe (mysqld 8.0.15) initializing of server has completed
这时文件夹下也会多出一个目录
如果不知道密码了,也可以将其删掉,重新初始化一下
安装
mysqld --install
开启服务
net start mysql
MySQL 服务正在启动 ...
MySQL 服务已经启动成功。
修改密码
用户登陆,输入刚才的密码,
注意一个语句的结尾要使用“;”
D:\DataBase\mysql-8.0.15-winx64\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.15
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.
修改密码 alter user ‘root’@‘localhost’ identified with mysql_native_password by ‘xxxx’;
查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)
查看表
mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| role_edges |
| 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 |
+---------------------------+
33 rows in set (0.04 sec)
修改密码 alter user ‘root’@‘localhost’ identified with mysql_native_password by ‘xxxx’;
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'xxxx';
Query OK, 0 rows affected (0.08 sec)
添加用户
管理员root的host是localhost,代表仅限localhost登录访问。如果要允许开放其他ip登录,则需要添加新的host。如果要允许所有ip访问,可以直接修改成“%”
CREATE USER ‘xxh’@’%’ IDENTIFIED WITH mysql_native_password BY ‘xxh123!@#’;
参考:https://www.cnblogs.com/laumians-notes/p/9069498.html
执行sql脚本,可以有2种方法:
第一种方法:
在命令行下(未连接数据库),输入 mysql -h localhost -u root -p123456 < F:\hello world\niuzi.sql (注意路径不用加引号的!!) 回车即可.
第二种方法:
在命令行下(已连接数据库,此时的提示符为 mysql> ),输入 source F:\hello world\niuzi.sql (注意路径不用加引号的) 或者 . F:\hello world\niuzi.sql (注意路径不用加引号的) 回车即可