Mysql DBA 進階營運學習筆記-Mysql常用基礎命令實戰

來源:互聯網
上載者:User

標籤:lock   設定檔   creat   令行   copyright   iat   test   安全措施   預設   

7.1 單一實例mysql啟動和關閉方法

(1)常規方法啟動資料庫

1.啟動mysql服務命令

[[email protected] ~]# /etc/init.d/mysqld startStarting MySQL. SUCCESS!

2.查看mysql連接埠

[[email protected] ~]# ss -lnt|grep 3306LISTEN 0  50*:3306 *:* 

3.查看mysql進程

會啟動兩個進程第一個就是mysql_safe第二個是mysqld

[[email protected] ~]# ps -ef|grep mysql|grep -v greproot   2796  1  0 16:23 pts/000:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/var --pid-file=/usr/local/mysql/var/localhost.localdomain.pidmysql  2912   2796  0 16:23 pts/000:00:00 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql --log-error=/usr/local/mysql/var/localhost.localdomain.err --pid-file=/usr/local/mysql/var/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306 

4.MySQL資料庫啟動原理

/etc/init.d/mysqld 是一個shell啟動指令碼,啟動後最終會調用mysqld_safe指令碼,最後調用mysqld服務啟動mysql。

如下,/etc/init.d/mysqld指令碼中調用的mysqld_safe的程式。

$bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &

(2)初始化資料庫時MySQL系統輸出的給出的啟動方法

Mysqld_safe --user=mysql &

提示:

1.當找回root密碼時,會經常使用mysqld_safe –user=mysql &帶參數啟動

2.我們自己開發資料庫啟動指令碼時,會用到這個方法。

3./etc/init.d/mysqld和mysqld_safe --user=mysql的啟動實質一樣。一般出故障的時候我們用mysqld_safe來啟動,因為可以加參數。

[[email protected] ~]# cd /usr/local/mysql/|mysqld_safe &

(3)常規方法關閉資料庫

1.關閉MySQL命令

[[email protected] ~]# /etc/init.d/mysqld stopShutting down MySQL. SUCCESS![[email protected] ~]# ss -lnt|grep 3306

2.MySQL常規關閉資料庫原理

‘stop‘)# Stop daemon. We use a signal here to avoid having to know the# root password.# The RedHat / SuSE lock directory to removelock_dir=/var/lock/subsys/mysqlmanager# If the manager pid_file doesn‘t exist, try the server‘sif test ! -s "$pid_file"then  pid_file=$server_pid_file  lock_dir=/var/lock/subsys/mysqlfiif test -s "$pid_file"then  mysqlmanager_pid=`cat $pid_file`  if (kill -0 $mysqlmanager_pid 2>/dev/null)  thenecho $echo_n "Shutting down MySQL"kill $mysqlmanager_pid# mysqlmanager should remove the pid_file when it exits, so wait for it.wait_for_pid removed "$mysqlmanager_pid"; return_value=$?  elselog_failure_msg "MySQL manager or server process #$mysqlmanager_pid is not running!"rm $pid_file  fi

(4)強制關閉mysql

Killall mysqld

Pkill mysqld

Killall -9 mysqld

Kill pid

提示:第二種方法啟動和關閉資料庫一般生產環境不用,特別是關閉命令。

強調:盡量不要野蠻殺死mysql服務,生產高並發環境可能會引起資料丟失。

下面引用老男孩老師的博文,野蠻殺死資料庫導致的故障企業案例:

http://oldboy.blog.51cto.com/2561410/1431161

http://oldboy.blog.51cto.com/2561410/1431172

http://www.cnblogs.com/peida/archive/2012/12/20/2825837.html

(5) 優雅關閉資料庫方法

第一種mysqladmin方法

[[email protected] ~]# mysqladmin -uroot -p123456 shutdown

第二種方法內建的指令碼
/etc/init.d/mysqld stop

7.1.1多執行個體mysql啟動與關閉方法執行個體

啟動:

/data/3306/mysql start/data/3307/mysql start

關閉:

/data/3306/mysql stop/data/3306/mysql stop

多執行個體啟動指令碼實現啟動和關閉方法

啟動:

${cmdPath}/mysqld_safe --defaults-file=${myPath}/my.cnf --user=mysql --basedir=${softPath} --datadir=${myPath}/data &>/dev/null &

關閉:

mysqladmin -u"${my_user}" -p"${my_pass}" -S "$socketfile" shutdown

7.2 登入mysql方法

7.2.1單一實例mysql登入

① Mysql 剛裝完資料庫無需密碼登入,不要密碼。

② Mysql –uroot 剛裝完資料庫無需密碼登入,不要密碼。

③ Mysql –uroot –p 這是標準的命令列登入命令。

④ Mysql –uroot –p’123456’ 非指令碼一般不這樣用,密碼明文會泄露密碼可以掩飾history功能解決。

解決方案:

History –c清空記錄

History –d 刪除指定行

可以查看系統記錄,可以刪除

[[email protected] 3306]# cat /root/.bash_history

也可以查看mysql記錄,可以刪除

[[email protected] 3306]# cat /root/.mysql_history

登入後預設提示符是mysql>這個提示符可以修改,目的是為了區分測試環境和正式環境。工作中一定要先區分正式環境和測試環境。不管正式環境還是測試環境都要先備份在操作。
更改mysql資料登入提示符(瞭解的知識)方法如下:

1.命令列修改提示符

mysql> prompt \[email protected] \r:\m\s-> 這種改變是臨時的不生效的

