当前位置:Gxlcms > 数据库问题 > mysql设置更改root密码、连接mysql、常用命令

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

时间:2021-07-01 10:21:17 帮助过:4人阅读

设置、更改root用户密码

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

  1. <code>[root@localhost ~]# export PATH=$PATH:/usr/local/mysql/bin/mysql</code>

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

  1. <code>[root@localhost ~]# vim /etc/profile
  2. ......
  3. export PATH=$PATH:/usr/local/mysql/bin/
  4. 刷新配置:
  5. [root@localhost ~]# source /etc/profile</code>
  • 设置密码

    首次登陆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就不需要密码了。

  1. <code>登录mysql:
  2. [root@localhost ~]# mysql -uroot
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. ......
  5. mysql>
  6. // 不使用-p参数直接登录。
  7. 切换到mysql库:
  8. mysql> use mysql; // 切换到mysql库
  9. Reading table information for completion of table and column names
  10. You can turn off this feature to get a quicker startup with -A
  11. Database changed
  12. mysql> select * from user\G;
  13. // 查看用户的表信息,该表中存放的是用户相关信息(密码、授权…)
  14. // G选项的作用是使输出信息有序显示,不加该选项,显示内容会很乱
  15. mysql> select password from user;
  16. // 查看用户密码,显示结果Wie加密字符串!
  17. 重置密码:
  18. mysql> update user set password=password(‘112233‘) where user=‘root‘;
  19. Query OK, 4 rows affected (0.11 sec)
  20. Rows matched: 4 Changed: 4 Warnings: 0
  21. // 将密码更改为‘112233’
  22. 恢复配置文件:
  23. [root@localhost ~]# vim /etc/my.cnf
  24. // 将之前加入skip-grant那行注释掉
  25. 重启mysql服务:
  26. [root@localhost ~]# /etc/init.d/mysqld restart
  27. Shutting down MySQL.. SUCCESS!
  28. Starting MySQL. SUCCESS!
  29. 登录:
  30. [root@localhost ~]# mysql -uroot -p‘112233‘
  31. Warning: Using a password on the command line interface can be insecure.
  32. Welcome to the MySQL monitor. Commands end with ; or \g.
  33. ......
  34. mysql> </code>

重置密码步骤: 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连接一样。

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

    1. <code>[root@localhost ~]# mysql -uroot -p‘112233‘ -e "show databases"</code>

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

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

Mysql 常用命令

  1. <code>查看有哪些数据库:
  2. mysql> show databases;
  3. +--------------------+
  4. | Database |
  5. +--------------------+
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. | test |
  10. +--------------------+
  11. 4 rows in set (0.00 sec)
  12. 切换到mysql库:
  13. mysql> use mysql
  14. Reading table information for completion of table and column names
  15. You can turn off this feature to get a quicker startup with -A
  16. Database changed
  17. mysql>
  18. 查看库里的表:
  19. mysql> show tables;
  20. +---------------------------+
  21. | Tables_in_mysql |
  22. +---------------------------+
  23. | columns_priv |
  24. | db |
  25. | event |
  26. | func |
  27. | general_log |
  28. | help_category |
  29. | help_keyword |
  30. | help_relation |
  31. | help_topic |
  32. | innodb_index_stats |
  33. | innodb_table_stats |
  34. | ndb_binlog_index |
  35. | plugin |
  36. | proc |
  37. | procs_priv |
  38. | proxies_priv |
  39. | servers |
  40. | slave_master_info |
  41. | slave_relay_log_info |
  42. | slave_worker_info |
  43. | slow_log |
  44. | tables_priv |
  45. | time_zone |
  46. | time_zone_leap_second |
  47. | time_zone_name |
  48. | time_zone_transition |
  49. | time_zone_transition_type |
  50. | user |
  51. +---------------------------+
  52. 28 rows in set (0.00 sec)
  53. 查看表里面的字段:
  54. mysql> desc user;
  55. +------------------------+-----------------------------------+------+-----+-----------------------+-------+
  56. | Field | Type | Null | Key | Default | Extra |
  57. +------------------------+-----------------------------------+------+-----+-----------------------+-------+
  58. | Host | char(60) | NO | PRI | | |
  59. | User | char(16) | NO | PRI | | |
  60. | Password | char(41) | NO | | | |
  61. | Select_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  62. | Insert_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  63. ......
  64. +------------------------+-----------------------------------+------+-----+-----------------------+-------+
  65. 43 rows in set (0.00 sec)
  66. 查看表是怎么创建的
  67. mysql> show create table user\G;
  68. *************************** 1. row ***************************
  69. Table: user
  70. Create Table: CREATE TABLE `user` (
  71. `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
  72. `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
  73. `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT ‘‘,
  74. `Select_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  75. `Insert_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  76. `Update_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  77. `Delete_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  78. ......
  79. // \G 是让结果竖排显示;
  80. 查看当前登录的用户:
  81. mysql> select user();
  82. +----------------+
  83. | user() |
  84. +----------------+
  85. | root@localhost |
  86. +----------------+
  87. 1 row in set (0.00 sec)
  88. 查看当前所在的库:
  89. mysql> select database();
  90. +------------+
  91. | database() |
  92. +------------+
  93. | mysql |
  94. +------------+
  95. 1 row in set (0.00 sec)
  96. 创建库:
  97. mysql> create database db1;
  98. Query OK, 1 row affected (0.00 sec)
  99. mysql> show databases;
  100. +--------------------+
  101. | Database |
  102. +--------------------+
  103. | information_schema |
  104. | db1 |
  105. | mysql |
  106. | performance_schema |
  107. | test |
  108. +--------------------+
  109. 5 rows in set (0.00 sec)
  110. mysql> use db1 //切换到db1库
  111. Database changed
  112. 创建表:
  113. mysql> use db1;
  114. // 先切换到指定库下
  115. Database changed
  116. mysql> create table t1(`id` int(4),`name` char(40));
  117. // 括号中是定义字段及字段格式,使用反引号引起来
  118. Query OK, 0 rows affected (1.51 sec)
  119. // drop table t1,可以删除表。
  120. 查看当前数据库的版本:
  121. mysql> select version();
  122. +-----------+
  123. | version() |
  124. +-----------+
  125. | 5.6.35 |
  126. +-----------+
  127. 1 row in set (0.00 sec)
  128. // 数据库版本:5.6.35
  129. 查看数据库状态:
  130. mysql> show status;
  131. +-----------------------------------------------+-------------+
  132. | Variable_name | Value |
  133. +-----------------------------------------------+-------------+
  134. | Aborted_clients | 0 |
  135. | Aborted_connects | 0 |
  136. +-----------------------------------------------+-------------+
  137. 查看所有参数:
  138. mysql> show variables\G; //查看所有参数
  139. 查看指定参数
  140. mysql> show variables like ‘max_connect%‘\G;
  141. // like表示匹配;%是通配符
  142. 更改参数:
  143. mysql> set global max_connect_errors=110;
  144. Query OK, 0 rows affected (0.04 sec)
  145. #在此只是临时更改,如果要永久更改,需要编辑配置文件/etc/my.cnf
  146. 查看队列:
  147. mysql> show processlist;
  148. +----+------+-----------+------+---------+------+-------+------------------+
  149. | Id | User | Host | db | Command | Time | State | Info |
  150. +----+------+-----------+------+---------+------+-------+------------------+
  151. | 5 | root | localhost | db1 | Query | 0 | init | show processlist |
  152. +----+------+-----------+------+---------+------+-------+------------------+
  153. 1 row in set (0.01 sec)
  154. 完整显示:
  155. mysql> show full processlist;
  156. +----+------+-----------+------+---------+------+-------+-----------------------+
  157. | Id | User | Host | db | Command | Time | State | Info |
  158. +----+------+-----------+------+---------+------+-------+-----------------------+
  159. | 6 | root | localhost | db1 | Query | 0 | init | show full processlist |
  160. +----+------+-----------+------+---------+------+-------+-----------------------+
  161. 1 row in set (0.00 sec)</code>

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

可以使用 ctrl+l 清屏

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

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

标签:创建   names   操作   stat   用户   format   success   mes   col   

人气教程排行