MySQL安装以及客户端的操作
在linux上安装
Yum Repository
sudo yum install mysql-community-server
APT
sudo apt-get install mysql-server
安装后开启MySQL Server服务
systemctl:
systemctl {start|stop|restart|status} mysqld
或者 service:
service mysqld {start|stop|restart|status}
MySQL 服务初始化
第一次开启服务:
-
服务端初始化.
-
SSL证书与密钥被生成,存放在数据文件夹中.
-
validate_password
is installed and enabled. -
超级用户:
'root'@'localhost
被创建. 密码存放在日志中: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客户端连接选项
输出
-
如果输出数据的结果太宽,自动改成垂直显示,与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
- 指定MySQL服务的TCP端口号
--database=db_name
, -D db_name
- 指定使用的数据库
- 连接超时秒数,默认为0
--wait
, -w
- 如果连接不能建立,则会等待相应秒数重连。
- 如果中途连接终端,则自动重连
- To suppress reconnection behavior, use
--skip-reconnect
.
功能执行
--execute=statement
, -e statement
- 处理提供的statement 语句并推出mysql。
- 连接之后执行str。
- Write line numbers for errors. Disable this with
--skip-line-numbers
.
- The maximum size of the buffer for client/server communication. The default is 16MB, the maximum is 1GB.
- 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
:关闭长命令的使用。如可使用q
而quit
无法使用
--no-beep
, -b
- 发生错误时,禁止发出声音.
- 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> 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 (
) 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 (
) Disable pager, print to stdout.
notee ( ) 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]],
[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