mysql命令行工具,是在linux系統(tǒng)中用來管理登錄數(shù)據(jù)庫或執(zhí)行數(shù)據(jù)庫命令的的命令工具,其語法格式如下:
mysql [options] [databases]
常用選項:
-uUSERNAME:用戶名,默認為root;
-hHOSTNAME:遠程主機地址,默認為localhost;客戶端連接服務端,服務器會反解客戶端的Ip為主機名,關閉此功能(skip_name_resolve=on);
-p[PASSWORD]:USERNAME所表示的用戶的密碼,默認為空;
-P,--port=#:mysql服務器監(jiān)聽的端口,默認為3306;
-S,--socket=/PATH/TO/mysql.sock:套接字文件路徑;
-D,--database=DB_NAME ;連接到服務器端之后,設定此處指明的數(shù)據(jù)庫為默認數(shù)據(jù)庫;
-e,--execute='SQL STATEMENT':連接至服務器并讓其執(zhí)行此命令后直接返回;
注意:msql的用戶賬號由兩部分組成:'USERNAME'@'HOST';其中HOST用于限制此用戶可通過哪些遠程主機連接當前的mysql服務;
HOST的表示方式,支持使用通配符;
%:匹配任意長度的任意字符;如:172.16.%.% 172.16.0.0/16
_:匹配任意單個字符;如:tom@172.16.%.%
另外在搭建完成mysql服務器之后,需要對數(shù)據(jù)庫進行初始化的安全加固操作,如:
[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!
使用案例:
#遠程登錄數(shù)據(jù)庫
[root@localhost ~]# mysql -uwpuser -pmagedu -h 192.168.0.188
#遠程執(zhí)行數(shù)據(jù)庫命令而不進入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>