安装依赖

部署 MySQL

  • 下载包:https://downloads.mysql.com/archives/community/
  • 解压至指定目录(D:\mysql5.7)
  • 创建 data 目录和 log 目录
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
D:\MYSQL5.7
├─Data
│  ├─data_3306
│  └─tmp_3306
├─Log
│  └─log_3306
│      ├─bin_log
│      ├─innodb_log
│      └─relay_log
└─mysql-5.7.33-winx64
  • 创建 mysql.ini(D:\mysql5.7\Data\mysql.ini)
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
[client]
port	= 3306
socket = D:/mysql5.7/Data/data_3306/mysql.sock

[mysql]
prompt="\u@\h \R:\m:\s [\d]> "
no-auto-rehash

[mysqld]
symbolic-links=0
user	= mysql
port	= 3306
server_id = 51301
basedir	= D:/mysql5.7/mysql-5.7.33-winx64
datadir	= D:/mysql5.7/Data/data_3306
socket = D:/mysql5.7/Data/data_3306/mysql.sock
pid-file = mysql.pid
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
skip_name_resolve = 1
open_files_limit    = 65535
back_log = 1024
max_connections = 1500
max_connect_errors = 900000000
table_open_cache = 2002
table_definition_cache = 2002
table_open_cache_instances = 64
lower_case_table_names = 1
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 2000
query_cache_size = 0
query_cache_type = 0
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
tmpdir = D:/mysql5.7/Data/tmp_3306 
max_heap_table_size = 32M
slow_query_log = 1
innodb_log_group_home_dir = D:/mysql5.7/Log/log_3306/innodb_log
log-bin = D:/mysql5.7/Log/log_3306/bin_log/mysql-bin
log_error= D:/mysql5.7/Log/log_3306/mysql.err
relay-log = D:/mysql5.7/Log/log_3306/relay_log/relay-bin
#read-only=1
slow-query-log
slow_query_log_file = D:/mysql5.7/mysql_log/log_3306/slow_queries.log
long_query_time = 1
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 1G
max_binlog_size = 64M
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
default-time-zone = '+8:00'
log_timestamps = SYSTEM
#innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2G
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:12M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 1G
innodb_log_files_in_group = 2
innodb_max_undo_log_size = 1G
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
#innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 1G
internal_tmp_disk_storage_engine = InnoDB
innodb_status_file = 0
innodb_status_output = 0
innodb_status_output_locks = 0
innodb_stats_on_metadata = 0

#performance_schema
performance_schema = 1
performance_schema_instrument = '%=on'
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

[mysqldump]
quick
max_allowed_packet = 32M
  • 初始化数据
1
D:\mysql5.7\mysql-5.7.33-winx64\bin>mysqld.exe --initialize-insecure --datadir=D:\mysql5.7\Data\data_3306
  • 将 MySQL 注册为 服务
1
D:\mysql5.7\mysql-5.7.33-winx64\bin>mysqld --install MySQL5.7 --defaults-file="D:\mysql5.7\Data\mysql.ini"

配置身份信息

  • MySQL 初始化后,root 默认只允许通过 localhost 方式连接数据库,但由于 Windows 总会自动解析 localhost 至网络地址,无法通过 MySQL 身份认证,只能通过 更改 MySQL User 这个系统表的方式配置身份信息

  • 以免身份验证方式启动 MySQL(如果 MySQL 正在运行,需关闭)

1
C:\WINDOWS\system32>D:\mysql5.7\mysql-5.7.33-winx64\bin\mysqld --defaults-file=D:\mysql5.7\Data\mysql.ini --skip-grant-tables
  • 更改初始化的 root 用户的连接地址为 127.0.0.1
1
D:\mysql5.7\mysql-5.7.33-winx64\bin>mysql.exe
1
2
3
4
mysql> use mysql;
Database changed
mysql> update `User` set Host='127.0.0.1' where User='root';
mysql> flush privileges;
  • 关闭免身份认证启动的 MySQL,在 windows service 中正常启动 MySQL
  • 通过 127.0.0.1 和 root 用户连接 MySQL,配置其他管理用户
1
D:\mysql5.7\mysql-5.7.33-winx64\bin>mysql.exe -uroot  -h127.0.0.1
1
2
3
4
5
6
7
8
-- 更改 root@'127.0.0.1'密码,空密码安全性存在问题
mysql> set password for 'root'@'127.0.0.1'=PASSWORD('rootPassword');
-- 创建用户,并配置表权限
mysql> grant all privileges on *.* to 'xxx'@'127.0.0.1' identified by 'xxxPassword';
-- 通过更改系统表,配置用户允许 GRANT 
mysql> use mysql;
mysql> update user set Grant_priv="Y" where User='xxx';
mysql> flush privileges;
  • 配置成功后,可通过刚刚创建的用户(xxx@‘127.0.0.1’),管理 MySQL