登录MySQL
# 所有参数是可选的。参数u为用户名,参数p为密码,参数h为主机,port为端口,dbname为数据库的名字。
# 直接“-p”不加密码,则会先提示输入密码再登录(这样密码是不可见的更安全)。如果密码中有特殊字符,按Shell的语法用单引号括起来即可。
mysql -u root -p mypassword -h myhost --port=3306 dbname
无密码登录(找回密码)
对于Ubuntu系统
注:以下均使用sudo权限
vi /etc/mysql/my.cnf # 编辑配置文件,在[mysqld]中加入一行“skip-grant-tables”
service mysql restart # 重启MySQL服务
mysql # 进入mysql的命令行
还可尝试直接运行
mysql -skip-grant-tables
MySQL命令行
登录进入MySQL命令行后,光标前会显示mysql>
MySQL命令行中可以直接执行SQL语句,注意需要用分号结束。
命令行中多数指令都不需要区分大小写。
用户信息
MySQL的用户名和密码保存在数据库mysql的user表中,而数据库本身保存在文件中。
进入MySQL,输入show variables like '%datadir%';
可以显示数据库保存的文件夹。
首次启动MySQL服务时,会自动创建用户数据库,通常会提示修改密码。默认root用户没有密码。
MySQL 5.7+用户相关设置
参考官方文档:《14.7.1 Account Management Statements》 http://dev.mysql.com/doc/refman/5.7/en/account-management-sql.html
创建用户
mysql> CREATE USER 'jzj'@'localhost' IDENTIFIED BY '[email protected]';
Query OK, 0 rows affected (0.00 sec)
查看当前用户
mysql> SELECT CURRENT_USER();
+----------------+
| current_user() |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)
设置密码
mysql> SET PASSWORD FOR 'jeffrey'@'localhost' = password_option;
设置当前用户密码
mysql> set password='[email protected]';
Query OK, 0 rows affected, 1 warning (0.00 sec)
MySQL早期版本密码设置(5.6及以前)
查看用户信息
mysql> select host,user,password from mysql.user;
修改密码可使用update命令(不推荐)
mysql> update mysql.user set password=password('new password') where user='root';
使用mysqladmin修改密码
~ mysqladmin -uroot -p'old password' password 'new password'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
最后,欢迎扫码关注微信公众号。程序员同行学习交流,聊天交友,国内外名企求职内推(微软 / 小冰 / Amazon / Shopee / Coupang / ATM / 头条 / 拼多多等),可加我微信 jzj2015 进技术群(备注进技术群,并简单自我介绍)。

本文由jzj1993原创,转载请注明来源:https://www.paincker.com/mysql-basic-usage
(标注了原文链接的文章除外)
暂无评论