mysql设置更改root密码、连接mysql、常用命令

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
简介:

设置、更改root用户密码

首次使用mysql会提示‘该命令不在’,原因是还没有将该命令加入环境变量,如果要使用该命令,需要使用其绝对路径:/usr/local/mysql/bin/mysql,为了方便,先将其加入系统环境变量。

[root@localhost ~]# export PATH=$PATH:/usr/local/mysql/bin/mysql

重启系统后该变量会失效,若要永久生效,需要将其加入环境变量配置文件:

[root@localhost ~]# vim /etc/profile
......
export PATH=$PATH:/usr/local/mysql/bin/

刷新配置:
[root@localhost ~]# source /etc/profile
  • 设置密码

    首次登陆mysql,root用户没有密码,直接登陆

    [root@localhost ~]# mysql -uroot
    //-u指定用户登录
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    mysql>quit
    Bye
    // quit命令可以退出mysql。

    设置密码:
    [root@localhost ~]# mysqladmin -uroot password '123456'
    Warning: Using a password on the command line interface can be insecure.
    // 这里并没有报错,只是提示说密码在命令行显示出来了不×××全。

    不使用密码登录:
    [root@localhost ~]# mysql -uroot
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    // 提示登录被拒绝,需要密码。

    使用密码登录:
    [root@localhost ~]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    // -p参数,使用密码登录。可以将密码接在-p后。也可以不在-p后输入密码,根据后面的提示信息输入,这个方法不会暴露用户密码,更安全。

注意: 在没设置root密码时使用-p参数登录mysql,会提示输入密码,这是直接回车就行。

  • 更改密码

    当知道用户密码时,进行密码更改:
    [root@localhost ~]# mysqladmin -uroot -p'123456' password '654321'
    Warning: Using a password on the command line interface can be insecure.
    // 警告密码在命令行输入,不安全。但是密码已经修改成功!

    使用旧密码登录:
    [root@localhost ~]# mysql -uroot -p123456
    Warning: Using a password on the command line interface can be insecure.
    // 警告密码在命令行输入,不安全。
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    // 提示登录信息验证失败,密码错误!

    使用新密码登录
    [root@localhost ~]# mysql -uroot -p654321
    Warning: Using a password on the command line interface can be insecure.
    // 警告密码在命令行输入,不安全。
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    mysql> 
    // 使用新密码登录成功!

  • 密码重置

    在不记得root密码时使用,重置密码。

    编辑配置文件:
    [root@localhost ~]# vim /etc/my.cnf
    [mysqld]
    skip-grant // 忽略授权!
    ......
    // 在mysqld模块下加入代码:skip-grant

    重启mysql服务:
    [root@localhost ~]# /etc/init.d/mysqld restart
    Shutting down MySQL.. SUCCESS! 
    Starting MySQL.. SUCCESS!

注意: 完成上面操作之后登录mysql就不需要密码了。

登录mysql:
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
......
mysql>
// 不使用-p参数直接登录。

切换到mysql库:
mysql> use mysql; // 切换到mysql库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from user\G;
// 查看用户的表信息,该表中存放的是用户相关信息(密码、授权…)
// G选项的作用是使输出信息有序显示,不加该选项,显示内容会很乱  
mysql> select password from user;
// 查看用户密码,显示结果Wie加密字符串! 

重置密码:
mysql> update user set password=password('112233') where user='root';
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4  Changed: 4  Warnings: 0
// 将密码更改为‘112233’

恢复配置文件:
[root@localhost ~]# vim /etc/my.cnf
// 将之前加入skip-grant那行注释掉

重启mysql服务:
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

登录:
[root@localhost ~]# mysql -uroot -p'112233'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
......
mysql> 

重置密码步骤: vim /etc/my.cnf-->添加skip-grant-->mysql restart-->登录-->use mysql-->update user set password=...-->vim /etc/my.cnf-->删除skip-grant-->mysql restart。

连接mysql

  • 远程连接

    使用IP和端口号连接

    [root@localhost ~]# mysql -uroot -p'112233' -h127.0.0.1 -P3306
    Warning: Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    mysql>

    // -h=host,指定IP,-P=port,指定端口号

  • 本地连接

    直接可以直接连接或使用socket连接。

    使用socket链接:
    [root@localhost ~]# mysql -uroot -p'112233' -S/tmp/mysql.sock
    Warning: Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    mysql>

    // -S=socket,指定socket。此方法只适用于本地连接。和直接mysql连接一样。

  • 连接数据后显示所有数据库

    [root@localhost ~]# mysql -uroot -p'112233' -e "show databases"

    Warning: Using a password on the command line interface can be insecure.
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | mysql |
    | performance_schema |
    | test |
    +--------------------+

    -e 参数后可以跟一条mysql语句。
    // 该方法常用于shell脚本中。

Mysql 常用命令

查看有哪些数据库:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

切换到mysql库:
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 

查看库里的表:
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

