Win下 MySQL 免安裝配置 及 常見問題處理 方法

1、MySQL下載
鏈接:http://pan.baidu.com/s/1miQdB4G 密碼:a7p1

2、下載后將文件解壓到安裝目錄下
(G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64)

3、在安裝目錄下將文件 my-default.ini 重命名為 my.ini,用記事本打開

需要修改的地方:
basedir 、datadir:改成安裝目錄
port:3036
注意:data 目錄在初始化時會自動生成一些文件,目錄需提前建好

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/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.

[mysqld]

# 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 = G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin   # 該參數指定了安裝 MySQL 的安裝路徑
datadir = G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\data    該參數指定了 MySQL 數據庫data的文件放在哪個路徑下
port = 3306
# server_id = XX.XX.XX.XX


# 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 = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
skip-grant-tables

4、配置環境變量
我的電腦-->屬性-->高級系統設置-->環境變量
編輯系統變量 Path (即添加 安裝目錄下的bin文件路徑)
將 **G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin ** 添加到 Path 變量值后面。

5、以管理員身份運行命令提示符 cmd 進入安裝目錄,切換至bin目錄
(一定要用管理員身份運行,不然權限不夠)

開始->運行->cmd,例如我要進入G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin
1)先進入G磁盤,輸入 G:回車
2)cd Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin

注意點:若直接輸 CD G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin,目錄不會切換,但在下次輸入盤符的時候進入上一次希望進入的目錄,如輸入G:會直接進入安裝目錄)

C:\Windows\system32>cd G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin
C:\Windows\system32>G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin
'G:\Program' 不是內部或外部命令,也不是可運行的程序或批處理文件。
C:\Windows\system32>G:
G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin

5.1 安裝服務,執行 mysqld -install

 G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin>mysqld -install
 Service successfully installed.      //成功安裝服務

5.2 初始化data目錄, 執行 mysqld --initialize-insecure --user=mysql;(免密)

此步驟非常重要,未初始化,可能會導致mysql服務無法啟動

注意:MySQL 5.7.17初始化時創建了臨時密碼,在data目錄下的 LAPTOP-OOR4C1HG.err 文件打開后,查看第一個Note,可以找到臨時密碼。

 G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin>mysqld -initialize
 G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin>
自動生成的data目錄.png

打開 err 文件可以看到給 root 創建了空密碼
2017-07-28T14:49:27.797774Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

若要免密,則執行 mysqld --initialize-insecure --user=mysql;

5.3 設置root免密登錄 (初始化時未設置免密)
1)在配置文件 my.ini 中 [mysqld] 下面加上 skip-grant-tables
2)也可以執行 mysqld -nt -skip-grant-tables
--skip-grant-tables 的意思是啟動MySQL服務的時候跳過權限表認證

 G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin>mysqld.exe -nt -skip-grant-tables
 G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64\bin>

6、啟動服務 ,執行net start mysql

  C:\Windows\system32>net start mysql
  MySQL 服務正在啟動 .
  MySQL 服務已經啟動成功。

常見問題:服務無法啟動
處理方法:任務管理器,結束mysqld進程,重新啟動mysql

 G:\Program Files (x86)\MySQL\mysql-5.7.17-winx64>net start mysql
 MySQL 服務正在啟動 ..
 MySQL 服務無法啟動。
 服務沒有報告任何錯誤。
 請鍵入 NET HELPMSG 3534 以獲得更多的幫助。

7、進入mysql修改密碼
因設置過免密登錄,輸入命令 mysql -uroot -p,彈出輸入密碼,直接敲回車鍵即可
輸入mysql+回車就可以進行數據庫相關方面的操作了

8、停止服務,執行stop start mysql

溫馨提示:恢復root密碼登錄

1)先修改下root密碼
mysql5.7數據庫下已經沒有password這個字段了,password字段改成了authentication_string

查看密碼字段:
輸入 desc user;
或輸入 select * from user;

use mysql;
//更新密碼,new_pass 
update user set authentication_string=password("new_pass") where user="root";    
flush privileges;     //刷新權限
quit;    //退出    (或 exit;)

2)再將配置文件中 my.iniskip-grant-tables注釋掉(前面加 #)
3)操作完后重新啟動mysql服務,便可以用之前設置好的密碼登錄了
(輸入命令 mysql -uroot -p,再輸入密碼,看到以下提示,那么恭喜你成功了)


如需登錄遠程數據庫,加參數-h


 C:\WINDOWS\system32>mysql -uroot -p
 Enter password: ******
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 4
 Server version: 5.7.17

 Copyright (c) 2000, 2016, 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>


常見問題

1、密碼失效的情況下如何修改密碼,報錯1820

1)、通過 alter user

mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.20 sec)

2)、通過 set password=password("123456");

(使用PASSWORD()函數,對密碼進行加密,此時不需要使用FLUSH PRIVILEGES)

2、Mysql 報錯1130

解決方法:需要授權(hostname改成 IP地址,newpasspwd 改成新密碼)
grant all on . to 'root'@'hostname' identified by 'newpasspwd'

工具連接MySQL 失敗.png

針對單項操作進行授權(如select、update等),如有很多數據庫,則都要進行設置一遍

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP    # 根據實際需要填寫
    -> ON database.*      # 指定數據庫
    -> TO 'username'@'localhost'          #指定用戶
    -> IDENTIFIED BY 'password';     #用戶密碼

以上命令會在mysql數據庫中的user表創建一條用戶信息記錄。

未授權create權限.png

3、密碼永久生效

alter user 'root'@'localhost' password expire never;

4、卸載軟件后如何刪除服務

sc delete mysql

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容