MySQL 8 帶來了全新的體驗,好比支持 NoSQL、JSON 等,擁有比 MySQL 5.7 兩倍以上的性能提高。本文講解如何在 Windows 下安裝 MySQL 8,以及基本的 MySQL 用法。html
下載地址 https://dev.mysql.com/downloads/mysql/8.0.html。mysql
本例爲:MySQL Community Server 8.0.12。sql
解壓至安裝目錄,好比 D 盤根目錄下。數據庫
本例爲:D:\mysql-8.0.12-winx64
。windows
my.ini 是 MySQL 安裝的配置文件:socket
[mysqld] # 安裝目錄 basedir=D:\\mysql-8.0.12-winx64 # 數據存放目錄 datadir=D:\\mysqlData\\data
my.ini放置在 MySQL 安裝目錄的根目錄下。須要注意的是,要先建立D:\mysqlData
目錄。data
目錄是由 MySQL 來建立。性能
執行:code
mysqld --defaults-file=D:\mysql-8.0.12-winx64\my.ini --initialize --console
控制檯輸出以下,說明安裝成功:orm
>mysqld --defaults-file=D:\mysql-8.0.12-winx64\my.ini --initialize --console 2018-08-20T16:14:45.287448Z 0 [System] [MY-013169] [Server] D:\mysql-8.0.12-winx64\bin\mysqld.exe (mysqld 8.0.12) initializing of server in progress as process 5012 2018-08-20T16:14:45.289628Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting. 2018-08-20T16:14:45.299329Z 0 [ERROR] [MY-010119] [Server] Aborting 2018-08-20T16:14:45.301316Z 0 [System] [MY-010910] [Server] D:\mysql-8.0.12-winx64\bin\mysqld.exe: Shutdown complete (mysqld 8.0.12) MySQL Community Server - GPL. D:\mysql-8.0.12-winx64\bin>mysqld --defaults-file=D:\mysql-8.0.12-winx64\my.ini --initialize --console 2018-08-20T16:15:25.729771Z 0 [System] [MY-013169] [Server] D:\mysql-8.0.12-winx64\bin\mysqld.exe (mysqld 8.0.12) initializing of server in progress as process 18148 2018-08-20T16:15:43.569562Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: L-hk!rBuk9-. 2018-08-20T16:15:55.811470Z 0 [System] [MY-013170] [Server] D:\mysql-8.0.12-winx64\bin\mysqld.exe (mysqld 8.0.12) initializing of server has completed
其中,「L-hk!rBuk9-.」就是 root 用戶的初始化密碼。稍後能夠作更改。server
執行mysqld就能啓動 MySQL server,或者執行 mysqld --console能夠看到完整的啓動信息:
>mysqld --console 2018-08-20T16:18:23.698153Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. 2018-08-20T16:18:23.698248Z 0 [System] [MY-010116] [Server] D:\mysql-8.0.12-winx64\bin\mysqld.exe (mysqld 8.0.12) starting as process 16304 2018-08-20T16:18:27.624422Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. 2018-08-20T16:18:27.793310Z 0 [System] [MY-010931] [Server] D:\mysql-8.0.12-winx64\bin\mysqld.exe: ready for connections. Version: '8.0.12' socket: '' port: 3306 MySQL Community Server - GPL.
關閉,能夠執行 mysqladmin -u root shutdown
。
使用 mysql 來登陸,帳號爲 root,密碼爲「L-hk!rBuk9-.」:
>mysql -u root -p Enter password: ************ Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 8.0.12 Copyright (c) 2000, 2018, 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.
執行下面的語句來改密碼。其中「123456」即爲新密碼。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.13 sec)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.08 sec)
mysql> CREATE DATABASE lite; Query OK, 1 row affected (0.19 sec)
mysql> USE lite; Database changed
建表執行:
mysql> CREATE TABLE t_user (user_id BIGINT NOT NULL, username VARCHAR(20)); Query OK, 0 rows affected (0.82 sec)
查看數據庫中的全部表:
mysql> SHOW TABLES; +----------------+ | Tables_in_lite | +----------------+ | t_user | +----------------+ 1 row in set (0.00 sec)
查看錶的詳情:
mysql> DESCRIBE t_user; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | user_id | bigint(20) | NO | | NULL | | | username | varchar(20) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
mysql> INSERT INTO t_user(user_id, username) VALUES(1, '老衛'); Query OK, 1 row affected (0.08 sec)