一、 MySQL初始密碼
新安裝的MySQL默認是沒有密碼的,設置初始密碼可以用以下命
mysqladmin -u root password 'new-password' mysqladmin -u root password 'password' -S /data/3306/mysql.sock #socket多實例
設置初始密碼后,可在當前用戶家目錄下創建.my.cnf文件,配置好用戶名和密碼后,該用戶可以免密碼登錄,要注意設置文件權限,不要讓其他用戶看到密碼信息
vi ~/.my.cnf [client] user=root host=localhost password=cathy
MySQL服務器在讀取配置文件的時候是按照以下順序讀取的:
/etc/my.cnf --> /etc/mysql/my.cnf --> $MYSQL_HOME/my.cnf --> --default-extra-file=/path/to/somefile--> ~/.my.cnf
如果不同配置文件有相同配置選項則以最后一個配置文件為准
二、修改root密碼
方法一:命令行
mysqladmin -u root -p oldpassword password 'password' #單實例 mysqladmin -u root -p oldpassword password 'password'-S /data/3306/mysql.sock#多實例
方法二:登錄mysql
mysql> update mysql.user set Password=password("storm") where User="root" mysql> flush privileges
此方法有風險,必須指定條件和password函數, 否則有可能會其他用戶的密碼一並改掉
方法三:
myslq> set password=password(456) # 修改當前登錄用戶密碼 set password for 'storm'@'%' =password(456) # 修改指定用戶密碼 myslq> flush privileges
三、重置忘記的密碼
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 # 登錄時用空密碼
四、創建用戶及授權
方法一:先創建用戶再授權
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] ...