mysql在shell登录并执行相关sql

前言

场景:通过脚本执行创建数据库,创建表等相关的语句,这时候就得不登录mysql就能直接执行相关的语句。mysql也提供了一系列的命令和参数。

https://dev.mysql.com/doc/

应用例子

列出所有的数据库

[root@master opt]# mysql -h 192.168.11.217 -P3306 -u root -p'root' -se 'show databases;';
mysql: [Warning] Using a password on the command line interface can be insecure.
Database
information_schema
helloworld
kylo
mysql
performance_schema
sys

列出数据库表

[root@master opt]# mysql -h 192.168.11.217 -P3306 -u root -p'root' -D mysql -se 'show tables;';
mysql: [Warning] Using a password on the command line interface can be insecure.
Tables_in_mysql
columns_priv
db
engine_cost
event
...[此处仅仅列出一部分]

相关参数简单解释

-h:后面跟随数据库所在的host或者IP地址

-P:表示端口号,mysql一般是3306

-u:表示username,一般是登录数据库的用户名

-p:表示password,登录数据库的密码

-D:后面跟着数据库的名称

mysql文档网址

https://dev.mysql.com/doc/

有空将对部分内容进行实验翻译...

坚壁清野

猜你喜欢

转载自www.cnblogs.com/hzhiping/p/10217210.html