MySQL安裝以及客戶端的使用

MySQL安裝以及客戶端的操做

MySQL Client Mannualhtml

在linux上安裝

Yum Repositorymysql

sudo yum install mysql-community-server

APTlinux

sudo apt-get install mysql-server

安裝後開啓MySQL Server服務

systemctl:sql

systemctl {start|stop|restart|status} mysqld

或者 service:docker

service mysqld {start|stop|restart|status}

MySQL 服務初始化

第一次開啓服務:shell

  • 服務端初始化.數據庫

  • SSL證書與密鑰被生成,存放在數據文件夾中.bash

  • validate_password is installed and enabled.session

  • 超級用戶:'root'@'localhost 被建立. 密碼存放在日誌中:ide

    sudo grep 'temporary password' /var/log/mysqld.log

開啓服務後設置密碼:

mysql -uroot -p
ALTER USER 'root'@'%' IDENTIFIED BY 'MyNewPass';
  • Note:

    • 密碼要至少包含:

    • 一個大寫字母

      • 一個小寫字母
      • 一個數字
      • 一個特殊字符
      • 密碼長度不小於8
    • user分爲host爲localhost%的用戶:

      • User@% 容許從全部的ip訪問. User@localhost 只容許從localhost訪問。

使用docker部署MySQL

(1)使用docker安裝

  • 拉取mysql

    docker pull mysql:[tag]
  • 啓動鏡像,配置掛載卷:

    docker run -d -p 3306:3306 --name mysql \    
    -e MYSQL_ROOT_PASSWORD="assinscreed" \
    -e MYSQL_USER="root" \
    -e MYSQL_PASSWORD="assinscreed" \
    mysql:[tag]
    • 其中數據以及配置文件掛載:

      --mount type=bind,src=/path-on-host-machine/my.cnf,dst=/etc/my.cnf \
         --mount type=bind,src=/path-on-host-machine/datadir,dst=/var/lib/mysql \

MySQl客戶端

MySQL客戶端命令選項-官方文檔

經常使用的MySQL客戶端鏈接選項

輸出

--auto-vertical-output

  • 若是輸出數據的結果太寬,自動改爲垂直顯示,與sql語句以\G做用相同。

    image-20210221223009639

  • --vertical, -E:強制垂直顯示。

  • --html, -H

    • 輸出爲HTML格式,以下:

      image-20210221224757684

  • --xml, -X:輸出爲xml格式

鏈接選項

--user=user_name, -u user_name

  • 指定用戶名。

--host=host_name, -h host_name

  • 指定鏈接主機地址。

--password[=password], -p[password

  • 指定登陸密碼,注意的是密碼與選項之間不能有空格。

--port=port_num, -P port_num

  • 指定MySQL服務的TCP端口號

--database=db_name, -D db_name

  • 指定使用的數據庫

--connect-timeout=value

  • 鏈接超時秒數,默認爲0

--wait, -w

  • 若是鏈接不能創建,則會等待相應秒數重連。

--reconnect

  • 若是中途鏈接終端,則自動重連
  • To suppress reconnection behavior, use --skip-reconnect.

功能執行

--execute=statement, -e statement

  • 處理提供的statement 語句並推出mysql。

--init-command=str

  • 鏈接以後執行str。

--line-numbers

--max-allowed-packet=value

  • The maximum size of the buffer for client/server communication. The default is 16MB, the maximum is 1GB.

--max-join-size=value

  • The automatic limit for rows in a join when using --safe-updates. (Default value is 1,000,000.)

--named-commands, -G

  • 能夠在mysql中使用命令,如:quit\q能夠同時使用。

  • --skip-named-commands:關閉長命令的使用。如可以使用\qquit沒法使用

--no-beep, -b

  • 發生錯誤時,禁止發出聲音.

--print-defaults

  • Print the program name and all options that it gets from option files.

--quick, -q

  • Do not cache each query result, print each row as it is received. This may slow down the server if the output is suspended. With this option, mysql does not use the history file.

--verbose, -v

  • Verbose mode. Produce more output about what the program does. This option can be given multiple times to produce more and more output. (For example, -v -v -v produces table output format even in batch mode.)

--version, -V

  • 打印版本信息並退出。

MySQL客戶端命令

MySQL客戶端命令官方文檔

mysql> help

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given
               outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing
               binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.
query_attributes(\) Sets string parameters (name1 value1 name2 value2 ...)
for the next query to pick up.

For server side help, type 'help contents'

經常使用:

clear, \c
  • 清空當前的輸入,示例:

    mysql> select wrong
        -> \c
    mysql>
connect [db_name [host_name]], \r [db_name [host_name]]
  • 從新鏈接指定主機下的數據庫。
delimiter str, \d str
  • 指定SQL語句分界符號(終止符),默認爲`;
prompt [str], \R [str]
  • 修改提示符爲指定的字符串。
resetconnection, \x
  • 重建鏈接,並清除seesion,示例:

    mysql> SELECT LAST_INSERT_ID(3);
    +-------------------+
    | LAST_INSERT_ID(3) |
    +-------------------+
    |                 3 |
    +-------------------+
    
    mysql> SELECT LAST_INSERT_ID();
    +------------------+
    | LAST_INSERT_ID() |
    +------------------+
    |                3 |
    +------------------+
    
    mysql> resetconnection;
    
    mysql> SELECT LAST_INSERT_ID();
    +------------------+
    | LAST_INSERT_ID() |
    +------------------+
    |                0 |
    +------------------+
source file_name, \. file_name
  • 執行文件中的SQL語句。
system commmand, \! command
  • 經過linux系統的shell執行命令。
tee [file_name], \T [file_name]
  • 同時將輸出結果輸入到指定的文件中。

HELP命令的使用

mysql> help contents
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the
following categories:
   Account Management
   Administration
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Functions and Modifiers for Use with GROUP BY
   Geographic Features
   Language Structure
   Plugins
   Storage Engines
   Stored Routines
   Table Maintenance
   Transactions
   Triggers
  • 可使用%_,進行命令的匹配:

    mysql> HELP rep%
    Many help items for your request exist.
    To make a more specific request, please type 'help <item>',
    where <item> is one of the following
    topics:
       REPAIR TABLE
       REPEAT FUNCTION
       REPEAT LOOP
       REPLACE
       REPLACE FUNCTION

處理文本中的SQL語句

讀取文件中的SQ語句(經常使用於還原mysqldump備份的數據):

mysql db_name < text_file

若是文件中指定了數據庫(use db_name),能夠直接:

mysql < text_file

若是已經進入了mysql:

mysql> source file_name
mysql> \. file_name
相關文章
相關標籤/搜索