2.設定檔修改提示符

在my.cnf設定檔中的[mysql]模組下添加如下內容,儲存後,無需重啟mysql,退出當前session重新登入即可,如果在設定檔中添加,可以用\避免轉義帶來的問題。

[mysql]no-auto-rehashprompt \\[email protected] \\r:\\m\\s->

7.2.2多執行個體mysql登入方法

多執行個體mysql本地登入

[[email protected] ~]# mysql -uroot -p -S /data/3306/mysql.sock[[email protected] ~]# mysql -uroot -p -S /data/3307/mysql.sock

提示:多執行個體通過mysql的-S 指定不同的sock檔案登入

注意:多執行個體遠端連線無需指定sock路徑

mysql -uroot -p -h 192.168.1.115 -P3306

7.3 善用mysql協助命令help

MySQL中的help命令和linux命令列的man是類似的,想要查看MySQL中的命令使用文法,就到需要用help,help後面接相關命令及命令組合即可。例如:help create.

[email protected] 09:4813->help

7.4 設定及修改mysql root使用者密碼

7.4.1 MySQL資料庫使用者安全性原則介紹

安裝完mysql資料庫後,預設的管理員root密碼為空白,很不安全,因此要設定一個密碼,在安裝MySQL單一實例後,我們已經做了一些安全措施,例如:

a. 為root設定密碼

b. 刪除無用的mysql庫內的使用者帳號

c. 刪除預設存在的test資料庫。

除了上面的方法,針對MySQL資料庫的使用者處理,我們還需要刪除root添加新的管理使用者。

(1)刪除所有mysql中的使用者,包括root超級使用者。

[email protected] 10:5249->delete from mysql.user;Query OK, 0 rows affected (0.00 sec)

(2)增加system並升級為超級管理員,及和root等價的使用者。

grant all privileges on *.* to ‘system‘@‘localhost‘ identified by ‘123456‘ with  grant option;

7.4.2 為管理員設定密碼

mysqladmin -u root password ‘zbf666‘ 沒有密碼的使用者佈建密碼。mysqladmin -u root -S /data/3307/mysql.sock password ‘123456‘ 多執行個體設定密碼。

7.4.2.1 命令列外修改管理員root密碼

mysqladmin -usystem -p123456 password ‘zbf666‘mysqladmin -usystem -p123456 password ‘zbf666‘ –S /data/3306/mysql.sock 適合多執行個體

7.4.2.2 Sql語句修改管理員密碼

[email protected] 11:4303->update mysql.user set password=password("wwn520") where user=‘system‘ and host=‘localhost‘;Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0[email protected] 11:4321->flush privileges;重新整理到資料檔案Query OK, 0 rows affected (0.00 sec)

提示:

1.必須指定where條件。

2.必須使用password()函數來加密更改密碼。

注意:如果是生產環境,應該起碼8為數字並且有字母數位混合。
這種方法可以使用—skip-grant-tables找回密碼。

7.4.2.3 第三個方法修改管理員密碼

很少用這種方法

[email protected] 11:4358->set password=password("zbf666");Query OK, 0 rows affected (0.00 sec)[email protected] 11:4845->flush privileges;Query OK, 0 rows affected (0.00 sec)

7.5 找回丟失的mysql root使用者密碼

7.5.1 啟動修改丟失的Mysql單一實例root密碼方法

  1. 首先停止資料庫

    [[email protected] ~]# /etc/init.d/mysqld stopShutting down MySQL. SUCCESS!
  2. 帶--skip-grant-tables啟動mysql

    [[email protected] ~]# mysqld_safe --skip-grant-tables --user=mysql &[[email protected] ~]#Mysql 登入時空密碼
  3. 無密碼即可登入mysql

    [[email protected] ~]# mysqlWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.1.72-log Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.[email protected] 12:3556->
  4. 修改root密碼為新密碼

    [email protected] 12:3838->set password=password("zbf666");ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement[email protected] 12:3913->update mysql.user set password=password("zbf666") where user=‘system‘ and host=‘l=‘localhost‘;Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0[email protected] 12:3936->flush privileges;Query OK, 0 rows affected (0.00 sec)
  5. 重啟服務在登入

    [[email protected] ~]# mysqladmin -usystem -pzbf666 shutdown;180118 00:42:53 mysqld_safe mysqld from pid file /usr/local/mysql/var/localhost.localdomain.pid ended[1]+  Donemysqld_safe --skip-grant-tables --user=mysql[[email protected] ~]# /etc/init.d/mysql start[[email protected] ~]# mysql -usystem -pzbf666Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.1.72-log Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.[email protected] 12:4329->

7.5.2 多執行個體MySQL啟動修改丟失root密碼

1.關閉mysql

Killall mysqld

2.啟動時加—skip-grant-tables參數,指定3306的設定檔

Mysql_safe –defaults-file=/data/3306/my.cnf –skip-grant-table &

3.登入資料庫

Mysql –u root –p –S /data/3306/mysql.sock 登入時空密碼

4.修改資料庫密碼

mysql> update mysql.user set password=password("zbf") where user=‘root‘;Query OK, 5 rows affected (0.00 sec)Rows matched: 5  Changed: 5  Warnings: 0mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)

4.重啟服務用新密碼登入

Killall mysqd

單一實例:/etc/init.d/mysqld restart

多執行個體:/data/3306/mysql restart

Mysql DBA 進階營運學習筆記-Mysql常用基礎命令實戰

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.