MySQL基本操作總結

來源:互聯網
上載者:User

標籤:mysql基礎命令   mysql基本操作   

一、mysql服務作業

 單一實例:

  (1)啟動與關閉mysql

     [[email protected] ~]$ /etc/init.d/mysql start

     [[email protected] ~]$ netstat -lnt | grep 3306

      tcp        0      0 :::3306           :::*          LISTEN     

     [[email protected] ~]$ /etc/init.d/mysql stop

   注意:/etc/init.d/mysql 由mysql-5.6.20.tar.gz解壓目錄/mysql-5.6.20/support-files/mysql.server.sh複製而來

 (2)安全模式啟動(生產環境不推薦使用)

    [[email protected] home]# /usr/local/mysql/bin/mysqld_safe --user=mysql &

    [[email protected] home]# 151207 12:16:22 mysqld_safe Logging to ‘/usr/local/data/mysql.err‘.

    151207 12:16:22 mysqld_safe Starting mysqld daemon with databases from /usr/local/data


    [[email protected] home]# netstat -tlnp | grep 3306

     tcp        0      0 :::3306                     :::*                        LISTEN      2317/mysqld   

    [[email protected] home]# killall mysqld  或者kill -9 mysqld 

多運行幾次,直到顯示:mysqld: no process killed   

  多執行個體:

  (1)啟動與關閉mysql

   /data/3306/mysql start

   /data/3307/mysql start

   

   /data/3306/mysql stop

   /data/3307/mysql stop

   需要自己寫啟動指令碼

二、登陸Mysql方法

   (1)單一實例 

     [[email protected] home]# mysql   剛安裝完成無密碼情況登陸方式

[[email protected] home]# mysql -uroot  剛安裝完成無密碼情況登陸方式

[[email protected] home]# mysql -uroot -p  標準dba登陸方式,避免密碼被看見

[[email protected] home]# mysql -uroot -p‘111111‘  非指令碼一般不這樣用,避免密碼被看見

mysql>   登陸後預設提示符號

   (2)多執行個體:

      [[email protected] run]# mysql -uroot -p -S /usr/local/data/mysql.sock

 [[email protected] run]# mysql -uroot -p -S /usr/local/data1/mysql.sock

 通過mysql -S命令指定不同的sock檔案登陸不同的服務中

   (3)登陸後的顯示資訊

    [[email protected] home]# mysql -uroot -p    登陸方式

    Enter password:                       密碼輸入

    Welcome to the MySQL monitor.  Commands end with ; or \g.  mysql命令以; 或 \g 結尾

    Your MySQL connection id is 4

    Server version: 5.6.20-log Source distribution    當前server版本


    Copyright (c) 2000, 2014, 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> 

    注意:上述資訊很有用,可以直觀瞭解mysql。

   (4)巧用Mysql的help

      如授權:mysql> help grant

 GRANT ALL ON db1.* TO ‘jeffrey‘@‘localhost‘ IDENTIFIED BY ‘mypass‘;

      mysql> GRANT ALL ON *.* TO ‘tangbo‘@‘localhost‘ IDENTIFIED BY ‘111111‘;

             Query OK, 0 rows affected (0.12 sec)

   (5)退出mysql

     mysql> quit 或 exit


三、修改mysql的root密碼

   (1)安裝mysql後,預設的root密碼為空白,很不安全,所以要設定密碼

      單一實例:[[email protected] ~]# mysqladmin -uroot password ‘111111‘

      Warning: Using a password on the command line interface can be insecure.

      警告:使用命令列介面的密碼方式不安全

      多執行個體:[[email protected] ~]# mysqladmin -uroot -p‘111111‘ password ‘111111‘ -S /usr/local/data/mysql.sock

   (2)修改root密碼方法一

       [[email protected] ~]# mysqladmin -uroot -p‘111111‘ password ‘tangbo‘

       [[email protected] ~]# mysqladmin -uroot -p‘111111‘ password ‘111111‘ -S /usr/local/data/mysql.sock

   (3)修改root密碼方法二

       mysql> update mysql.user set password=password ("tangbo") where user=‘root‘

       mysql> flush privileges;

   (4)修改root密碼方法三

       Mysql安裝完畢後,root預設口令為空白,需要馬上修改root密碼

       mysql> set password=password(‘tangbo‘);

       Query OK, 0 rows affected (0.02 sec)

       *******************生產環境,密碼必須複雜********************

四、找回丟失的mysql root 密碼

    單一實例啟動方法:

 killall mysqld 

 [[email protected] ~]# mysqld_safe --skip-grant-table &

 mysql -u root -p

 或者把它寫設定檔  my.cnf:   skip-grant-tables

  多執行個體啟動方法:

 killall mysqld 

  [[email protected] ~]# mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-table &

   mysql -uroot -p -S /data/3306/mysql.sock   登陸時空密碼


     


本文出自 “山貓” 部落格,謝絕轉載!

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.