1.下載mysql
2.刪除MySql服務
使用管理員身份打開cmd,切換到mysql解壓的bin目錄,運行命令:
D:\MySql\bin>sc delete MySql
3.新建my.ini配置文件
在mysql程序的根目錄下,新建一個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
datadir = D:\MySQL\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
basedir和datadir根據具體mysql解壓的路徑而定
4.重新生成data文件
這個操作會刪除之前生成的data文件,如果有重要的數據表,請先備份好
回到cmd,重新生成data文件。運行:
D:\MySql\bin>mysqld --initialize-insecure --user=mysql
命令執行成功後mysql文件夾下會重新生成data文件夾
5.重新安裝mysql服務,同時綁定my.ini配置文件
安裝MySql服務,同時設置綁定my.ini配置文件。命令:
D:\MySql\bin>mysqld --install "MySql" --defaults-file="d:/mysql/my.ini"
6.啓動服務
cmd命令:
D:\MySql\bin>net start mysql
7.重新設置密碼
登錄mysql:
D:\MySql\bin>mysql -u root -p
第一次登錄不需要輸密碼,直接回車
修改root密碼:
update mysql.user set authentication_string=password("你的密碼") where user="root";
flush privileges
退出mysql
mysql> exit
重新使用新密碼登錄
8.後記-mysql修改用戶密碼的方法及命令
方法1: 用SET PASSWORD命令
首先登錄MySQL。
格式:mysql> set password for 用戶名@localhost = password('新密碼');
例子:mysql> set password for root@localhost = password('123');
方法2:用mysqladmin
格式:mysqladmin -u用戶名 -p舊密碼 password 新密碼
例子:mysqladmin -uroot -p123456 password 123
方法3:用UPDATE直接編輯user表
首先登錄MySQL。
mysql> use mysql;
mysql> update user set password=password('123') where user='root' and host='localhost';
mysql> flush privileges;
方法4:在忘記root密碼的時候,可以這樣
以windows爲例:
1. 關閉正在運行的MySQL服務。
2. 打開DOS窗口,轉到mysql\bin目錄。
3. 輸入mysqld --skip-grant-tables 回車。--skip-grant-tables 的意思是啓動MySQL服務的時候跳過權限表認證。
4. 再開一個DOS窗口(因爲剛纔那個DOS窗口已經不能動了),轉到mysql\bin目錄。
5. 輸入mysql回車,如果成功,將出現MySQL提示符 >。
6. 連接權限數據庫: use mysql; 。
6. 改密碼:update user set password=password("123") where user="root";(別忘了最後加分號) 。
7. 刷新權限(必須步驟):flush privileges; 。
8. 退出 quit。
9. 註銷系統,再進入,使用用戶名root和剛纔設置的新密碼123登錄。