1. 啟動與關(guān)閉MySQL
1.1 單實(shí)例MySQL啟動與關(guān)閉
- 正確啟動單實(shí)例MySQL數(shù)據(jù)庫
[root@oldboy mysql-5.6.41]# cp support-files/mysql.server /etc/init.d/mysqld
這條命令是把數(shù)據(jù)庫自帶的啟動腳本復(fù)制到/etc/init.d/目錄實(shí)現(xiàn)管理MySQL服務(wù)的啟動和停止。
啟動單實(shí)例數(shù)據(jù)庫的兩種方法:
(1)利用數(shù)據(jù)庫自帶的腳本啟動數(shù)據(jù)庫
[root@oldboy ~]# /etc/init.d/mysqld start
Starting MySQL.... SUCCESS!
(2)用初始化數(shù)據(jù)庫時MySQL系統(tǒng)給出的方法啟動
首先栽渴,停止前面已啟動的數(shù)據(jù)庫
[root@oldboy ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
然后蚌本,使用mysqld_safe啟動數(shù)據(jù)庫
[root@oldboy ~]# mysqld_safe --user=mysql &
[1] 1974
[root@oldboy ~]# 190204 17:52:46 mysqld_safe Logging to '/application/mysql/data/oldboy.err'.
190204 17:52:46 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data
啟動mysql不輸出提示:
[root@oldboy ~]# mysqld_safe --user=mysql >/dev/null 2>&1 &
[1] 2113
檢測數(shù)據(jù)庫是否已經(jīng)啟動:
[root@oldboy ~]# ss -lnt | grep 330
LISTEN 0 80 :::3306 :::*
[root@oldboy ~]# ps aux | grep mysql | grep -v grep
root 2113 0.0 0.1 106212 1556 pts/2 S 17:54 0:00 /bin/sh /application/mysql/bin/mysqld_safe --user=mysql
mysql 2207 0.4 44.9 1338832 451796 pts/2 Sl 17:54 0:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/application/mysql/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=oldboy.err --pid-file=oldboy.pid
- MySQL單實(shí)例服務(wù)啟動的原理
“/etc/init.d/mysqld”是MySQL自帶的使用Shell編寫的啟動腳本,執(zhí)行腳本后最終會調(diào)用mysqld_safe命令腳本履婉,mysqld_safe腳本執(zhí)行后又會調(diào)用mysqld主程序啟動MySQL服務(wù),因此在前文查看MySQL進(jìn)程時斟览,會發(fā)現(xiàn)不僅有mysqld_safe進(jìn)程毁腿,還有mysqld進(jìn)程
- MySQL單實(shí)例服務(wù)啟動小結(jié)
1)使用“/etc/init.d/mysqld start”命令啟動數(shù)據(jù)庫的本質(zhì)就相當(dāng)于執(zhí)行“mysqld_safe --user=mysql &”命令
2)在找回MySQL root密碼時,也會使用mysqld_safe程序苛茂,并且會認(rèn)帶忽略授權(quán)表的參數(shù)啟動來找回root密碼 - 正確關(guān)閉單實(shí)例MySQL數(shù)據(jù)庫
(1)使用數(shù)據(jù)庫自帶腳本關(guān)閉數(shù)據(jù)庫
無論是使用自帶腳本啟動數(shù)據(jù)庫已烤,還是使用mysqld_safe啟動數(shù)據(jù)庫,都可以采用自帶腳本關(guān)閉MySQL服務(wù)
[root@oldboy ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[1]+ Done mysqld_safe --user=mysql > /dev/null 2>&1
(2)使用mysqladmin管理命令關(guān)閉數(shù)據(jù)庫
[root@oldboy ~]# mysqladmin -uroot -poldboy123 shutdown
Warning: Using a password on the command line interface can be insecure. ---忽略
[root@oldboy ~]# netstat -antp | grep 330 ---關(guān)閉成功
- MySQL單實(shí)例服務(wù)關(guān)閉的基本原理
MySQL自帶腳本關(guān)閉數(shù)據(jù)庫的原理是通過kill pid的方式關(guān)閉數(shù)據(jù)庫 - 關(guān)閉MySQL數(shù)據(jù)庫方法小結(jié)
使用MySQL自帶的管理腳本
/etc/init.d/mysqld stop
使用mysqladmin命令關(guān)閉
mysqladmin -uroot -poldboy123 shutdown ---使用這個命令的最大障礙是必須知道密碼
1.2 多實(shí)例MySQL啟動與關(guān)閉方法示例
使用“/data/3306/mysql start”命令啟動妓羊,實(shí)質(zhì)上就是使用mysqld_safe加上不同的實(shí)例配置文件參數(shù)啟動
mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
使用mysqladmin命令關(guān)閉
mysqladmin -uroot -poldboy123 -S /data/3306/mysql.sock shutdown
2. MySQL連接原理方法及提示符設(shè)置
2.1 客戶端連接MySQL服務(wù)器原理結(jié)構(gòu)
- MySQL客戶端簡介
MySQL是一個典型的C/S服務(wù)結(jié)構(gòu)軟件胯究,作為運(yùn)維或DBA人員,我們經(jīng)常使用MySQL自帶的客戶端程序(在“/application/mysql/bin”目錄下)對其進(jìn)行管理躁绸,常用的管理命令有mysql裕循、mysqladmin、mysqldump净刮、mysqlbinlog剥哑,mysql是登錄MySQL最常用的客戶端程序。 -
客戶端連接MySQL服務(wù)器原理結(jié)構(gòu)
- MySQL連接方式介紹
MySQL的連接方式有TCP/IP和Socket連接方式庭瑰。TCP/IP連接方式一般是應(yīng)用(PHP/Python/Java程序)和數(shù)據(jù)庫服務(wù)不在一臺機(jī)器上的連接方案星持,對于mysql命令來說就是指定-h參數(shù)登錄,命令如下:
mysql -uroot -poldboy123 -h 10.0.0.52
本地連接數(shù)據(jù)庫常用的方式一般是Socket連接方式弹灭,特別是多實(shí)例本地MySQL登錄:
mysql -uroot -poldboy123 -S /tmp/mysql.sock
2.2 默認(rèn)單實(shí)例MySQL登錄方法
mysql ---剛裝完系統(tǒng)無密碼情況下的登錄方式
mysql -uroot ---無密碼情況下指定登錄用戶
mysql -uroot -p ---標(biāo)準(zhǔn)dba命令行登錄命令督暂,交互式輸入密碼可有效防止密碼泄露
mysql -uroot -poldboy123 ---這種登錄方式容易泄露密碼
防止MySQL密碼泄露的小妙招(MySQL安全策略)
1、通過環(huán)境變量來強(qiáng)制Linux不記錄敏感歷史命令
在命令行執(zhí)行“HISTCONTROL=ignorespace”后穷吮,再在輸入帶密碼的命令前面加一個空格登錄逻翁,登錄命令不會被記錄到歷史記錄里
[root@oldboy ~]# HISTCONTROL=ignorespace ---這是臨時生效,要想永久生效捡鱼,需要放入/etc/bashrc中
[root@oldboy ~]# mysql -uroot -poldboy123 ---命令開頭要多一個空格
2八回、操作完敏感的命令后及時刪除命令行記錄
[root@oldboy ~]# history | tail -4 ---顯示歷史記錄
113 history | tail -10
114 mysql -uroot -poldboy123
115 history -4
116 history | tail -4
[root@oldboy ~]# history -d 114 ---刪除序號為114的歷史記錄
[root@oldboy ~]# history | tail -5 ---序號114對應(yīng)的帶密碼的登錄命令已經(jīng)消失
113 history | tail -10
114 history -4
115 history | tail -4
116 history -d 114
117 history | tail -5
可執(zhí)行“history -c”清除所有記錄
[root@oldboy ~]# history -c
[root@oldboy ~]# history
1 history
也可執(zhí)行“> ~/.bash_history”清除歷史記錄文件
3、為帶密碼的啟動腳本以及備份腳本加700權(quán)限驾诈,用戶和組改為root
chmod 700 /data/3306/mysql ---如果采用kill信號來關(guān)閉數(shù)據(jù)庫缠诅,可不執(zhí)行
chmod 700 /server/scripts/bak.sh ---將密碼寫入my.cnf配置文件,使得執(zhí)行備份命令不需要加密碼
4乍迄、把密碼寫入my.cnf配置文件并加600權(quán)限管引,用戶和組改為mysql
[root@oldboy ~]# cp /application/mysql/my.cnf /etc/
[root@oldboy ~]# grep -A 2 client /etc/my.cnf
[client]
user = root ---注意user和password不能有大寫字母,不然識別不了
password = oldboy123
[root@oldboy ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
2.3 默認(rèn)多實(shí)例MySQL登錄方法
[root@oldboy ~]# mysql -uroot -p -S /data/3306/mysql.sock
[root@oldboy ~]# mysql -uroot -p -S /data/3307/mysql.sock
2.4 異地遠(yuǎn)程登錄MySQL方法
單實(shí)例異地遠(yuǎn)程登錄
mysql -uroot -p -h 127.0.0.1
多實(shí)例異地遠(yuǎn)程登錄
mysql -uroot -p -h 127.0.0.1 -P3306
mysql -uroot -p -h 127.0.0.1 -P3307
2.5 MySQL連接提示符說明
1闯两、MySQL提示符設(shè)置說明
為了區(qū)分日常的正式環(huán)境和測試環(huán)境褥伴,從而避免操作失誤,可以對提示符做一定的標(biāo)記性修改漾狼,并且可將其寫在配置里永久生效
(1)命令行修改登錄提示符
mysql> prompt \u@oldboy \r:\m:\s->
PROMPT set to '\u@oldboy \r:\m:\s->'
root@oldboy 09:00:34->
其中重慢,“\u@oldboy \r:\m:\s->”中的“\u”為登錄的數(shù)據(jù)庫用戶,“@”為分隔符逊躁,后面的oldboy為固定標(biāo)簽似踱,“\r:\m:\s”為時間信息,->為提示符標(biāo)識
(2)配置文件修改登錄提示符
在my.cnf配置文件的[mysql]模塊下添加如下內(nèi)容稽煤,重啟后屯援,無需重啟MySQL,退出當(dāng)前session念脯,重新登錄即可狞洋。如果是在my.cnf配置文件中添加的,可以使用“\”符號绿店,以避免轉(zhuǎn)義帶來的問題
[client]
prompt=\\u@oldboy \\r:\\m:\\s->
(3)多實(shí)例場景登錄提示符說明
在多實(shí)例場景下吉懊,要想使得提示符配置生效,不僅需要把參數(shù)放到my.cnf配置文件里假勿,還需要在連接MySQL時增加“--defaults-extra-file”參數(shù)指定修改的配置文件
可通過如下命令修改MySQL多實(shí)例配置文件借嗽,增加一行配置:
[root@oldboy ~]# head -4 /data/3307/my.cnf
[client]
port = 3307
user = root
password = oldboy123
prompt = \\u@oldboy \\r:\\m:\\s->
增加密碼參數(shù)后,要注意配置文件權(quán)限
重新登錄转培,可以發(fā)現(xiàn)增加了一個特殊的指定配置文件的參數(shù):
[root@oldboy ~]# mysql --defaults-extra-file=/data/3307/my.cnf -S /data/3307/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.41-log Source distribution
Copyright (c) 2000, 2018, 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.
root@oldboy 02:18:30->quit
2.6 退出數(shù)據(jù)庫
quit
exit
使用快捷鍵ctrl+c或ctrl+d
操作命令如下:
root@oldboy 02:20:47->quit
3. 設(shè)置及修改mysql root用戶密碼
3.1 MySQL數(shù)據(jù)庫用戶安全策略介紹
安裝完MySQL數(shù)據(jù)庫之后恶导,默認(rèn)的管理員root密碼為空(mysql5.7以前),這很不安全浸须。因此惨寿,需要為root用戶設(shè)置一個密碼邦泄,還可以做一些安全措施:
- 數(shù)據(jù)庫不設(shè)置外網(wǎng)IP
- 為root用戶設(shè)置比較復(fù)雜的密碼
- 刪除無用的mysql庫內(nèi)的用戶賬號,只保留root@localhost以及root@127.0.0.1
- 刪除默認(rèn)的test數(shù)據(jù)庫
- 增加用戶的時候裂垦,授權(quán)的權(quán)限應(yīng)盡量最小顺囊,允許訪問的主機(jī)范圍最小化
- 登錄命令行操作不攜帶密碼,而是回車后輸入密碼
以下采用更安全的措施來刪除root蕉拢,添加新的管理員用戶
1)刪除所有mysql中的用戶特碳,包括root超級用戶:
mysql> delete from mysql.user;
Query OK, 2 rows affected (0.14 sec)
這里的root可以保留,修改為其他用戶也可以
2)增加system并將該管理員用戶提升為超級管理員晕换,即與root等價的管理員用戶午乓,只是名字不同而已:
mysql> grant all privileges on *.* to system@'localhost' identified by 'oldboy123' with grant option;
Query OK, 0 rows affected (0.03 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)
此外,對于帶密碼的文件或腳本權(quán)限闸准,最好是文件用600益愈,腳本用700,用戶和組則用root或mysql
3.2 為管理員root用戶設(shè)置及修改密碼
1恕汇、為root用戶設(shè)置密碼
剛安裝完MySQL時是沒有密碼的腕唧,此時可以使用下面的命令為MySQL設(shè)置密碼
mysqladmin -uroot password 'oldboy123' ---適合單實(shí)例
mysqladmin -uroot password 'oldboy123' -S /data/3306/mysql.sock ---適合多實(shí)例
提示:命令實(shí)在Linux命令行執(zhí)行的,而不是在mysql命令行
2瘾英、為root用戶修改密碼的方法一:Linux命令行修改法
在Linux命令行下修改密碼適合于已知密碼的場合
mysqladmin -uroot -poldboy123 password 'oldboy' ---原密碼為oldboy123枣接,新密碼為oldboy
mysqladmin -uroot -poldboy password 'oldboy123' -S /data/3306/mysql.sock ---適合多實(shí)例方式
提示:練習(xí)完把密碼修改回oldboy123,方面后面練習(xí)
3缺谴、為root用戶修改密碼的方法二:SQL語句修改法
在MySQL命令行下修改密碼常用于遺忘了密碼的情況但惶,或者給不熟悉Linux命令行管理的人員使用
mysql> UPDATE mysql.user SET passowrd=PASSWORD("oldboy123") WHERE user='root' and host='localhost';
Query OK, 1 rows affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges; ---刷新權(quán)限使得修改密碼生效
Query OK, 0 rows affected (0.04 sec)
此方法更適合密碼丟失后,通過“--skip-grant-tables”參數(shù)啟動數(shù)據(jù)庫湿蛔,再對密碼進(jìn)行修改的情況
4膀曾、為root用戶修改密碼的方法三:SQL語句修改法
mysql> set password=password('oldboy');
Query OK, 1 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)
此方法有很大的局限性:
1)僅為修改當(dāng)前用戶民嗎
2)不適合通過“--skip-grant-tables”方式啟動后修改密碼
4. 找回MySQL root用戶密碼
4.1 找回MySQL單實(shí)例root用戶密碼的方法
首先停止MySQL服務(wù)
[root@oldboy ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
然后,使用mysqld_safe附帶的“--skip-grant-tables”(忽略授權(quán)登錄驗(yàn)證)啟動MySQL服務(wù)
[root@oldboy ~]# mysqld_safe --skip-grant-tables --user=mysql > /dev/null 2>&1 &
[1] 2897
[root@oldboy ~]# ss -antp | grep 330
LISTEN 0 80 :::3306 :::* users:(("mysqld",3006,10))
現(xiàn)在阳啥,無需密碼即可登錄MySQL
[root@oldboy ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.41 Source distribution
Copyright (c) 2000, 2018, 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>
可以將root密碼修改為新密碼了
mysql> set password=password('oldboy123'); ---此方法無效
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> update mysql.user set password=PASSWORD('oldboy123') where user="system" and host="localhost"; ---正確方法
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
重啟MySQL服務(wù)
[root@oldboy ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[1]+ Done mysqld_safe --skip-grant-tables --user=mysql > /dev/null 2>&1
[root@oldboy ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
測試登錄
[root@oldboy ~]# mysql -usystem -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.41 Source distribution
Copyright (c) 2000, 2018, 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> ---登錄成功
注意:加“--skip-grant-tables”參數(shù)登錄后添谊,修改完密碼一定要重啟,然后再對外提供服務(wù)察迟。如果發(fā)現(xiàn)重啟后使用MySQL不加密碼依然可以登錄斩狱,請查看是不是配置文件設(shè)置了密碼。此外扎瓶,在MySQL命令行使用的密碼會覆蓋my.cnf配置文件中配置的密碼
4.2 找回MySQL多實(shí)例root用戶的密碼方法
1所踊、關(guān)閉多實(shí)例3307MySQL服務(wù)
2、啟動數(shù)據(jù)庫時加“--skip-grant-tables”概荷,注意秕岛,該參數(shù)要放到結(jié)尾
[root@oldboy ~]# mysqld_safe --defaults-file=/data/3307/my.cnf --skip-grant-tables >/dev/null 2>&1 &
[1] 3608
[root@oldboy ~]# ss -antp | grep 330
LISTEN 0 80 :::3307 :::* users:(("mysqld",3795,11))
3、使用登錄命令登錄
[root@oldboy ~]# mysql -S /data/3307/mysql.sock
4、修改密碼
mysql> update mysql.user set password=PASSWORD('oldboy123') where user="root" and host="localhost";
Query OK, 0 rows affected (0.14 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
重啟MySQL服務(wù)继薛,使用新密碼登錄
[root@oldboy ~]# /data/3307/mysql stop
Stopping MySQL...
[root@oldboy ~]# /data/3307/mysql start
Starting MySQL...
[root@oldboy ~]# ss -antp | grep 330 ---重啟成功
LISTEN 0 80 :::3307 :::* users:(("mysqld",4236,11))
[root@oldboy ~]# mysql -S /data/3307/mysql.sock ---空密碼登錄失敗
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@oldboy ~]# mysql -uroot -poldboy123 -S /data/3307/mysql.sock
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.41-log Source distribution
Copyright (c) 2000, 2018, 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>