查看表里面的字段:
mysql> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(16)                          | NO   | PRI |                       |       |
| Password               | char(41)                          | NO   |     |                       |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N                     |       |
 ......
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
43 rows in set (0.00 sec)

查看表是怎么创建的
mysql> show create table user\G;
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
  `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
  `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
  `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  ......

  // \G 是让结果竖排显示;

查看当前登录的用户:
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

查看当前所在的库:
mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

创建库:
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use db1 //切换到db1库
Database changed

创建表:
mysql> use db1;  
// 先切换到指定库下
Database changed
mysql> create table t1(`id` int(4),`name` char(40));
// 括号中是定义字段及字段格式,使用反引号引起来
Query OK, 0 rows affected (1.51 sec)
// drop table t1,可以删除表。

查看当前数据库的版本:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.00 sec)
// 数据库版本:5.6.35

查看数据库状态:
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name                                 | Value       |
+-----------------------------------------------+-------------+
| Aborted_clients                               | 0           |
| Aborted_connects                              | 0           |
+-----------------------------------------------+-------------+

查看所有参数:
mysql> show variables\G;  //查看所有参数

查看指定参数
mysql> show variables like 'max_connect%'\G;
// like表示匹配;%是通配符

更改参数:
mysql> set global max_connect_errors=110;
Query OK, 0 rows affected (0.04 sec)
#在此只是临时更改,如果要永久更改,需要编辑配置文件/etc/my.cnf

查看队列:
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  5 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.01 sec)

完整显示:
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host      | db   | Command | Time | State | Info                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
|  6 | root | localhost | db1  | Query   |    0 | init  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

在mysql中 drop 后跟库或者表名,可以删除库或者表。

可以使用 ctrl+l 清屏

mysql的历史命令在.mysql_history 文件中。

本文转自 豆渣锅 51CTO博客,原文链接:http://blog.51cto.com/754599082/2060315


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
4月前
|
SQL Java 关系型数据库
Java连接MySQL数据库环境设置指南
请注意,在实际部署时应该避免将敏感信息(如用户名和密码)硬编码在源码文件里面;应该使用配置文件或者环境变量等更为安全可靠地方式管理这些信息。此外,在处理大量数据时考虑使用PreparedStatement而不是Statement可以提高性能并防止SQL注入攻击;同时也要注意正确处理异常情况,并且确保所有打开过得资源都被正确关闭释放掉以防止内存泄漏等问题发生。
197 13
|
4月前
|
SQL 关系型数据库 MySQL
MySQL数据库连接过多(Too many connections)错误处理策略
综上所述,“Too many connections”错误处理策略涉及从具体参数配置到代码层面再到系统与架构设计全方位考量与改进。每项措施都需根据具体环境进行定制化调整,并且在执行任何变更前建议先行测试评估可能带来影响。
1339 11
|
4月前
|
SQL 关系型数据库 MySQL
排除通过IP访问MySQL时出现的连接错误问题
以上步骤涵盖了大多数遇到远程连接 MySQL 数据库时出现故障情形下所需采取措施,在执行每个步骤后都应该重新尝试建立链接以验证是否已经解决问题,在多数情形下按照以上顺序执行将能够有效地排除并修复大多数基本链接相关故障。
406 3
|
4月前
|
SQL 监控 关系型数据库
查寻MySQL或SQL Server的连接数,并配置超时时间和最大连接量
以上步骤提供了直观、实用且易于理解且执行的指导方针来监管和优化数据库服务器配置。务必记得,在做任何重要变更前备份相关配置文件,并确保理解每个参数对系统性能可能产生影响后再做出调节。
525 11
|
5月前
|
存储 关系型数据库 MySQL
修复.net Framework4.x连接MYSQL时遇到utf8mb3字符集不支持错误方案。
通过上述步骤大多数情况下能够解决由于UTF-encoding相关错误所带来影响,在实施过程当中要注意备份重要信息以防止意外发生造成无法挽回损失,并且逐一排查确认具体原因以采取针对性措施解除障碍。
341 12
|
4月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
304 88
|
4月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
4月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
918 152
|
5月前
|
存储 运维 关系型数据库
从MySQL到云数据库,数据库迁移真的有必要吗?
本文探讨了企业在业务增长背景下,是否应从 MySQL 迁移至云数据库的决策问题。分析了 MySQL 的优势与瓶颈,对比了云数据库在存储计算分离、自动化运维、多负载支持等方面的优势,并提出判断迁移必要性的五个关键问题及实施路径,帮助企业理性决策并落地迁移方案。
|
4月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(上)
最终建议:当前系统是完美的读密集型负载模型,优化重点应放在减少行读取量和提高数据定位效率。通过索引优化、分区策略和内存缓存,预期可降低30%的CPU负载,同时保持100%的缓冲池命中率。建议每百万次查询后刷新统计信息以持续优化
379 91

推荐镜像

更多