• 基于Centos7.6

1.连接管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1>MySQL命令连接
#注意提前应该将用户授权做好
两种连接方式:
(1)TCP/IP方式:
格式: mysql -u用户名 -p -h 登录IP -P端口
#如果登录端口号为默认的3306可以不加-P3306,但使用-h必须配合-P,否则使用'mysql -u用户名 -P'格式是本地socket方式登录。
例子: mysql -uroot -p -h 10.0.0.50 -P3306
远程登录会在线程中显示为远程模式登录:
mysql> show processlist;
+----+------+---------------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+----------+------------------+
| 4 | test | 192.168.3.187:56216 | NULL | Query | 0 | starting | show processlist |
+----+------+---------------------+------+---------+------+----------+------------------+
1 row in set (0.00 sec)
#如果同时出现远程与socket方式登录,会优先使用远程登录。如:mysql -uroot -p -S /tmp/mysql.sock -h 10.0.0.50 -P3306
(2)Socket方式:
格式: mysql -u用户名 -p -S socket文件路径
例子: mysql -uroot -p -S /tmp/mysql.sock

2> 客户端工具连接
如SQLyog、Navicat等

2.内置功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
1>连接数据库
-u 用户
-p 密码
-h IP
-P 端口
-S socket文件
-e 免交互执行命令
< 导入SQL脚本

[root@db01 ~]# mysql -uroot -p -h 10.0.0.51 -P3306
Enter password:
mysql> select @@socket;
+-----------------+
| @@socket |
+-----------------+
| /tmp/mysql.sock |
[root@db01 ~]# mysql -uroot -p -S /tmp/mysql.sock
Enter password:
[root@db01 ~]# mysql -uroot -p -e "select user,host from mysql.user;"
Enter password:
+---------------+-----------+
| user | host |
+---------------+-----------+
| abc | 10.0.0.% |
| app | 10.0.0.% |
| root | 10.0.0.% |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
[root@db01 ~]#
[root@db01 ~]# mysql -uroot -p <world.sql
Enter password:
[root@db01 ~]#

2>内置命令:
help #打印mysql帮助文档
\c #结束上个命令运行
\q #退出mysql
\G #竖屏显示数据
source #恢复数据备份文件
#其他内置命令可以使用help查看
mysql> help

For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/

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 cha
rsets.warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.

For server side help, type 'help contents'

mysql>

MySQL—启动方式

1
2
3
以上多种方式,都可以单独启动MySQL服务
mysqld_safe和mysqld一般是在临时维护时使用。
另外,从Centos 7系统开始,支持systemd直接调用mysqld的方式进行启动数据库

3.初始化配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
1>作用
控制MySQL的启动
影响到客户端的连接

2>方法
预编译
**配置文件(所有启动方式)**
命令行参数 (仅限于 mysqld_safe mysqld)

3>初始配置文件
[root@db01 ~]# mysqld --help --verbose |grep my.cnf
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
注:
默认情况下,MySQL启动时,会依次读取以上配置文件,如果有重复选项,会以最后一个文件设置的为准。
但是,如果启动时加入了--defaults-file=xxxx时,以上的所有文件都不会读取.

4>配置文件书写方式
[标签]
配置项=xxxx

标签类型:服务端、客户端
服务器端标签:
[mysqld]
[mysqld_safe]
[server]

客户端标签:
[mysql]
[mysqldump]
[client]

配置文件的示例展示:
[root@db01 ~]# cat /etc/my.cnf
[mysqld] #服务器端配置
user=mysql #用户
basedir=/app/mysql #软件安装目录
datadir=/data/mysql #数据存放目录
socket=/tmp/mysql.sock #socket文件位置
server_id=6 #服务器ID
port=3306 #端口号
log_error=/data/mysql/mysql.log #日志文件位置
[mysql] #客户端配置
socket=/tmp/mysql.sock #socket文件位置
prompt=Master

5>配置文件读取顺序
[root@localhost ~]# mysql --help --verbose |grep my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
[root@localhost ~]#
#读取顺序/etc/my.cnf --》 /etc/mysql/my.cnf --》 /usr/local/mysql/etc/my.cnf --》 ~/.my.cnf

6>强制使用自定义配置文件启动数据库
使用参数:--defaults-file
使用方法:
pkill mysqld #关闭数据库
mysqld_safe --defaults-file=自定义配置文件路径 & #只会使用该配置文件,不会读取其他配置文件

7>强制使用自定义socket文件启动数据库
使用参数:-S
使用方法:
pkill mysqld
mysql -uroot -p -S socket文件路径
#例子:mysql -uroot -p -S /tmp/test.sock