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 即可

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM