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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

设置、更改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


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
存储 关系型数据库 MySQL
MySQL 忘记root密码解决方案
【7月更文挑战第19天】
|
23天前
|
关系型数据库 MySQL 数据安全/隐私保护
windows mysql8 安装后 提示密码不对,修改下密码认证方式就可以了
windows mysql8 安装后 提示密码不对,修改下密码认证方式就可以了
60 3
|
2天前
|
SQL 关系型数据库 MySQL
mysql性能调优:EXPLAIN命令21
【7月更文挑战第21天】掌握SQL性能调优:深入解析EXPLAIN命令的神奇用法!
14 1
|
10天前
|
SQL 关系型数据库 MySQL
实时计算 Flink版产品使用问题之要将MySQL同步到Doris,并设置整库同步,只变更库名、表名和表结构都不变,该如何设置
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
21天前
|
关系型数据库 MySQL Linux
Linux部署实战前言,MySQL在CentOS安装【单机软件】,MySQL的安装需要root权限,yum install mysql,systemctl enable mysqld开机自启的意思
Linux部署实战前言,MySQL在CentOS安装【单机软件】,MySQL的安装需要root权限,yum install mysql,systemctl enable mysqld开机自启的意思
|
21天前
|
Ubuntu 关系型数据库 MySQL
MySQL5.7在Ubuntu安装[单机软件],第一步登录root,sudo su -
MySQL5.7在Ubuntu安装[单机软件],第一步登录root,sudo su -
|
22天前
|
XML 关系型数据库 MySQL
支付系统----微信支付19---集成MyBatis-plus,数据库驱动对应的依赖版本设置问题,5没版本没有cj这个依赖,mysql驱动默认的是版本8,这里是一个父类,数据库都有,写个父类,继承就行
支付系统----微信支付19---集成MyBatis-plus,数据库驱动对应的依赖版本设置问题,5没版本没有cj这个依赖,mysql驱动默认的是版本8,这里是一个父类,数据库都有,写个父类,继承就行
|
24天前
|
关系型数据库 MySQL 数据库
mysql 中登录报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)ERROR
mysql 中登录报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)ERROR
|
24天前
|
关系型数据库 MySQL 开发工具
MySQL中忘记用户密码怎么办?
MySQL中忘记用户密码怎么办?
|
21天前
|
存储 关系型数据库 MySQL
探索MySQL:关系型数据库的基石
MySQL,作为全球最流行的开源关系型数据库管理系统(RDBMS)之一,广泛应用于各种Web应用、企业级应用和数据仓库中