MySQL安装

作者: Eniso | 来源:发表于2019-02-18 17:48 被阅读0次

    下载

    官方网站
    https://www.mysql.com

    选择社区版本 MySQL Community Downloads
    https://dev.mysql.com/downloads/

    选择服务器版Download MySQL Community Server
    https://dev.mysql.com/downloads/mysql/

    例如:下载mysql-8.0.15
    https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.15-winx64.zip

    安装

    • 解压

    假设把mysql-8.0.15-winx64.zip解压到下面的目录

    C:\Program Files\Apps\mysql-8.0.15-winx64

    • 配置环境变量

    在Path添加 C:\Program Files\Apps\mysql-8.0.15-winx64\bin

    • 编写配置文件

    将下面的代码保存到 C:\Program Files\Apps\mysql-8.0.15-winx64\bin\my.ini

    [mysqld]
    
    # The TCP/IP Port the MySQL Server will listen on
    port=3306
    
    # 服务端使用的字符集默认为utf8mb4(是utf8的超集)
    character-set-server = utf8mb4
    
    # 设置mysql的安装目录
    basedir = C:/Program Files/Apps/mysql-8.0.15-winx64
    
    # 设置mysql数据库的数据的存放目录
    datadir = C:/Program Files/Apps/mysql-8.0.15-winx64/data
    
    # 允许最大连接数
    max_connections = 200
    
    # 允许连接失败的次数(防止试图攻击数据库系统)
    max_connect_errors = 10
    
    # 创建新表时将使用的默认存储引擎
    default-storage-engine = INNODB
    
    # 默认使用"mysql_native_password"插件认证
    default_authentication_plugin = mysql_native_password
    
    
    [client]
    
    port = 3306
    
    default-character-set = utf8mb4
    
    
    • 初始化数据库
    mysqld --initialize --console
    
    2019-02-18T03:59:08.753060Z 0 [System] [MY-013169] [Server] C:\Program Files\Apps\mysql-8.0.15-winx64\bin\mysqld.exe (mysqld 8.0.15) initializing of server in progress as process 15692
    2019-02-18T03:59:13.102051Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 7zd%T?-F6*lu
    2019-02-18T03:59:15.001455Z 0 [System] [MY-013170] [Server] C:\Program Files\Apps\mysql-8.0.15-winx64\bin\mysqld.exe (mysqld 8.0.15) initializing of server has completed
    

    如果没有记下密码,可以删除datadir = C:/Program Files/Apps/mysql-8.0.15-winx64/data目录,重新执行初始化。初始化成功后,可以看到账号/密码为:root/7zd%T?-F6*lu

    • 安装服务
    mysqld --install MySQL --defaults-file="C:\Program Files\Apps\mysql-8.0.15-winx64\my.ini"
    

    成功安装时的输出类似:

    Service successfully installed.

    • 卸载
    mysqld --remove MySQL
    

    成功卸载时的输出类似:

    Service successfully removed.

    • 启动MySQL服务
    net start MySQL
    

    成功启动时的输出类似:

    MySQL 服务正在启动 ..
    MySQL 服务已经启动成功。

    • 登录
    mysql -uroot -p
    Enter password: ************
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 9
    Server version: 8.0.15
    
    Copyright (c) 2000, 2019, 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>
    
    • 修改密码
    alter user user() identified by "123456";
    

    命令总结

    # 生成data目录,console会显示root用户密码
    mysqld --initialize --console
    # 安装MySQL服务,名称为MySQL,使用配置文件
    mysqld --install MySQL --defaults-file="C:\Program Files\Apps\mysql-8.0.15-winx64\my.ini"
    # 启动MySQL服务
    net start MySQL
    # 登录
    mysql -uroot -p (输入密码后进入mysql)
    # 如果没有修改密码,操作的时候会出现如下错误
    # ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    # 通过下面命令修改密码
    alter user user() identified by "123456";
    

    相关文章

      网友评论

        本文标题:MySQL安装

        本文链接:https://www.haomeiwen.com/subject/qdlzeqtx.html