Windows下安装配置免安装MySQL5.7服务器
1、下载、解压安装包
从MySQL官方网站上下载mysql-5.7.19-winx64.zip
下载完成后,把安装包解压到D:\DevSoftware\mysql57\目录下,会生成一个mysql-5.7.19-winx64目录。现在MySQL的目录就是D:\DevSoftware\mysql57\mysql-5.7.19-winx64
2、创建数据目录
在D:\DevSoftware\mysql57\下创建一个data目录,作为mysql服务器的数据目录。
3、创建配置文件
在D:\DevSoftware\mysql57\mysql-5.7.19-winx64\创建一个my.ini文件,做为mysql的配置文件。
其内容为:
[mysqld]
basedir=D:/DevSoftware/mysql57/mysql-5.7.19-winx64
datadir=D:/DevSoftware/mysql57/data
port=3306
现在MySQL安装目录中的结构是这样的:
D:\DevSoftware\mysql57\
|--mysql-5.7.19-winx64\
|--data
|--mysql-5.7.19-winx64\my.ini
这样MySQL软件的目录,数据目录data,和配置文件,就形成了这样一个结构。
4、添加mysql的bin目录到环境变量path中
把D:\DevSoftware\mysql57\mysql-5.7.19-winx64\bin目录添加到PATH环境变量中去。
5、把mysql服务器安装成windows服务
以管理员权限运行CMD,进入到D:\DevSoftware\mysql57\mysql-5.7.19-winx64\bin目录中,执行:
mysqld --install
就可以把mysql服务器注册成windows服务了。
D:\DevSoftware\mysql57\mysql-5.7.19-winx64\bin>mysqld --install
Service successfully installed.
6、初始化MySQL数据库
在CMD中,进入到mysql安装目录的bin下,执行
mysqld --initialize --console
就可以初始化数据库了。如果初始化过程中报错,一般是data目录下有文件,进入data目录,删除其中的文件,再次执行初始化语句,就可以成功了。
初始化完成后,初始化信息中会给出root用户的随机密码,记住这个密码,接下来登陆mysql服务器时,需要输入这个密码。
D:\DevSoftware\mysql57\mysql-5.7.19-winx64\bin>mysqld --initialize --console
2017-08-04T10:06:35.200271Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-08-04T10:06:36.851562Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-08-04T10:06:37.312575Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-08-04T10:06:37.655489Z 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: 9a753507-78fc-11e7-a67d-54e1ad300ba0.
2017-08-04T10:06:37.691852Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-04T10:06:37.713805Z 1 [Note] A temporary password is generated for root@localhost: lwuW>7sxauEG
7、启动mysql服务器
因为mysql服务器已经被注册成Windows服务了,所以可以通过启动Windows服务的方式去启动mysql服务器。
在cmd中执行net start mysql。
D:\DevSoftware\mysql57\mysql-5.7.19-winx64\bin>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
8、登陆mysql服务器,并修改root密码
启动mysql服务器之后,就可以登陆服务器了。登陆服务器的时候需要输入root密码,root的密码就是初始化数据库后,系统给出的随机密码。
登陆到服务器之后,不能执行其他语句,必须先修改root用户密码。具体信息,见下图:
D:\DevSoftware\mysql57\mysql-5.7.19-winx64\bin>mysql -uroot -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.19 Copyright (c) 2000, 2017, 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('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
修改root密码后,就可以在mysql中正常执行其他的命令了。可以试试看是否能执行sql命令。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec) mysql> create database test;
Query OK, 1 row affected (0.00 sec) mysql> use test;
Database changed
mysql> create table a1(a int, b int);
Query OK, 0 rows affected (0.25 sec) mysql> insert into a1 values(1,2), (3, 4), (5, 6);
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from a1;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
+------+------+
3 rows in set (0.00 sec) mysql>
这样就装好了MySQL数据库。