MySQL-5.7 安装 配置

MySQL安装配置

(1) Mysql安装

整体安装步骤
1.检查服务器是否有mysql,如果有,是否要卸载
2.添加mysql组和mysql用户,用于设置mysql安装目录文件所有者和所属组
3.mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz解压,复制到/usr/local 目录下(其他目录也可以) 设置软链接
4.进入mysql文件夹,也就是mysql所在的目录,并更改所属的组和用户
5.安装、初始化、配置 数据库

(1.1) 检查服务器是否有mysql,如果有,是否要卸载

1
2
3
4
5
6
7
#查看是否安装mysql,grep的-i选项表示匹配时忽略大小写
rpm -qa|grep -i mysql
#查看是否有mysql路径下的程序在运行
ps -ef | grep mysqld
ps -el | grep mysqld
#卸载mysql
rpm -e pcp-pmda-mysql --nodeps

(1.2) 检查mysql组和用户是否存在,如无创建。

1
2
3
4
5
6
7
8
#检查mysql组和用户是否存在,如无创建。
cat /etc/group | grep mysql

#添加mysql组和mysql用户,用于设置mysql安装目录文件所有者和所属组
groupadd mysql

#useradd -r 参数表示mysql用户是系统用户,不可用于登录系统。
useradd -r -g mysql mysql

(1.3) 下载mysql安装包,解压到/usr/local目录下

1
2
3
4
5
6
7
http://dev.mysql.com/downloads/mysql/#downloads  mysql官网下载地址
https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-x86_64.tar
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz


tar -zxvf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz #解压
ln -s /usr/local/mysql-5.7.16-linux-glibc2.5-x86_64/ mysql #创建软链接,方便操作

(1.4) 进入mysql文件夹,也就是mysql所在的目录,并更改所属的组和用户

1
2
3
4
5
cd mysql
# 更改文件所属组和所属用户
(后面必须要有. 否则会有chown: missing operand after ‘mysql’ Try 'chown --help' for more information.的错误)
chown -R mysql . #更改当前目录所属的用户(也替换为 chown -R mysql /usr/local/mysql )
chgrp -R mysql . #更改当前目录所属的组(也替换为 chgrp -R mysql /usr/local/mysql )

