如果有服務,使用下面命令刪除,管理員身份打開cmd :
net stop mysql
sc delete mysql
pause
1、下載 https://dev.mysql.com/downloads/mysql/5.7.html
沒有的Oracle帳號怕麻煩的可以在些下載:
鏈接:https://pan.baidu.com/s/1E46mKltT4yenF3Souk1OZg
提取碼:91et
2、解壓為 D:\SoftWare\mysql-5.7.28-winx64,新建一個my.ini文件內容如下
[mysqld] skip-grant-tables # 設置3306端口 port=3306 # 設置mysql的安裝目錄 basedir=D:\SoftWare\mysql-5.7.28-winx64\ # 切記此處一定要用雙斜杠\\,單斜杠我這里會出錯。 # 設置mysql數據庫的數據的存放目錄 datadir=D:\\SoftWare\\mysql-5.7.28-winx64\\Data # 此處同上 # 允許最大連接數 max_connections=200 # 允許連接失敗的次數。這是為了防止有人從該主機試圖攻擊數據庫系統 max_connect_errors=10 # 服務端使用的字符集默認為UTF8 character-set-server=utf8 # 創建新表時將使用的默認存儲引擎 default-storage-engine=INNODB # 默認使用“mysql_native_password”插件認證 default_authentication_plugin=mysql_native_password [mysql] # 設置mysql客戶端默認字符集 default-character-set=utf8 [client] # 設置mysql客戶端連接服務端時默認使用的端口 port=3306 default-character-set=utf8
3、以管理員身份運行CMD命令窗口,並執行相應操作
D:
cd D:\SoftWare\mysql-5.7.28-winx64\bin
mysqld --defaults-file=D:\SoftWare\mysql-5.7.28-winx64\my.ini --initialize --user=mysql --console
請把上圖上初始密碼記住!
4、安裝MySQL服務,以管理員身份運行cmd
mysqld --install MySQL --defaults-file=D:\SoftWare\mysql-5.7.28-winx64\my.ini
5、啟動mysql,兩種方式
#后台運行 net start mysql #前台運行 mysqld --defaults-file=D:\mysql\my.ini
6、首次連接需要修改root密碼
mysql -uroot -p mysql> set password=password("mysql"); mysql> flush privileges;
Mysql添加用戶與授權
1、本地環境
CentOS Linux release 7.5.1804 (Core) mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
2、以root用戶登錄Mysql
mysql -uroot -proot
3、切換到mysql數據庫
use mysql
4、添加用戶
//只允許指定ip連接
create user '新用戶名'@'localhost' identified by '密碼'; //允許所有ip連接(用通配符%表示) create user '新用戶名'@'%' identified by '密碼';
5、為新用戶授權
//基本格式如下
grant all privileges on 數據庫名.表名 to '新用戶名'@'指定ip' identified by '新用戶密碼' ; //示例 //允許訪問所有數據庫下的所有表 grant all privileges on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' ; //指定數據庫下的指定表 grant all privileges on test.test to '新用戶名'@'指定ip' identified by '新用戶密碼' ;
6、設置用戶操作權限
//設置用戶擁有所有權限也就是管理員
grant all privileges on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION; //擁有查詢權限 grant select on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION; //其它操作權限說明,select查詢 insert插入 delete刪除 update修改 //設置用戶擁有查詢插入的權限 grant select,insert on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION; //取消用戶查詢的查詢權限 REVOKE select ON what FROM '新用戶名';
7、刪除用戶
DROP USER username@localhost;
8、修改后刷新權限
FLUSH PRIVILEGES;
附:可能碰到的問題
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
解決方法:
mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> set password=password("mysql");(或 set password for 'root'@'localhost'=password('root');) Query OK, 0 rows affected, 1 warning (0.00 sec) flush privileges;
2、啟動時一直啟動不了,也停止不了
D:\SOFTWARE\mysql-5.7.28-winx64\bin\>mysqld --console
報下面錯:
Found option without preceding group in config file
原因:就是my.ini 文件格式為utf-8
解決:文件文件格式修改為ANSI