Cài đặt MySQL 5.7 trở lên trên hệ thống Windows 10

0. Giới thiệu

Trong bài viết trước, tôi đã mô tả các bước cài đặt MySQL 5.6. Trong bài viết này, tôi sẽ hướng dẫn cài đặt MySQL 5.7 và các phiên bản sau, chủ yếu vì từ phiên bản 5.7 trở đi, các lệnh cài đặt có một vài thay đổi.

1. Tải MySQL

Địa chỉ tải: https://downloads.mysql.com/archives/community/

Tại đây chúng ta tải gói zip

2. Giải nén gói MySQL vào thư mục chỉ định

3. Thêm tệp my.ini

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/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.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# Thiết lập thư mục cài đặt mysql
basedir = D:/my_application/mysql-5.7.9-winx64
# Thiết lập thư mục lưu trữ dữ liệu mysql
datadir = D:/my_application/mysql-5.7.9-winx64/data
# Thiết lập cổng 3306
port = 3306
# server_id = .....

# 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 

# Cho phép số lượng kết nối tối đa
max_connections = 200
# Cho phép số lần kết nối thất bại. Điều này để ngăn chặn ai đó từ máy chủ này cố gắng tấn công hệ thống cơ sở dữ liệu
max_connect_errors = 10
# Bộ ký tự mặc định mà máy chủ sử dụng là UTF8
character-set-server = utf8mb4
# Bộ lưu trữ mặc định sẽ được sử dụng khi tạo bảng mới
default-storage-engine = INNODB
# Sử dụng plugin "mysql_native_password" để xác thực theo mặc định
default_authentication_plugin = mysql_native_password
[mysql]
# Thiết lập bộ ký tự mặc định cho client mysql
default-character-set = utf8mb4
[client]
# Thiết lập cổng mặc định mà client mysql sử dụng khi kết nối với server
port=3306
default-character-set = utf8mb4

Giải thích:
[client] đại diện cho nội dung cài đặt mặc định của client;
[mysql] đại diện cho cài đặt mặc định khi chúng ta sử dụng lệnh mysql để đăng nhập vào cơ sở dữ liệu;
[mysqld] đại diện cho cài đặt mặc định của cơ sở dữ liệu.

4. Thiết lập biến môi trường cho MySQL

Chuột phải vào This PC → Properties → Advanced system settings → Environment Variables
1. Thêm biến môi trường mysql
2. Thêm biến môi trường mysql vào Path

5. Khởi tạo thư mục dữ liệu

Mở Command Prompt (chạy với quyền Administrator), và chạy lệnh khởi tạo:

mysqld --initialize --console --basedir=D:/my_application/mysql-5.7.9-winx64 --datadir=D:/my_application/mysql-5.7.9-winx64/data

6. Cài đặt dịch vụ MySQL

mysqld --install MySQL57 --defaults-file=D:/my_application/mysql-5.7.9-winx64/my.ini

Giải thích: MySQL57 là tên dịch vụ

7. Khởi động dịch vụ MySQL

net start MySQL57

8. Kết nối với MySQL

mysql -u root -p

Giải thích: Sử dụng mật khẩu được tạo tự động

9. Thiết lập mật khẩu cho người dùng root và kiểm tra

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
SELECT User, Host, authentication_string FROM mysql.user WHERE User = 'root';

10. Kết nối với MySQL bằng client

Kết nối thành công như hình dưới:

11. Tham khảo

https://blog.csdn.net/addtyp/article/details/131831782

Thẻ: mysql Windows 10 Database installation Configuration

Đăng vào ngày 3 tháng 6 lúc 17:39