1.檢查mysql進程和端口是否正常
[root@station253 mysql]# ps aux | grep mysqld
[root@station253 mysql]# ps -ef | grep mysqld
[root@station253 mysql]# netstat -nutlp | grep mysqld
[root@station253 mysql]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2756 mysql 11u IPv4 33999 0t0 TCP *:mysql (LISTEN)node
2.數據目錄
[root@station253 mysql]# ll -h /mydata/data
total 30M
-rw-rw---- 1 mysql mysql 18M Dec 14 19:23 ibdata1
-rw-rw---- 1 mysql mysql 5.0M Dec 14 19:23 ib_logfile0
-rw-rw---- 1 mysql mysql 5.0M Dec 14 19:15 ib_logfile1
drwx------ 2 mysql mysql 16K Dec 14 17:39 lost+found
drwx------ 2 mysql root 4.0K Dec 14 19:22 mysql
-rw-rw---- 1 mysql mysql 107 Dec 14 19:15 mysql-bin.000001
-rw-rw---- 1 mysql mysql 28K Dec 14 19:22 mysql-bin.000002
-rw-rw---- 1 mysql mysql 1.1M Dec 14 19:22 mysql-bin.000003
-rw-rw---- 1 mysql mysql 107 Dec 14 19:23 mysql-bin.000004
-rw-rw---- 1 mysql mysql 76 Dec 14 19:23 mysql-bin.index
drwx------ 2 mysql mysql 4.0K Dec 14 19:22 performance_schema
-rw-r----- 1 mysql root 4.1K Dec 14 19:23 station253.example.com.err
-rw-rw---- 1 mysql mysql 5 Dec 14 19:23 station253.example.com.pid
drwx------ 2 mysql root 4.0K Dec 14 19:22 test
#data目錄下每一個目錄一般被識別爲數據庫,例如:mysql,test
#每新建一個數據庫,就自動新建一個目錄
#ibdata1,innode數據庫,ib_logfile日誌
#mysql-bin.0000x二進制日誌,用於實現mysql時間點恢復的重要文件。基於磁盤損壞時還能恢復數據考慮,不要將數據庫文件MYD和二進制日誌mysql-bin.0000x放在同一磁盤上。
#主機名station253.example.com.err文件錯誤日誌記錄本機mysql啓動運行中止過程當中出現的錯誤信息。
[root@station253 mysql]# tail -1 /mydata/data/station253.example.com.err
#檢查最後的狀態和錯誤。
Version: '5.5.33-log' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
#源碼安裝mysql.sock位置,rpm安裝的默認位置/var/lib/mysql/mysql.sockmysql
3.空密碼本地登陸
[root@station253 mysql]# mysql
mysql> use mysql;
Database changed
mysql> SHOW TABLES;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
...........................sql
mysql> SELECT HOST,USER,PASSWORD FROM user;
+------------------------+------+----------+
| HOST | USER | PASSWORD |
+------------------------+------+----------+
| localhost | root | |
| station253.example.com | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| station253.example.com | | |
+------------------------+------+----------+
6 rows in set (0.00 sec)數據庫
4.mysql的認證機制
mysql的驗證用戶權限不是根據用戶名,而是根據用戶名+主機名肯定的,某個用戶經本身的主機名進入系統纔會有對應權限。所以其中並無遠程主機名,固然沒法遠程鏈接。默認狀況即便4個root@主機名+2個匿名@主機名的用戶都是空密碼都是禁止遠程鏈接。
空密碼十分危險,給root用戶加密碼
生產環境下要首先刪除後3個,前3個留一個加密碼,刪除無用2個
::1 root #IPv6 地址
localhost
station253.example.comwindows
#初始化設置密碼,不指明主機,默認localhost
[root@station253 ~]# mysqladmin -uroot password 'redhat'
#第二次設置新密碼,須要老密碼驗證
[root@station253 ~]# mysqladmin -uroot password '123456' -p
Enter password: redhat
#有密碼時鏈接mysql
[root@station253 ~]# mysql -uroot -p
Enter password: 123456
mysql> use mysql;
Database changed
mysql> SELECT HOST,USER,PASSWORD from user;
+----------------------+------+-------------------------------------------+
| HOST | USER | PASSWORD |
+----------------------+------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |socket
爲用戶root@127.0.0.1,建立密碼
[root@station253 ~]# mysqladmin -uroot -h127.0.0.1 password 'redhat'
mysqladmin: connect to server at '127.0.0.1' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
修改root@127.0.0.1的密碼被root@localhost用戶阻止,-p 提供密碼
[root@station253 ~]# mysqladmin -uroot -h127.0.0.1 password 'redhat' -p
Enter password: 123456
#修改的是localhost的密碼
[root@station253 ~]# mysql -uroot -p
Enter password: redhat
mysql> use mysql;
Database changed
mysql> SELECT HOST,USER,PASSWORD from user; #表名嚴格區分大小寫
+----------------------+------+-------------------------------------------+
| HOST | USER | PASSWORD |
+----------------------+------+-------------------------------------------+
| localhost | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
但反解析127.0.0.1是指向localhost的,更改的仍是root@localhost,root@127.0.0.1仍舊爲空密碼ide
第二種方式爲用戶root@127.0.0.1,建立密碼
mysql> SET PASSWORD FOR'root'@'127.0.0.1'=PASSWORD('redhat');
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT HOST,USER,PASSWORD from user;
+----------------------+------+-------------------------------------------+
| HOST | USER | PASSWORD |
+----------------------+------+-------------------------------------------+
| localhost | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| station253.example.com | root | |
| 127.0.0.1 | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
密碼不是MD5加密,只是普通crypt函數加密,所以明文同樣,密文也同樣。函數
第三種方式
mysql> UPDATE user SET PASSWORD = PASSWORD('redhat') WHERE USER='root' and HOST='station253.example.com';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0ui
mysql> SELECT HOST,USER,PASSWORD from user;
+----------------------+------+-------------------------------------------+
| HOST | USER | PASSWORD |
+----------------------+------+-------------------------------------------+
| localhost | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| station253.example.com | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| 127.0.0.1 | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |加密
三個系統默認用戶添加密碼完成,刪除匿名用戶。
mysql> DROP USER''@localhost; #刪除用戶名爲空
Query OK, 0 rows affected (0.00 sec)
mysql> delete from user where password=''; #刪除密碼爲空
Query OK, 2 rows affected (0.00 sec)
mysql> SELECT HOST,USER,PASSWORD from user where password='';
Empty set (0.00 sec)
mysql> SELECT HOST,USER,PASSWORD from user;
+----------------------+------+-------------------------------------------+
| HOST | USER | PASSWORD |
+----------------------+------+-------------------------------------------+
| localhost | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| station253.example.com | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| 127.0.0.1 | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+----------------------+------+-------------------------------------------+
3 rows in set (0.00 sec)
重讀受權表生效。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit;
Bye
Windows遠程客戶端鏈接MySQL Server
但windows遠程客戶端仍舊不能遠程鏈接,受權,但生產環境千萬不要受權root用戶遠程登陸。
[root@station253 ~]# mysql -uroot -predhat
mysql> update user set host='192.168.1.%' where host='127.0.0.1';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> grant all privileges on *.* to root@'192.168.1.%' identified by 'redhat';
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user,password from user;
+----------------------+------+-------------------------------------------+
| host | user | password |
+----------------------+------+-------------------------------------------+
| localhost | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| station253.example.com | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| 192.168.1.% | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+----------------------+------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
windows外接進來能夠了。
mysql只須要容許本網段主機可訪問數據庫便可。
#########################################################
SQL的模式匹配容許你使用「_」匹配任何單個字符,而「%」匹配任意數目字符(包括零個字符)。