1. 首页 > MySQL教程 > 正文

MySQL教程FG162-MySQL程序通用使用方法

本文档介绍MySQL程序的通用使用方法,包括参数设置、环境变量配置和配置文件使用等内容,帮助读者掌握MySQL程序的基本操作技巧。风哥教程参考MySQL官方文档MySQL Programs等内容。

Part01-基础概念与理论知识

1.1 MySQL程序通用参数

MySQL程序通常支持一系列通用参数,这些参数可以控制程序的行为和功能。

# 查看MySQL程序通用参数
# mysql –help | head -50
mysql Ver 8.0.36 for Linux on x86_64 (MySQL Community Server – GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysql client
The following options may be given as the first argument:
–print-defaults Print the program argument list and exit.
–no-defaults Don’t read default options from any option file.
–defaults-file=# Only read default options from the given file #.
–defaults-extra-file=# Read this file after the global files are read.

-?, –help Display this help and exit.
-I, –help Synonym for -?
–auto-rehash Enable automatic rehashing. One doesn’t need to use
‘rehash’ to get table and field completion, but startup
and reconnecting may take longer. Disable with
–disable-auto-rehash. (Defaults to on; use –skip-auto-rehash
to disable.)
-A, –no-auto-rehash
No automatic rehashing. One has to use ‘rehash’ to get
table and field completion. This gives a quicker start of
mysql and disables rehashing on reconnect.
–auto-vertical-output
Automatically switch to vertical output mode if the
result is wider than the terminal width.
-B, –batch Don’t use history file. Disable interactive behavior.
(Enables –silent.)
–character-sets-dir=name
Directory for character set files.
–column-type-info Display column type information.
-c, –comments Preserve comments. Send comments to the server.
The default is –skip-comments (discard comments),
enable with –comments.
-C, –compress Use compression in server/client protocol.
-#, –debug[=#] This is a non-debug version. Catch this and exit.
–debug-check Check memory and open file usage at exit.
–debug-info Print some debug info at exit.
-D, –database=name Database to use.
–default-character-set=name
Set the default character set.
–delimiter=name Delimiter to be used.
-e, –execute=name Execute command and quit. (Disables –force and history file.)
-E, –vertical Print the output of a query (rows) vertically.
-f, –force Continue even if an SQL error occurs.
-G, –named-commands
Enable named commands. Named commands mean this program’s
internal commands; see mysql> help . When enabled, the
named commands can be used from any line of the query,
otherwise only from the first line, before an enter.
Disable with –disable-named-commands. This option is
disabled by default. (Use \c to cancel commands.)
-i, –ignore-spaces Ignore spaces after function names.
–init-command=name SQL Command to execute when connecting to MySQL server.
Will automatically be re-executed when reconnecting.
–local-infile[=0|1] Enable/disable LOAD DATA LOCAL INFILE.
-b, –no-beep Turn off beep on error.
-h, –host=name Connect to host.
-H, –html Produce HTML output.
-X, –xml Produce XML output.
–line-numbers Write line numbers for errors.
-L, –skip-line-numbers
Don’t write line numbers for errors.
-n, –unbuffered Flush buffer after each query.
–column-names Write column names in results. (Defaults to on)
-N, –skip-column-names
Don’t write column names in results.
-O, –set-variable=name
Change the value of a variable. Please note that this
option is deprecated; you can set variables directly with
–variable-name=value.
–show-warnings Show warnings after every statement.
-s, –silent Be more silent. Print results with a tab as separator,
each row on new line.
–ssl-mode=name SSL connection mode.
–ssl Deprecated. Use –ssl-mode instead.
–ssl-verify-server-cert
Deprecated. Use –ssl-mode=VERIFY_IDENTITY instead.
–ssl-ca=name CA file in PEM format.
–ssl-capath=name CA directory.
–ssl-cert=name X509 cert in PEM format.
–ssl-cipher=name SSL cipher to use.
–ssl-key=name X509 key in PEM format.
–ssl-crl=name Certificate revocation list.
–ssl-crlpath=name Certificate revocation list path.
–tls-version=name TLS version to use, permitted values are:
TLSv1, TLSv1.1, TLSv1.2, TLSv1.3
-t, –table Output in table format.
–tee=name Append everything into outfile.
-u, –user=name User for login if not current user.
-U, –safe-updates Only allow UPDATE and DELETE that uses keys.
-U, –i-am-a-dummy Synonym for option –safe-updates, -U.
-v, –verbose Write more. (-v -v -v gives the table output format)
-V, –version Output version information and exit.
-p, –password[=name]
Password to use when connecting to server.
If password is not given it’s solicited on the tty.
-P, –port=# Port number to use for connection or 0 for default to,
in order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
–protocol=name The protocol to use for connection (tcp, socket, pipe, memory).
-S, –socket=name The socket file to use for connection.
–server-public-key-path=name
File path to the server public RSA key in PEM format.
–get-server-public-key
Get server public key from server.
-w, –wait Wait and retry if connection is down.
–connect-timeout=# Number of seconds before connection timeout.
–max-allowed-packet=#
The maximum packet length to send to or receive from
server.
–net-buffer-length=#
The buffer size for TCP/IP and socket communication.
–select-limit=# Automatic limit for SELECT statements.
–max-join-size=# Automatic limit for rows in a join.
–secure-auth Refuse client connecting to server if it uses old
(pre-4.1.1) protocol. Deprecated. Always TRUE
–server-arg=name Send argument to the server.
–show-errors Show errors. (Defaults to on)
–default-auth=name Default authentication client-side plugin to use.
–binary-mode By default, ASCII \0 is disallowed and \r\n is converted to \n.
This option disables both features, and also disables
parsing of all client commands except \C and \c.
–connect-expired-password
Notify the server that this client is prepared to handle
expired password sandbox mode.
–plugin-dir=name Directory for client-side plugins.
–default-character-set=name
Set the default character set.
–histignore=name A colon-separated list of patterns to keep statements
from being logged into mysql history.
–reconnect If the connection is lost, automatically reconnect.
(Defaults to on)
–disable-reconnect
Disable auto-reconnect.
–enable-cleartext-plugin
Enable/disable the clear text authentication plugin.
–print-defaults Print the program argument list and exit.
–no-defaults Don’t read default options from any option file.
–defaults-file=# Only read default options from the given file #.
–defaults-extra-file=# Read this file after the global files are read.

1.2 MySQL程序环境变量

MySQL程序可以通过环境变量来配置其行为,这些环境变量可以影响程序的默认设置。

# 查看MySQL相关环境变量
# env | grep MYSQL
MYSQL_HOME=/usr
MYSQL_TCP_PORT=3306

# 设置MySQL环境变量
# export MYSQL_HOME=/usr
# export MYSQL_TCP_PORT=3306
# export MYSQL_PWD=password

# 验证环境变量
# echo $MYSQL_HOME
/usr
# echo $MYSQL_TCP_PORT
3306

1.3 MySQL程序配置文件

MySQL程序可以通过配置文件来设置其行为,配置文件通常位于特定的目录中。

# 查看MySQL配置文件位置
# mysql –help | grep my.cnf
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

# 查看配置文件内容
# cat /etc/my.cnf
[mysqld]
datadir=/mysql/data
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
socket=/var/lib/mysql/mysql.sock

Part02-生产环境规划与建议

2.1 MySQL程序使用安全规范

在生产环境中使用MySQL程序需要遵循以下安全规范:

  • 不要在命令行中直接指定密码
  • 使用适当的用户权限
  • 启用SSL加密连接
  • 定期更新MySQL程序版本
  • 限制程序的执行权限
# 安全连接MySQL服务器
# mysql -u root -p –ssl-mode=REQUIRED
Enter password:

# 避免在命令行中指定密码
# mysql -u root -p
Enter password:

# 正确设置MySQL程序权限
# chmod 755 /usr/bin/mysql
# chown root:root /usr/bin/mysql

2.2 MySQL程序性能优化建议

MySQL程序的性能优化可以提高数据库的运行效率,包括:

  • 优化程序的参数设置
  • 调整程序的资源分配
  • 使用适当的程序版本
  • 定期更新和维护程序

2.3 MySQL程序故障处理建议

MySQL程序的故障处理需要遵循以下建议:

  • 定期检查程序的运行状态
  • 备份重要的配置文件
  • 建立故障处理流程
  • 定期进行故障演练

Part03-生产环境项目实施方案

3.1 MySQL程序部署方案

在生产环境中部署MySQL程序需要考虑以下因素:

  • 选择合适的部署方式(二进制包、源码编译、容器等)
  • 配置程序的路径和环境变量
  • 设置程序的权限和安全性
  • 配置程序的参数和选项
# 安装MySQL程序
# yum install -y mysql-community-server mysql-community-client

# 启动MySQL服务
# systemctl start mysqld
# systemctl enable mysqld

# 检查MySQL服务状态
# systemctl status mysqld
● mysqld.service – MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-04-01 10:00:00 CST; 1h ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 1234 ExecStart=/usr/sbin/mysqld –daemonize –pid-file=/var/run/mysqld/mysqld.pid (code=exited, status=0/SUCCESS)
Main PID: 1235 (mysqld)
Status: “Server is operational”
CGroup: /system.slice/mysqld.service
└─1235 /usr/sbin/mysqld –daemonize –pid-file=/var/run/mysqld/mysqld.pid

3.2 MySQL程序监控方案

MySQL程序的监控可以及时发现和解决问题,包括:

  • 监控程序的运行状态
  • 监控程序的性能指标
  • 监控程序的错误日志
  • 设置程序的告警机制

3.3 MySQL程序维护方案

MySQL程序的维护需要制定详细的方案,包括:

  • 定期检查程序的运行状态
  • 定期清理程序的日志和临时文件
  • 定期备份程序的配置文件
  • 定期更新程序的版本

Part04-生产案例与实战讲解

4.1 MySQL程序通用参数使用案例

以下是MySQL程序通用参数的使用案例:

# 使用–host参数连接远程MySQL服务器
# mysql -u root -p –host=192.168.1.100
Enter password:

# 使用–port参数指定端口
# mysql -u root -p –port=3307
Enter password:

# 使用–database参数指定数据库
# mysql -u root -p –database=fgedudb
Enter password:

# 使用–execute参数执行SQL语句
# mysql -u root -p –execute=”SELECT version();”
fgedudb
+———–+
| version() |
+———–+
| 8.0.36 |
+———–+

4.2 MySQL程序环境变量配置案例

以下是MySQL程序环境变量配置的案例:

# 配置MySQL环境变量
# vi ~/.bashrc
export MYSQL_HOME=/usr
export MYSQL_TCP_PORT=3306
export PATH=$PATH:$MYSQL_HOME/bin

# 使环境变量生效
# source ~/.bashrc

# 验证环境变量
# echo $MYSQL_HOME
/usr
# echo $MYSQL_TCP_PORT
3306
# which mysql
/usr/bin/mysql

4.3 MySQL程序配置文件使用案例

以下是MySQL程序配置文件使用的案例:

# 配置MySQL客户端
# vi /etc/my.cnf
[client]
socket=/var/lib/mysql/mysql.sock
user=root
password=password

# 测试配置文件
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123
Server version: 8.0.36 MySQL Community Server – GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> exit
Bye

Part05-风哥经验总结与分享

5.1 MySQL程序使用最佳实践

根据多年的经验,以下是MySQL程序使用的最佳实践:

  • 使用最新稳定版本的MySQL程序
  • 定期更新和维护MySQL程序
  • 合理配置MySQL程序的参数
  • 使用适当的工具进行监控和管理
  • 制定完善的备份和恢复策略

5.2 MySQL程序常见问题与解决方案

以下是MySQL程序常见问题与解决方案:

  • 连接失败:检查网络连接、防火墙设置和MySQL服务状态
  • 权限错误:检查用户权限和密码设置
  • 性能问题:优化程序参数和数据库结构
  • 版本兼容性:确保程序版本与数据库版本匹配

5.3 MySQL程序版本兼容性注意事项

MySQL程序版本兼容性需要注意以下事项:

  • 确保程序版本与数据库版本匹配
  • 注意版本之间的功能差异
  • 在升级前进行充分的测试
  • 制定版本升级的回滚策略
风哥提示:MySQL程序的使用需要根据实际的业务需求和环境特点进行,不同的场景可能需要不同的配置和优化策略。

更多视频教程www.fgedu.net.cn

学习交流加群风哥微信: itpux-com

学习交流加群风哥QQ113257174

更多学习教程公众号风哥教程itpux_com

from MySQL:www.itpux.com

GF-MySQL数据库培训文档系列

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息