MySQL8.0安装与使用

Windows

  1.下载:https://dev.mysql.com/downloads/mysql/

               推荐使用镜像:http://mirrors.sohu.com/mysql/MySQL-8.0/

 

  2.下载完成之后, 解压到目录下,例如:D:\mysql-8.0.22-winx64

 

  3. 设置环境变量

    变量名称:MYSQL_HOME

    变量值:D:\mysql-8.0.22-winx64

 

 

 

  4. 生成data目录

 

 

          注意: 以管理员身份运行cmd

  进入到bin目录下D:\mysql-8.0.22-winx64\bin

       执行命令:

mysqld --initialize-insecure --user=mysql

  则会在bin目录的同级生成data目录

 

  5. 安装MySQL

   

mysqld  -install

 

 

 

  6. 启动服务

net start MySQL

 

 

  7. 登录MySQL

mysql  -u root -p

//默认没有设置密码, 直接回车即可

  8)查询用户密码

select host,user,authentication_string from mysql.user

 

 

  9)密码修改

use mysql;



ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

flush privileges;


 

  10)再次登录

 

 

  11)mysql配置

            默认mysql8.0是data目录中是不存在my.ini配置文件的,如有需要新建即可 

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/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]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
# 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
# These are commonly set, remove the # and set as required.
basedir = D:\mysql-8.0.22-winx64
datadir = D:\mysql-8.0.22-winx64\data
port = 3306
# server_id = .....
# 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 = 16M
read_rnd_buffer_size = 16M 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

        设置完成之后, 可以设置mysql默认的配置文件

 

mysqld --defaults-file="D:\mysql-8.0.22-winx64\data\my.ini"

  然后重启服务即可, 如果你在开始安装的时候已经创建了配置文件,可以在安装的时候直接指定即可

mysqld --install "MySql80" --defaults-file="C:\install\mysql\mysql-8.0.16-winx64\my.ini"

 

 

其他相关命令:

  1. 停止MySQL服务

net stop  mysql

 

Linux

  1.下载rpm包

Wget  https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

  2.使用yum进行安装

yum install mysql80-community-release-el7-3.noarch.rpm

  3.查看yum中的mysql版本

yum repolist all | grep mysql

  4.使用yum-config-manager进行安装

yum-config-manager  命令不存在的时候,安装yum-config-manager
yum -y install yum-utils

  5.选择安装的mysql版本

sudo yum-config-manager --disable mysql57-community 
sudo yum-config-manager --enable mysql80-community

  6.查看已选择的版本

yum repolist enabled | grep mysql

  如果系统是centos8的话,需要单独操作

sudo yum module disable mysql

  7.使用yum进行安装

yum install mysql


由于网络等问题, 使用yum下载MySQL相关包失败, 无奈只能使用rpm进行安装

 

  rpm:

  1)检查mysql是否安装

rpm -qa|grep mysql
Rpm -qa|grep mariadb
如果存在mariadb则卸载
Yum remove mariadb-libs-...

  2)设置阿里云yum源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

  3)生成缓存

Yum makecache

  4)下载rpm包(http://mirrors.ustc.edu.cn/mysql-ftp/Downloads

1)下载server
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/mysql-community-server-8.0.22-1.el7.x86_64.rpm

2)Client
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/mysql-community-client-8.0.22-1.el7.x86_64.rpm

3)Common
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/mysql-community-common-8.0.22-1.el7.x86_64.rpm

4)Lib
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/mysql-community-libs-8.0.22-1.el7.x86_64.rpm

  5)首先安装环境依赖

yum install -y perl.x86_64 
yum install -y libaio.x86_64 
yum install -y net-tools.x86_64

  6)安装

rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm 
rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm 
rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm 
rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm

  7)启动mysql服务

Systemctl start mysqld

  8)安装之后,查看默认的登录密码

Grep ‘temporary password’ /var/log/mysqld.log

  9)登录设置修改初始密码,并允许远程连接

Mysql -u root -p 123456
Alter user ‘root’@’localhost’ identified with mysql_native_password by ‘password’;

允许远程访问:
update user set Host='%' where Host='localhost' and 'User'='root';

立即生效:
Flush privileges;

  10)开放相应端口

-- 查看已经开放的端口
firewall-cmd --list-ports  

-- 开启端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent 

 

 

 

 注意点:

  1. 修改密码8.0版本中已经没有password函数

    需要使用mysql_native_password 来进行修改

 

  2.  mysql8.0版本必须符合长度(默认是8位),且必须含有数字,小写或大写字母,特殊字符。因为实在我本地自己的库,所以我修改了密码最小长度和密码的策略

 

  3. 远程连接数据库 出现 Client does not support authentication protocol requested by server(使用mysql_native_password 操作)

 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

 

  4.使用docker运行mysql8之后就行链接报错:

Can't connect to local MySQL server through socket '/tmp/mysql.sock

      最后发现是我连接的时候没有指定 -h   需要指定一下-h 127.0.0.1 即可

 

posted @ 2020-11-25 19:02  X-Wolf  阅读(1593)  评论(0编辑  收藏  举报