美文网首页
mysql 安装

mysql 安装

作者: 泡水鱼干 | 来源:发表于2021-05-17 19:53 被阅读0次

    包含 windows、linux、mac下mysql安装,linux下MariaDB安装

    下载

    MySQL 5.7 网易镜像官网 5.7官网 8

    win10 安装

    下载

    下载 windows(x86,64-bit),ZIP Archive

    解压文件

    将下载的压缩包解压到指定目录(比如:E:\install_work\mysql)

    配置文件

    发现 mysql 根目录下没有 data 目录和 my.ini 文件,初始化 mysql 时会自动创建 data 目录,我们只需创建一个 my.ini 文件即可

    [Client]
    port = 3306
    
    [mysqld]
    #设置3306端口
    port = 3306
    # 设置mysql的安装目录
    basedir=E:\install_work\mysql
    # 设置mysql数据库的数据的存放目录
    datadir=E:\install_work\mysql\data
    # 允许最大连接数
    max_connections=200
    # 服务端使用的字符集默认为8比特编码的latin1字符集
    character-set-server=utf8
    # 创建新表时将使用的默认存储引擎
    default-storage-engine=INNODB
    
    [mysql]
    # 设置mysql客户端默认字符集
    default-character-set=utf8
    

    初始化

    搜索 cmd 找到命令提示符,右键以管理员身份运行,进入 bin 目录,输入以下命令:

    mysqld --initialize --user=mysql --console
    

    初始化成功后,系统会在 mysql 目录下创建 data 目录,并生成初始密码。


    004.png

    安装 mysql

    mysqld --install mysql
    

    显示 Service successfully installed.表示安装成功。

    启动 mysql

    第一种

    cmd 中运行 services.msc 打开服务窗口,在服务窗口中找到 mysql 服务,点击右键可以启动或停止

    第二种

    以管理员身份运行 cmd

    • 停止:net stop mysql
    • 启动:net start mysql

    登录 mysql

    mysql -u root -p 输入初始密码
    

    修改登录密码

    set password=password('新密码');
    

    配置环境变量

    选择 Path,将我们的 bin 目录添加到环境变量中。
    (1)新建系统变量 MYSQL_HOME (mysql 根目录),并配置变量值为 E:\install_work\mysql;

    009.png

    (2)编辑系统变量 Path ,将 ;%MYSQL_HOME%\bin 添加到 Path 变量值后面。

    010.png

    配置好后,下次即可用新密码登陆 mysql。

    问题

    输入 mysqld --initialize --user=mysql --console 时出现

    由于系统找不到 MSVCR120.dll,无法继续执行代码。重新安装程序可能会解决此问题

    由于系统找不到 MSVCP120.dll,无法继续执行代码。重新安装程序可能会解决此问题

    解决方案

    下载 DirectX 修复工具增强版

    mac 安装

    使用 brew install 进行安装

    linux 安装

    安装前

    环境依赖

    yum search libaio
    yum install libaio
    # 先安装Perl modules
    yum install -y perl-Module-Install.noarch
    

    下载

    wget -c http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
    

    卸载

    如果之前没有安装,跳过此步骤

    停用
    service mysqld stop
    # 或者杀掉进程
    netstat -apn|grep 3306
    
    检查是否安装
    # 通过rpm包安装的检查方法
    [root@localhost /]> rpm -qa|grep -i mysql
    # 通过 yum 安装
    [root@localhost /]> yum search mysql
    # linux 系统其他方法
    [root@localhost /]> which mysql
    /usr/local/mysql/bin/mysql
    [root@localhost /]> whereis mysql
    mysql: /usr/lib64/mysql /usr/local/mysql /usr/share/mysql /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysql
    
    卸载
    rpm -qa mysql # 删除默认携带的mysql
    
    # 卸载旧版本数据库
    rpm -e mysql    # 普通删除模式
    rpm -e --nodeps mysql    # 强力删除模式,使用上面命令删除时,提示有依赖的文件,则用该命令可以对其进行强力删除
    yum -y remove mysql-libs* # 移除关联库
    
    find / -name mysql # 将查找到的删除掉
    rpm -qa|grep mysql # 将查询到的 yum remove掉
    

    解压

    tar -xzvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
    

    创建软连接

    cd /usr/local
    ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql
    

    创建用户

    # 创建用户组 mysql
    groupadd mysql
    # 创建用户 mysql 用户仅拥有所有权,没有登陆系统权限
    useradd -r -g mysql -s /bin/false mysql
    

    修改所有者

    cd /usr/local/mysql
    chown -R mysql:mysql ./
    

    安装

    [root@localhost bin]> ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
    2021-03-31T04:41:09.898572Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2021-03-31T04:41:10.286615Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2021-03-31T04:41:10.356393Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2021-03-31T04:41:10.423584Z 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: 50ca58c4-91db-11eb-a3c9-001c42b16902.
    2021-03-31T04:41:10.425456Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2021-03-31T04:41:11.315322Z 0 [Warning] CA certificate ca.pem is self signed.
    2021-03-31T04:41:11.478109Z 1 [Note] A temporary password is generated for root@localhost: Uh6=I#S.7e%W
    # 上行的随机密码用于登陆
    

    使用 –initial-insecure,则会创建空密码的 root@localhost 账号

    开启服务

    [root@localhost mysql]> ./support-files/mysql.server start
    Starting MySQL.2021-03-31T04:45:06.414049Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
     ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
    [root@localhost log]> mkdir mariadb
    [root@localhost log]> echo "" > ./mariadb/mariadb.log
    [root@localhost log]> chown -R mysql:mysql mariadb
    [root@localhost mysql]> cd /var/lib/
    [root@localhost lib]> mkdir mysql
    [root@localhost lib]> echo "" > ./mysql/localhost.localdomain.pid
    [root@localhost mysql> vim /etc/my.cnf
    # 修改datadir和socket将目录指向初始化指定目录,如果/etc下没有my.cnf,创建
    [mysqld]
    datadir=/usr/local/mysql/data
    socket=/usr/local/mysql/data/mysql.sock
    character_set_server=utf8
    init_connect='SET NAMES utf8'
    [client]
    port=3306
    socket=/usr/local/mysql/data/mysql.sock
    default-character-set=utf8
    [root@localhost mysql]> ./support-files/mysql.server start
    Starting MySQL SUCCESS!
    

    此处如果不使用 mysql 提供脚本,可以使用下面脚本启动

    mysqld_safe --defaults-file=my.cnf &
    

    配置摘要

    [mysqld]
    # 端口设置
    port = 3306
    # mysql.sock
    socket = /home/mysql/mysqldb5.7/mysql-5.7.16/mysql.sock
    # 基础目录
    basedir = /home/mysql/mysqldb5.7/mysql-5.7.16
    # 数据存储目录:
    datadir = /home/mysql/mysqldb5.7/mysql-5.7.16/data
    # 日志文件:
    log-error = /home/mysql/mysqldb5.7/mysql-5.7.16/logs/mysql_error.log
    # 进程文件:
    pid-file = /home/mysql/mysqldb5.7/mysql-5.7.16/mysql.pid
    # 所属用户
    user = mysql
    # mysql5.5之后
    character-set-server = utf8
    #mysql5.5之前
    #default-character-set = utf8
    # 表名大小写(0:大小写敏感;1:大小写不敏感)
    lower_case_table_names = 1
    

    放入系统进程

    [root@localhost mysql]$ cp support-files/mysql.server /etc/init.d/mysqld
    
    # 查看自启动列表中有没有mysqld
    [root@localhost support-files]> chkconfig --list mysqld
    注:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
           要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies [target]'。
    
    服务 mysqld 支持 chkconfig,但它未在任何运行级别中被引用(请运行“chkconfig --add mysqld”)
    # 添加mysqld到自启动列表
    [root@localhost support-files]> chkconfig --add mysqld
    [root@localhost support-files]> chkconfig --list mysqld
    
    注:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
           要列出 systemd 服务,请执行 'systemctl list-unit-files'。查看在具体 target 启用的服务请执行'systemctl list-dependencies [target]'。
    
    mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关
    # 设置开机启动
    [root@localhost support-files]> chkconfig mysqld on
    

    重启 mysql

    [root@localhost mysql]> service mysqld restart
    Shutting down MySQL.. SUCCESS!
    Starting MySQL. SUCCESS!
    # 启动
    [root@localhost mysql]> service mysqld start
    # 停止
    [root@localhost mysql]> service mysqld stop
    

    环境变量

    [root@localhost bin]# vim ~/.bash_profile
    export PATH=/usr/local/mysql/bin:$PATH # 添加mysql的环境变量
    [root@localhost bin]# source ~/.bash_profile
    

    登陆

    使用初始化生成的临时密码

    [root@localhost mysql]# mysql -uroot -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.31
    
    Copyright (c) 2000, 2020, 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>
    

    如果登陆时候:[ERROR] unknown variable 'sql_mode=NO_ENGIN_SUBSTITUTION,STRICT_TRANS_TABLES',在配置文件中取消相应注释,或添加相应配置

    修改密码

    mysql> ALTER user 'root'@'localhost' identified by '123456';
    Query OK, 0 rows affected (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> UPDATE user set user.Host='%' WHERE user.User='root';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    

    刷新权限

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

    系统信息

    mysql> \s
    --------------
    mysql  Ver 14.14 Distrib 5.7.31, for linux-glibc2.12 (x86_64) using  EditLine wrapper
    
    Connection id:          2
    Current database:
    Current user:           root@localhost
    SSL:                    Not in use
    Current pager:          stdout
    Using outfile:          ''
    Using delimiter:        ;
    Server version:         5.7.31 MySQL Community Server (GPL)
    Protocol version:       10
    Connection:             Localhost via UNIX socket
    Server characterset:    latin1
    Db     characterset:    latin1
    Client characterset:    utf8
    Conn.  characterset:    utf8
    UNIX socket:            /usr/local/mysql/data/mysql.sock
    Uptime:                 8 sec
    
    Threads: 1  Questions: 5  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.625
    -----
    

    编码修改

    mysql> show variables like 'character%';
    +--------------------------+----------------------------------------------------------------+
    | Variable_name            | Value                                                          |
    +--------------------------+----------------------------------------------------------------+
    | character_set_client     | utf8                                                           |
    | character_set_connection | utf8                                                           |
    | character_set_database   | latin1                                                         |
    | character_set_filesystem | binary                                                         |
    | character_set_results    | utf8                                                           |
    | character_set_server     | latin1                                                         |
    | character_set_system     | utf8                                                           |
    | character_sets_dir       | /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/share/charsets/ |
    +--------------------------+----------------------------------------------------------------+
    8 rows in set (0.01 sec)
    
    mysql> set character_set_database=utf8;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    

    MariaDB安装

    安装MariaDB

    安装

    $ yum -y install mariadb mariadb-server
    $ systemctl start mariadb
    $ systemctl enable mariadb
    # MariaDB的相关简单配置,设置密码,会提示先输入密码
    $ mysql_secure_installation
    初次安装root密码为空,直接回车
    Y+回车(设置密码)
    是否删除匿名用户,回车
    是否禁止root远程登录,回车(根据情况设置)
    是否删除test数据库,回车
    是否重新加载权限表,回车
    $ mysql -uroot -p
    

    配置

    # 备份配置文件
    $ cp /etc/my.cnf{,.bak}
    # /etc/my.cnf在[mysqld]标签下添加
    init_connect=‘SET collation_connection = utf8_unicode_ci’
    init_connect=‘SET NAMES utf8’
    character-set-server=utf8
    collation-server=utf8_unicode_ci
    skip-character-set-client-handshake
    
    # 备份配置文件
    $ cp /etc/my.cnf.d/client.cnf{,.bak}
    # /etc/my.cnf.d/client.cnf,在[client]中添加
    default-character-set=utf8
    
    # 备份配置文件
    cp /etc/my.cnf.d/mysql-clients.cnf{,.bak}
    # 配置文件/etc/my.cnf.d/mysql-clients.cnf,在[mysql]中添加
    default-character-set=utf8
    
    $ systemctl restart mariadb
    $ mysql -uroot -p
    MariaDB [(none)]> show variables like "%character%";
    MariaDB [(none)]> show variables like "%collation%";
    
    # 在MariaDB数据库中创建jumpserver库,并授权连接
    MariaDB [(none)]> create database jumpserver;
    MariaDB [(none)]> grant all on jumpserver.* to root@'192.168.3.%' identified by "root";
    MariaDB [(none)]> grant all on jumpserver.* to jumpserver@'192.168.3.%' identified by "root";
    MariaDB [(none)]> flush privileges;
    

    相关文章

      网友评论

          本文标题:mysql 安装

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