(1.5) 安装、初始化、配置 数据库

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
# 执行安装脚本,--user 指的是mysql用户,--basedir是数据库的安装目录,可以自定义,--datadir是数据存放目录,可以自定义
# 这一步会生成随机密码,一定要保存
[root@host_wkq bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2017-08-16T02:48:19.810647Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-08-16T02:48:27.480885Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-08-16T02:48:29.962049Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-08-16T02:48:30.134064Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 62d4c750-822d-11e7-98fc-fa163e50f135.
2017-08-16T02:48:30.223416Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-16T02:48:30.224491Z 1 [Note] A temporary password is generated for root@localhost: wgU;&c,hn6V,


执行 cp -a ./support-files/my-default.cnf /etc/my.cnf 命令
[root@iZ2ze5ynf8pjgvvvq0hzbfZ mysql]# cp -a ./support-files/my-default.cnf /etc/my.cnf #复制配置文件
cp: overwrite ‘/etc/my.cnf’? yes


执行 cp -a ./support-files/mysql.server /etc/init.d/mysqld 命令
[root@iZ2ze5ynf8pjgvvvq0hzbfZ mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysqld #复制配置文件

进入bin目录,执行 ./mysqld_safe --user=mysql & 命令
[root@iZ2ze5ynf8pjgvvvq0hzbfZ bin]# ./mysqld_safe --user=mysql & ##启动守护进程
[1] 27812
[root@iZ2ze5ynf8pjgvvvq0hzbfZ bin]# 2016-12-10T15:12:54.430174Z mysqld_safe Logging to '/usr/local/mysql/data/iZ2ze5ynf8pjgvvvq0hzbfZ.err'.
2016-12-10T15:12:54.503677Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

执行 /etc/init.d/mysqld restart 命令
[root@iZ2ze5ynf8pjgvvvq0hzbfZ bin]# /etc/init.d/mysqld restart # 启动mysql
Shutting down MySQL..2016-12-10T15:14:50.965214Z mysqld_safe mysqld from pid file /usr/local/mysql/data/iZ2ze5ynf8pjgvvvq0hzbfZ.pid ended
[ OK ]
Starting MySQL. [ OK ]
[1]+ Done ./mysqld_safe --user=mysql
[root@iZ2ze5ynf8pjgvvvq0hzbfZ bin]#

mysql安装完了,可以使用了。
如果还想配置的,执行以下步骤。

用随机生成的密码登录mysql

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
[root@localhost bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16

Copyright (c) 2000, 2016, 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> show databases;
#第一次登录会提示你修改密码,修改密码就可以了,修改完重新登录。
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> SET PASSWORD = PASSWORD('abc2017qwer');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> alter user 'root'@'localhost' password expire never;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

添加远程访问权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mysql> use mysql; 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host = '%' where user = 'root'; #修改root用户的远程访问权限
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select host, user from user; #查看设置是否成功
+-----------+-----------+
| host | user |
+-----------+-----------+
| % | root |
| localhost | mysql.sys |
+-----------+-----------+
2 rows in set (0.00 sec)

mysql> exit
Bye
[root@iZ2ze5ynf8pjgvvvq0hzbfZ bin]# /etc/init.d/mysqld restart
Shutting down MySQL.. [ OK ]
Starting MySQL. [ OK ]
[root@iZ2ze5ynf8pjgvvvq0hzbfZ bin]#

mysql_install_db命令安装生成密码保存在/root/.mysql_secret,需要自己查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要登录。

执行 bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 命令

[root@localhost mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2016-06-01 15:23:25 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2016-06-01 15:23:30 [WARNING] The bootstrap log isn't empty:
2016-06-01 15:23:30 [WARNING] 2016-06-01T22:23:25.491840Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2016-06-01T22:23:25.492256Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2016-06-01T22:23:25.492260Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)

执行 cat /root/.mysql_secret 命令
[root@iZ2ze5ynf8pjgvvvq0hzbfZ bin]# cat /root/.mysql_secret
#Password set for user 'root@localhost' at 2016-12-10 23:06:16
Y*rBklxr_q&) 这个是密码,有点难写

忘记MySQL密码

Linux下如果忘记MySQL的root密码,可以通过修改配置的方法,重置root密码
修改MySQL的配置文件(默认为/etc/my.cnf),在[mysqld]下添加一行skip-grant-tables
保存配置文件后,重启MySQL服务 service mysqld restart
再次进入MySQL命令行 mysql -uroot -p,输入密码时直接回车,就会进入MySQL数据库了,这个时候按照常规流程修改root密码即可。

1
2
3
4
5
6
7
8
9
10
update user set password=password('newpassword') where user='root';  #MySQL-5.6修改密码命令
update user set authentication_string=password('root') where user='root' ; #MySQL-5.7修改密码命令
flush privileges;


mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

设置mysql开机启动

1
2
#执行 chkconfig --level 35 mysqld on 命令
[root@iZ2ze5ynf8pjgvvvq0hzbfZ bin]# chkconfig --level 35 mysqld on

MySQL启动、停止、重启命令

1
2
3
4
/etc/init.d/mysqld start  #使用mysqld启动MySQL
/etc/init.d/mysqld stop #使用mysqld停止MySQL

/etc/init.d/mysqld restart #重启MySQL

(2) MySQL-5.7 my.cnf配置

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

#[client]
#port=3306
#default-character-set = utf8mb4

#[mysql]
#default-character-set = utf8mb4

[mysqld]
#character-set-server=utf8mb4
#innodb_file_per_table=1
#lower_case_table_names=1
#max_allowed_packet=1024M

#
# 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=mysql-bin
server-id=1

#if query_time > 1s sql will log
#long_query_time=1

#slow-query-log=1
#slow-query-log-file=/usr/local/mysql/logs/slow_query.log


# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server = utf8mb4
# server_id = .....
sock = /var/lib/mysql/mysql.sock

# 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

(3) MySQL详细安装过程

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
[root@host_wkq local]# pwd
/usr/local
[root@host_wkq local]# ll
total 807716
drwxr-xr-x. 2 root root 6 May 25 2015 bin
drwxr-xr-x. 2 root root 6 May 25 2015 etc
drwxr-xr-x. 2 root root 6 May 25 2015 games
drwxr-xr-x. 2 root root 6 May 25 2015 include
drwxr-xr-x 8 10 143 4096 Mar 15 16:35 jdk1.8.0_131
-rw-r--r-- 1 root root 185540433 Aug 16 10:36 jdk-8u131-linux-x64.tar.gz
drwxr-xr-x. 2 root root 6 May 25 2015 lib
drwxr-xr-x. 2 root root 6 May 25 2015 lib64
drwxr-xr-x. 2 root root 6 May 25 2015 libexec
drwxr-xr-x 9 root root 120 Aug 16 10:37 mysql-5.7.16-linux-glibc2.5-x86_64
-rw-r--r-- 1 root root 641555814 Aug 16 10:35 mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 2 root root 6 May 25 2015 sbin
drwxr-xr-x. 5 root root 46 Jul 25 10:38 share
drwxr-xr-x. 2 root root 6 May 25 2015 src

[root@host_wkq local]# tar -zxvf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz #解压
mysql-5.7.16-linux-glibc2.5-x86_64/bin/myisam_ftdump
mysql-5.7.16-linux-glibc2.5-x86_64/bin/myisamchk
mysql-5.7.16-linux-glibc2.5-x86_64/bin/myisamlog
mysql-5.7.16-linux-glibc2.5-x86_64/bin/myisampack
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_client_test_embedded
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_config_editor
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_embedded
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_install_db
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_plugin
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_secure_installation
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_ssl_rsa_setup
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_tzinfo_to_sql
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysql_upgrade
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysqladmin
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysqlbinlog
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysqlcheck
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysqldump
mysql-5.7.16-linux-glibc2.5-x86_64/bin/mysqlimport

...

mysql-5.7.16-linux-glibc2.5-x86_64/share/serbian/errmsg.sys
mysql-5.7.16-linux-glibc2.5-x86_64/share/slovak/errmsg.sys
mysql-5.7.16-linux-glibc2.5-x86_64/share/spanish/errmsg.sys
mysql-5.7.16-linux-glibc2.5-x86_64/share/swedish/errmsg.sys
mysql-5.7.16-linux-glibc2.5-x86_64/share/ukrainian/errmsg.sys
mysql-5.7.16-linux-glibc2.5-x86_64/support-files/mysql-log-rotate
mysql-5.7.16-linux-glibc2.5-x86_64/support-files/mysqld_multi.server
mysql-5.7.16-linux-glibc2.5-x86_64/lib/libmysqlclient.so
mysql-5.7.16-linux-glibc2.5-x86_64/lib/libmysqlclient.so.20
mysql-5.7.16-linux-glibc2.5-x86_64/lib/libmysqlclient.so.20.3.3
mysql-5.7.16-linux-glibc2.5-x86_64/share/install_rewriter.sql
mysql-5.7.16-linux-glibc2.5-x86_64/share/uninstall_rewriter.sql
mysql-5.7.16-linux-glibc2.5-x86_64/support-files/magic
mysql-5.7.16-linux-glibc2.5-x86_64/support-files/mysql.server
mysql-5.7.16-linux-glibc2.5-x86_64/docs/INFO_BIN
mysql-5.7.16-linux-glibc2.5-x86_64/docs/INFO_SRC


[root@host_wkq local]# ln -s /usr/local/mysql-5.7.16-linux-glibc2.5-x86_64/ mysql #创建软链接,方便操作
[root@host_wkq local]# ll
total 807716
drwxr-xr-x. 2 root root 6 May 25 2015 bin
drwxr-xr-x. 2 root root 6 May 25 2015 etc
drwxr-xr-x. 2 root root 6 May 25 2015 games
drwxr-xr-x. 2 root root 6 May 25 2015 include
drwxr-xr-x 8 10 143 4096 Mar 15 16:35 jdk1.8.0_131
-rw-r--r-- 1 root root 185540433 Aug 16 10:36 jdk-8u131-linux-x64.tar.gz
drwxr-xr-x. 2 root root 6 May 25 2015 lib
drwxr-xr-x. 2 root root 6 May 25 2015 lib64
drwxr-xr-x. 2 root root 6 May 25 2015 libexec
lrwxrwxrwx 1 root root 46 Aug 16 10:44 mysql -> /usr/local/mysql-5.7.16-linux-glibc2.5-x86_64/
drwxr-xr-x 9 root root 120 Aug 16 10:37 mysql-5.7.16-linux-glibc2.5-x86_64
-rw-r--r-- 1 root root 641555814 Aug 16 10:35 mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 2 root root 6 May 25 2015 sbin
drwxr-xr-x. 5 root root 46 Jul 25 10:38 share
drwxr-xr-x. 2 root root 6 May 25 2015 src

[root@host_wkq local]# cd mysql #进入mysql目录
[root@host_wkq mysql]# ll
total 40
drwxr-xr-x 2 root root 4096 Aug 16 10:38 bin
-rw-r--r-- 1 7161 31415 17987 Sep 29 2016 COPYING
drwxr-xr-x 2 root root 52 Aug 16 10:38 docs
drwxr-xr-x 3 root root 4096 Aug 16 10:37 include
drwxr-xr-x 5 root root 4096 Aug 16 10:38 lib
drwxr-xr-x 4 root root 28 Aug 16 10:37 man
-rw-r--r-- 1 7161 31415 2478 Sep 29 2016 README
drwxr-xr-x 28 root root 4096 Aug 16 10:38 share
drwxr-xr-x 2 root root 107 Aug 16 10:38 support-files

[root@host_wkq mysql]# chown -R mysql . #更改当前目录所属的用户(也替换为 chown -R mysql /usr/local/mysql )
[root@host_wkq mysql]# chgrp -R mysql . #更改当前目录所属的组(也替换为 chgrp -R mysql /usr/local/mysql )

[root@host_wkq mysql]# cd bin
[root@host_wkq bin]# pwd
/usr/local/mysql/bin
[root@host_wkq bin]# ll
total 1355456
-rwxr-xr-x 1 mysql mysql 9207169 Sep 29 2016 innochecksum
-rwxr-xr-x 1 mysql mysql 251228 Sep 29 2016 lz4_decompress
-rwxr-xr-x 1 mysql mysql 8347930 Sep 29 2016 myisamchk
-rwxr-xr-x 1 mysql mysql 7922860 Sep 29 2016 myisam_ftdump
-rwxr-xr-x 1 mysql mysql 7676657 Sep 29 2016 myisamlog
-rwxr-xr-x 1 mysql mysql 8050890 Sep 29 2016 myisampack
-rwxr-xr-x 1 mysql mysql 5652398 Sep 29 2016 my_print_defaults
-rwxr-xr-x 1 mysql mysql 10884339 Sep 29 2016 mysql
-rwxr-xr-x 1 mysql mysql 9718874 Sep 29 2016 mysqladmin
-rwxr-xr-x 1 mysql mysql 11780342 Sep 29 2016 mysqlbinlog
-rwxr-xr-x 1 mysql mysql 10084494 Sep 29 2016 mysqlcheck
-rwxr-xr-x 1 mysql mysql 217782410 Sep 29 2016 mysql_client_test_embedded
-rwxr-xr-x 1 mysql mysql 4879 Sep 29 2016 mysql_config
-rwxr-xr-x 1 mysql mysql 8648715 Sep 29 2016 mysql_config_editor
-rwxr-xr-x 1 mysql mysql 253303409 Sep 29 2016 mysqld
-rwxr-xr-x 1 mysql mysql 224181321 Sep 29 2016 mysqld-debug
-rwxr-xr-x 1 mysql mysql 26705 Sep 29 2016 mysqld_multi
-rwxr-xr-x 1 mysql mysql 27186 Sep 29 2016 mysqld_safe
-rwxr-xr-x 1 mysql mysql 9989115 Sep 29 2016 mysqldump
-rwxr-xr-x 1 mysql mysql 7424 Sep 29 2016 mysqldumpslow
-rwxr-xr-x 1 mysql mysql 217423259 Sep 29 2016 mysql_embedded
-rwxr-xr-x 1 mysql mysql 9734439 Sep 29 2016 mysqlimport
-rwxr-xr-x 1 mysql mysql 10711017 Sep 29 2016 mysql_install_db
-rwxr-xr-x 1 mysql mysql 5722776 Sep 29 2016 mysql_plugin
-rwxr-xr-x 1 mysql mysql 18165530 Sep 29 2016 mysqlpump
-rwxr-xr-x 1 mysql mysql 9664543 Sep 29 2016 mysql_secure_installation
-rwxr-xr-x 1 mysql mysql 9669356 Sep 29 2016 mysqlshow
-rwxr-xr-x 1 mysql mysql 9778641 Sep 29 2016 mysqlslap
-rwxr-xr-x 1 mysql mysql 6098192 Sep 29 2016 mysql_ssl_rsa_setup
-rwxr-xr-x 1 mysql mysql 216919461 Sep 29 2016 mysqltest_embedded
-rwxr-xr-x 1 mysql mysql 5211835 Sep 29 2016 mysql_tzinfo_to_sql
-rwxr-xr-x 1 mysql mysql 13025173 Sep 29 2016 mysql_upgrade
-rwxr-xr-x 1 mysql mysql 29531087 Sep 29 2016 mysqlxtest
-rwxr-xr-x 1 mysql mysql 5790065 Sep 29 2016 perror
-rwxr-xr-x 1 mysql mysql 5431432 Sep 29 2016 replace
-rwxr-xr-x 1 mysql mysql 5651159 Sep 29 2016 resolveip
-rwxr-xr-x 1 mysql mysql 5732163 Sep 29 2016 resolve_stack_dump
-rwxr-xr-x 1 mysql mysql 109100 Sep 29 2016 zlib_decompress

# 执行安装脚本,--user 指的是mysql用户,--basedir是数据库的安装目录,可以自定义,--datadir是数据存放目录,可以自定义
# 这一步会生成随机密码,一定要保存
[root@host_wkq bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2017-08-16T02:48:19.810647Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-08-16T02:48:27.480885Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-08-16T02:48:29.962049Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-08-16T02:48:30.134064Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 62d4c750-822d-11e7-98fc-fa163e50f135.
2017-08-16T02:48:30.223416Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-16T02:48:30.224491Z 1 [Note] A temporary password is generated for root@localhost: wgU;&c,hn6V,

[root@host_wkq bin]# cd ..
[root@host_wkq mysql]# pwd
/usr/local/mysql
[root@host_wkq mysql]# ll
total 40
drwxr-xr-x 2 mysql mysql 4096 Aug 16 10:38 bin
-rw-r--r-- 1 mysql mysql 17987 Sep 29 2016 COPYING
drwxr-x--- 5 mysql mysql 139 Aug 16 10:48 data
drwxr-xr-x 2 mysql mysql 52 Aug 16 10:38 docs
drwxr-xr-x 3 mysql mysql 4096 Aug 16 10:37 include
drwxr-xr-x 5 mysql mysql 4096 Aug 16 10:38 lib
drwxr-xr-x 4 mysql mysql 28 Aug 16 10:37 man
-rw-r--r-- 1 mysql mysql 2478 Sep 29 2016 README
drwxr-xr-x 28 mysql mysql 4096 Aug 16 10:38 share
drwxr-xr-x 2 mysql mysql 107 Aug 16 10:38 support-files


[root@host_wkq mysql]# cp -a ./support-files/my-default.cnf /etc/my.cnf # 复制mysql配置文件
cp: overwrite ‘/etc/my.cnf’? y
[root@host_wkq mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysqld # 复制mysql配置文件

[root@host_wkq mysql]# cd bin
[root@host_wkq bin]# pwd
/usr/local/mysql/bin


[root@host_wkq bin]# ./mysqld_safe --user=mysql & #启动守护进程
[1] 18188
[root@host_wkq bin]# 2017-08-16T02:53:55.647112Z mysqld_safe Logging to '/usr/local/mysql/data/host_wkq.err'.
2017-08-16T02:53:55.745534Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

[root@host_wkq bin]#
[root@host_wkq bin]#
[root@host_wkq bin]# /etc/init.d/mysqld restart #启动mysql
Shutting down MySQL..2017-08-16T02:55:43.053119Z mysqld_safe mysqld from pid file /usr/local/mysql/data/host_wkq.pid ended
SUCCESS!
Starting MySQL.. SUCCESS!
[1]+ Done ./mysqld_safe --user=mysql
[root@host_wkq bin]#


[root@host_wkq bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin #创建软链接,避免启动失败


[root@host_wkq bin]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16

Copyright (c) 2000, 2016, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> SET PASSWORD = PASSWORD('mysql_root_password'); #修改密码
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| 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 |
+---------------------------+
31 rows in set (0.00 sec)

mysql> select user,host from user;
+-----------+-----------+
| user | host |
+-----------+-----------+
| mysql.sys | localhost |
| root | localhost |
+-----------+-----------+
2 rows in set (0.00 sec)

mysql> alter user 'root'@'localhost' password expire never; #设置root账户永不过期
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges; #把对数据库的操作同步到内存
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
[root@host_wkq bin]#

expire

Windows安装配置MySQL

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

所有系统所有版本的下载地址

MYSQL=C:\ProfessionalSoftware\MySQL\mysql-5.7.22-winx64
%MySQL%\bin

References

[1] 官方文档
[2] server-configuration-defaults
[3] option-files
[4] Mysql 5.7 Linux安装详细步骤
[5] Linux安装MySQL的两种方法
[6] linux下mysql的root密码忘记解决方法
[7] Linux下MySQL忘记root密码怎么办
[8] MySQL 二进制日志(Binary Log)
[9] MySQL5.7 开启bin-log功能
[10] MySQL 5.7 开启binary log(binlog)及注意事项