一:安裝mysql
前提條件:
在mysql官網下載,比如mysql-5.6.26-winx64.zip,解壓mysql-5.6.26-winx64,儘量不要去其他網站下載。
1、win10下,win+x鍵選擇“命令提示符(管理員)”。
(注意:一定要用管理員運行CMD進入DOS!)
2、在環境變量PATH中添加:
;D:\Program\MySql\mysql-5.6.26-winx64\bin
3、進入到mysql-5.6.26-winx64的bin目錄下
C:\WINDOWS\system32>d:
D:\>cd D:\Program\MySql\mysql-5.6.26-winx64
D:\Program\MySql\mysql-5.6.26-winx64>cd bin
4、輸入服務安裝命令
D:\Program\MySql\mysql-5.6.26-winx64\bin>mysqld install MySQL --defaults-file="D:\Program\MySql\mysql-5.6.26-winx64\my-default.ini"
Service successfully installed.
(下載的壓縮包裏如果沒有my-default.ini,那麼可以手工創建)
my-default.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.
[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 = .....
# datadir = .....
# port = .....
# 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 = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
5、啓動MYSQL服務
D:\Program\MySql\mysql-5.6.26-winx64\bin>net start mysql
MySQL 服務正在啓動 .
MySQL 服務已經啓動成功。
6、修改MYSQL密碼
D:\Program\MySql\mysql-5.6.26-winx64\bin>mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
Database changed
mysql> UPDATE user SET password=PASSWORD("root") WHERE user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> QUIT
Bye
7、測試密碼是否修改成功
D:\Program\MySql\mysql-5.6.26-winx64\bin>mysql -uroot -proot
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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.
二:創建用戶和數據庫並賦予權限
2.使用root管理員登錄mysql。
mysql -uroot -p123456;
3.創建新用戶
CREATE USER 'user'@'%' IDENTIFIED BY 'password';
注:'%' - 所有情況都能訪問
‘localhost’ - 本機才能訪問
’111.222.111.111‘ - 指定 ip 才能訪問
修改密碼
update mysql.user set password=password('新密碼') where user='user1';
4.新用戶登錄訪問,可以看到默認生成的兩個數據庫,看不到其他任何數據庫;
5.給用戶添加權限
grant all privileges on 想授權的數據庫.* to 'user'@'%';
all可以替換爲 select,delete,update,create,drop
此時訪問即可看到新添加的數據庫;
6.刪除用戶
Delete FROM mysql.user Where User='user1';
注意:刷新權限;在對用戶進行操作後,可能不及時生效,需進行權限刷新
flush privilege;