mysql命令行工具,是在linux系统中用来管理登录数据库或执行数据库命令的的命令工具,其语法格式如下:
mysql [options] [databases]
常用选项:
-uUSERNAME:用户名,默认为root;
-hHOSTNAME:远程主机地址,默认为localhost;客户端连接服务端,服务器会反解客户端的Ip为主机名,关闭此功能(skip_name_resolve=on);
-p[PASSWORD]:USERNAME所表示的用户的密码,默认为空;
-P,--port=#:mysql服务器监听的端口,默认为3306;
-S,--socket=/PATH/TO/mysql.sock:套接字文件路径;
-D,--database=DB_NAME ;连接到服务器端之后,设定此处指明的数据库为默认数据库;
-e,--execute='SQL STATEMENT':连接至服务器并让其执行此命令后直接返回;
注意:msql的用户账号由两部分组成:'USERNAME'@'HOST';其中HOST用于限制此用户可通过哪些远程主机连接当前的mysql服务;
HOST的表示方式,支持使用通配符;
%:匹配任意长度的任意字符;如:172.16.%.% 172.16.0.0/16
_:匹配任意单个字符;如:tom@172.16.%.%
另外在搭建完成mysql服务器之后,需要对数据库进行初始化的安全加固操作,如:
[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y #是否设定mysql的rootpasswd,默认为空
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y #是否移除匿名用户
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y #是否禁止root远程登录
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y #是否删除testdatabase
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y #重读权限表
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
使用案例:
#远程登录数据库
[root@localhost ~]# mysql -uwpuser -pmagedu -h 192.168.0.188
#远程执行数据库命令而不进入mysql交互模式
[root@localhost ~]# mysql -uwpuser -pmagedu -h 192.168.0.188 -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| wordpress |
+--------------------+
mysql在5.6版本之后支持新特性:通过mysql_config_editor工具将登陆mysql服务的认证信息加密后保存在.mylogin.cnf文件中(默认为于用户主目录下)。随后mysql客户端工具可通过读取该加密文件连接到mysql,避免在命令行重复输入登陆信息,提供高了安全性。
#将相应的认证信息加密保存早.mylogin.cnf文件
[root@mysql ~]# mysql_config_editor set --login-path=mysqlauth --user=root --password
Enter password:
[root@mysql ~]# mysql_config_editor set --login-path=magedu --user=magedu --password
Enter password:
#查看保存的认证信息
[root@mysql ~]# mysql_config_editor print --all
[mysqlauth]
user = root
password = *****
[magedu]
user = magedu
password = *****
#使用mysql工具登陆时,指定要读取加载的认证用户信息
[root@mysql ~]# mysql --login-path=mysqlauth
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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>