首先下载MySQL5.7的压缩包:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
1.解压缩到目标文件夹,解压后有许多文件,介绍一下用到的几个(在bin目录下)
mysqld:SQL的守护进程(即MySQL服务进程),为了运行客户端程序,mysqld必须运行,因为客户程序需要通过连接服务访问数据库
mysql:命令行工具,用于交互式地输入SQL语句或以批处理模式从文件执行SQL语句
mysqladmin:一个用于执行管理操作的客户端程序,例如创建或删除数据库,重载授权表,将表刷新到磁盘,以及重新打开日志文件。mysqladmin还可以用于从服务器检索版本、流程和状态信息
2.创建一个配置文件
配置文件有很多种,读取顺序如下表,后读取的优先级更高,即文件终端配置选项会覆盖前面的配置选项,可以使用mysql --help查看读取的配置文件
File NamePurpose
%WINDIR%\my.ini, %WINDIR%\my.cnf
Global options
C:\my.ini, C:\my.cnf
Global options
BASEDIR\my.ini, BASEDIR\my.cnf
Global options
defaults-extra-file
The file specified with --defaults-extra-file, if any
%APPDATA%\MySQL\.mylogin.cnf
Login path options (clients only)
为了方便,这里就不展开讲了,直接在mysql解压目录下创建一个my.ini配置文件。用文本编辑器打开,输入以下内容(这里假设将压缩包解压在E盘根目录下)然后保存
[mysqld]
# mysql的安装目录(windows下使用双斜杠,或者用'/'也行)basedir=E:\\mysql
# mysql的数据目录datadir=E:\\mydata\\data
#设置端口,可省略
port=3306
[mysqladmin]
#端口配置可以省略,但mysaladmin的操作是根据端口来进行的
port=3306
注意:从MySQL 5.7.6开始,ZIP文件不再包含data目录,所以需要手动创建,使用以下命令
C:\> bin\mysqld --initialize --console
C:\> bin\mysqld --initialize-insecure --console
--initialize会创建一个随机的root账户密码,而--initialize-insecure则使用空密码,--console选项用于显示详细信息
3.启动mysql服务
注意:这里只是运行mysql服务,还没有将其作为为windows服务来运行
切换到bin目录下输入命令
E:\mysql\bin> mysqld --console
如不使用--console选项则详细信息会存储到到data目录下扩展名为.err的文件中
如果运行成功应该看到和下面差不多的信息
InnoDB: The first specified datafile c:\ibdata\ibdata1 did not exist:
InnoDB: a new database to be created!InnoDB: Setting file c:\ibdata\ibdata1 size to209715200InnoDB: Database physically writes the file full: wait...
InnoDB: Log file c:\iblogs\ib_logfile0 did not exist: new to be created
InnoDB: Setting log file c:\iblogs\ib_logfile0 size to31457280InnoDB: Log file c:\iblogs\ib_logfile1 did not exist: new to be created
InnoDB: Setting log file c:\iblogs\ib_logfile1 size to31457280InnoDB: Log file c:\iblogs\ib_logfile2 did not exist: new to be created
InnoDB: Setting log file c:\iblogs\ib_logfile2 size to31457280InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: creating foreign key constraint system tables
InnoDB: foreign key constraint system tables created011024 10:58:25 InnoDB: Started
如果mysql服务完成了启动并显示类似这样的信息,说明已经准备好接受客户端程序的连接
mysqld: ready forconnections
Version: '5.7.25' socket: '' port: 3306
4.用命令行工具mysql连接服务
E:\mysql\bin>mysql -u root -p
成功连接
可以使用mysqladmin工具结束mysql服务(密码为空,不为空要使用p选项)
E:\mysql\bin>mysqladmin -u root shutdown
5.将mysql作为windows服务启动
如前面还没暂停mysql服务,需先暂停(前面有讲),然后使用如下命令安装服务
E:\mysql\bin>mysqld --install
安装完成后即可用net start mysql服务启动服务,然后用mysql -u root -p成功连接
可以使用下面命令查看运行状态,端口以测试服务是否运行正常
E:\mysql\bin>mysqladmin -u root -p version status proc
6.关于移除mysql服务
首先 net stop mysql关闭mysql服务,然后输入
E:\mysql\bin>mysqld --remove