Yum Repositorymysql
sudo yum install mysql-community-server
APTlinux
sudo apt-get install mysql-server
systemctl:sql
systemctl {start|stop|restart|status} mysqld
或者 service:docker
service mysqld {start|stop|restart|status}
第一次開啓服務: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:
密碼要至少包含:
一個大寫字母
user分爲host爲localhost
與%
的用戶:
User@%
容許從全部的ip訪問. User@localhost
只容許從localhost訪問。(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 \
若是輸出數據的結果太寬,自動改爲垂直顯示,與sql語句以\G
做用相同。
--vertical
, -E
:強制垂直顯示。
--html
, -H
輸出爲HTML格式,以下:
--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
--database=db_name
, -D db_name
--wait
, -w
--skip-reconnect
.--execute=statement
, -e statement
--skip-line-numbers
.--safe-updates
. (Default value is 1,000,000.)--named-commands
, -G
能夠在mysql中使用命令,如:quit
與\q
能夠同時使用。
--skip-named-commands
:關閉長命令的使用。如可以使用\q
而quit
沒法使用
--no-beep
, -b
--quick
, -q
--verbose
, -v
-v -v -v
produces table output format even in batch mode.)--version
, -V
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
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
system commmand, \! command
tee [file_name], \T [file_name]
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
讀取文件中的SQ語句(經常使用於還原mysqldump備份的數據):
mysql db_name < text_file
若是文件中指定了數據庫(use db_name),能夠直接:
mysql < text_file
若是已經進入了mysql:
mysql> source file_name mysql> \. file_name