1、 MySQL初始密碼html
新安裝的MySQL默認是沒有密碼的,設置初始密碼能夠用如下命 java
mysqladmin -u root password 'new-password' mysqladmin -u root password 'password' -S /data/3306/mysql.sock #socket多實例
設置初始密碼後,可在當前用戶家目錄下建立.my.cnf文件,配置好用戶名和密碼後,該用戶能夠免密碼登陸,要注意設置文件權限,不要讓其餘用戶看到密碼信息mysql
vi ~/.my.cnf [client] user=root host=localhost password=cathy
MySQL服務器在讀取配置文件的時候是按照如下順序讀取的:sql
/etc/my.cnf --> /etc/mysql/my.cnf --> $MYSQL_HOME/my.cnf --> --default-extra-file=/path/to/somefile--> ~/.my.cnf數據庫
若是不一樣配置文件有相同配置選項則以最後一個配置文件爲準安全
2、修改root密碼服務器
方法一:命令行網絡
mysqladmin -u root -p oldpassword password 'password' #單實例 mysqladmin -u root -p oldpassword password 'password'-S /data/3306/mysql.sock#多實例
方法二:登陸mysqlapp
mysql> update mysql.user set Password=password("storm") where User="root" mysql> flush privileges
此方法有風險,必須指定條件和password函數, 不然有可能會其餘用戶的密碼一併改掉
方法三:socket
myslq> set password=password(456) # 修改當前登陸用戶密碼 set password for 'storm'@'%' =password(456) # 修改指定用戶密碼 myslq> flush privileges
3、重置忘記的密碼
1. 中止mysqld服務
2. 啓動mysqld,忽略受權表
--skip-grant-tables表示忽略受權表啓動MySQL
--skip-networking表示不連入網絡,防止無密碼啓動mysql帶來安全隱患
3. 登陸mysql修改密碼
mysql mysql> update mysql.user set Password=password("newpassword") where User="root" mysql> flush privileges
注意:二進制包安裝MySQL,在執行mysqld_safe --skip-grant-tables命令時會出現以下錯誤:
[root@dev ~]# mysqld_safe 160419 20:32:08 mysqld_safe Logging to '/usr/local/mysql/data/dev.err'. touch: cannot touch `/usr/local/mysql/data/dev.err': No such file or directory chmod: cannot access `/usr/local/mysql/data/dev.err': No such file or directory touch: cannot touch `/usr/local/mysql/data/dev.err': No such file or directory chown: cannot access `/usr/local/mysql/data/dev.err': No such file or directory 160419 20:32:08 mysqld_safe The file /usr/local/mysql/bin/mysqld does not exist or is not executable. Please cd to the mysql installation directory and restart this script from there as follows: ./bin/mysqld_safe& See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information /application/mysql/bin/mysqld_safe: line 128: /usr/local/mysql/data/dev.err: No such file or directory
錯誤緣由暫時未找到,可是仔細看報錯信息:Please cd to the mysql installation directory and restart this script from there as follows: ./bin/mysqld_safe&
意思是要進入MySQL的安裝目錄再執行mysqld_safe命令,我嘗試進入我安裝的MySQL目錄:/application/mysql/bin/而後執行mysqld_safe依然報錯,最後發現只有在MySQL的安裝目錄下執行./bin/mydqld_safe才能正常執行,具體緣由還有待查找!
4. 重啓mysql
/etc/init.d/mysqld stop
/etc/init.d/mysqld start
若修改完密碼後沒法關閉,用mysqladmin -uroot -pnewpassword shutdown關閉
多實例MySQL啓動修改丟失的root密碼
mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables& mysql -uroot -p -S /data/3306/mysql.sock # 登陸時用空密碼
4、建立用戶及受權
方法一:先建立用戶再受權
mysql> create user 'storm'@'localhost' IDENTIFIED BY 'mypass'; mysql> grant all on db1.* to 'jeffrey'@'localhost';
方法二:直接受權給用戶,用戶不存在時則建立
mysql> grant all on dbname.* to 'jeffrey'@'localhost' identified by 'password'
1. 用戶名格式
MySQL建立用戶時,用戶名的格式爲:'用戶名'@'主機' ,其中用戶名和主機名的引號可用單引號,也可用雙引號,可是不可省略。主機權限範圍例舉以下:
‘storm'@'%'
#表示全部主機都可使用storm帳戶登陸
mysql ‘storm'@'192.168.0.%
#表示只容許192.168.0網段的用戶鏈接,這種狀況即便本地鏈接mysql,也須要指定-h IP的方式鏈接
mysql‘storm'@'localhost’
#表示只容許本地鏈接
2. MySQL用戶權限
MySQL可受權的權限有:SELECT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS,FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES,LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOWVIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, all表示全部權限。
爲用戶受權時,可精確到數據庫、數據庫所對應的表、表裏的某個字段的具體權限,如:
grant all on *.* to ‘storm'@'%'
#爲storm授予全部庫的全部操做權限
grant all on school.* to ‘storm'@'%'
#爲storm授予school數據庫下的全部表的全部操做權限
grant select, insert,update on school.class to ‘storm'@'%'
#爲storm授予school數據庫下class表的 select, insert,update權限
grant update(age) on on school.class to ‘storm'@'%'
#表示storm用戶只能update的字段爲age
3. 查看用戶權限
mysql> show grants for storm@'%';
4. 撤回用戶權限
REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...