mysql常用操作 密码,常用命令,用户管理

来源:互联网 发布:淘宝网店供应商 编辑:程序博客网 时间:2024/05/17 23:01

13.1-13.4 密码,常用命令,用户管理

首次创建和更改root密码

首次进入mysql不需要密码。

/usr/local/mysql/bin/mysql -uroot  //登录,首次不需要密码ctrl+c或者exit可以退出

但是我们可以把mysql的启动添加到环境变量中,这样使用起来更方便。

vim /etc/profile添加以下语句:PATH=$PATH:/usr/local/mysql/bin/mysqlsource /etc/profile //让配置文件生效

现在就可以直接使用mysql命令登录了:
mysql -uroot

首次创建密码
mysqladmin -uroot password ‘123123’

登录
mysql -uroot -p123123

更改root密码
方法1:
mysqladmin -uroot -p password ‘111111’
输入原密码

验证
mysql -uroot -p111111

方法2:
直接在用户数据库文件中更改密码。
如果忘记了原密码,或者原密码被破坏了不能登录。可以先:
vim /etc/my.cnf
在[mysqld]下加入skip-grant跳过密码验证,然后restart mysqld,这里不能reload。

如果记得密码,就用密码登录:
mysql -uroot -ppassword

use mysql;
update user set password=password(‘aminglinux’) where user=’root’;

成功后退出mysql,编辑my.cnf并重reload,重启也可以。然后登录。

无论是更改密码后,还是更改了my.cnf,至少应该reload或者重启才能立刻生效。

安全问题
需要注意的是,直接在命令行中一次输入所有的密码,name在bash_history里可以看到这个密码。
在mysql内部输入的密码,在.mysql_history中看不到密码部分,因为关键信息都被隐藏了。
这两个文件都在用户的家目录下。

用户权限与创建

先root登录
mysql -uroot -p

以下是在mysql内的操作
1 创建一个本机用户
grant all on *.* to user1 identified by '123123'
这里的user1特指在localhost上登录。

2 创建一个可以在指定机器上登录的用户
grant all on *.* to 'user2'@'192.168.27.130' identified by '123123'
这个用户只能在指定的ip上登录mysql。加引号的目的是因为有@这个特殊符号。

3 创建一个可以在任意机器厂登录的用户
grant all on *.* 'user3'@'%' identified by '123123'
用户可以从任意机器上登录,显然这样也会造成安全隐患。%表示通配任意ip。

查看用户权限

 show grants; show grants for user2@192.168.133.1;

连接数据库

mysql的默认端口一般都是3306,但是也有一些情况端口号被改了。那么可以通过自定义端口登录。
例如以下命令指定从3307端口登录。
mysql -uroot -p111111 -h192.168.27.128 -P3307

如果不加-P那么在连接时将会报错。
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.27.128' (111)

常用命令

查询当前有哪些db

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || db2                || mysql              || mysql2             || performance_schema || test               || zabbix             || zrlog              |+--------------------+8 rows in set (0.00 sec)

查询某个库里的表有哪些

mysql> use mysql;  //先转到这个dbReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+---------------------------+| Tables_in_mysql           |+---------------------------+| columns_priv              || db                        || event                     || func                      || general_log               || help_category             || help_keyword              || help_relation             |

查看某个表的全部字段

mysql> desc db;+-----------------------+---------------+------+-----+---------+-------+| Field                 | Type          | Null | Key | Default | Extra |+-----------------------+---------------+------+-----+---------+-------+| Host                  | char(60)      | NO   | PRI |         |       || Db                    | char(64)      | NO   | PRI |         |       || User                  | char(16)      | NO   | PRI |         |       || Select_priv           | enum('N','Y') | NO   |     | N       |       || Insert_priv           | enum('N','Y') | NO   |     | N       |       || Update_priv           | enum('N','Y') | NO   |     | N       |       || Delete_priv           | enum('N','Y') | NO   |     | N       |       || Create_priv           | enum('N','Y') | NO   |     | N       |       |

以下方式可以显示的更清晰,在创建表时的命令。

mysql> show create table db\G*************************** 1. row ***************************       Table: dbCreate Table: CREATE TABLE `db` (  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',  `Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',  `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',  `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

查看当前是哪个用户

mysql> select user();+----------------+| user()         |+----------------+| root@localhost |+----------------+1 row in set (0.00 sec)

查看当前使用的数据库

mysql> select database();+------------+| database() |+------------+| mysql      |+------------+1 row in set (0.01 sec)

查看数据库版本

mysql> select version();+------------+| version()  |+------------+| 5.6.36-log |+------------+1 row in set (0.00 sec)

创建一个数据库

mysql> create database db3;Query OK, 1 row affected (0.00 sec)

创建一个表

mysql> use db3;  //先进入相应的dbDatabase changedmysql> create table t1(`id` int(4),`name` char(40));  //创建表Query OK, 0 rows affected (0.03 sec)mysql> show tables;  //查看当前bd的所有表+---------------+| Tables_in_db3 |+---------------+| t1            |+---------------+1 row in set (0.00 sec)

查看mysql状态

mysql> show status; //这些参数都可以在my.cnf中定义+-----------------------------------------------+-------------+| Variable_name                                 | Value       |+-----------------------------------------------+-------------+| Aborted_clients                               | 0           || Aborted_connects                              | 0           || Binlog_cache_disk_use                         | 0           || Binlog_cache_use                              | 408         || Binlog_stmt_cache_disk_use                    | 0           |

查看mysql参数

show variables;| ssl_capath                                             |                                                                                                                                                                                                                                                                                                                                                  || ssl_cert                                               |                                                                                                                                                                                                                                                                                                                                                  || ssl_cipher                                             |                                                                                                                                                                                                                                                                                                                                                  || ssl_crl                                                |                                                                                                                                                                                                                                                                                                                                                  || ssl_crlpath                                            |                                                                                                                                                                                                                                                                                                                                                  || ssl_key                                                |                                                                                                                                                                                                                                                                                                                                                  || storage_engine                                         | InnoDB                                                                                                                                                                                                                                                                                                                                           |

更改mysql参数

mysql> show variables like 'max_connect%';  //在mysql里,%d的功能等同于命令行的*表示通配+--------------------+-------+| Variable_name      | Value |+--------------------+-------+| max_connect_errors | 100   || max_connections    | 151   |+--------------------+-------+2 rows in set (0.00 sec)mysql> set global max_connect_errors=1000;  //更改Query OK, 0 rows affected (0.00 sec)mysql> show variables like 'max_connect_errors'; //再次查看+--------------------+-------+| Variable_name      | Value |+--------------------+-------+| max_connect_errors | 1000  |+--------------------+-------+1 row in set (0.00 sec)

查看当前的进程队列

mysql> show processlist;+----+--------+-----------------+--------+---------+------+-------+------------------+| Id | User   | Host            | db     | Command | Time | State | Info             |+----+--------+-----------------+--------+---------+------+-------+------------------+|  1 | zabbix | localhost:48084 | zabbix | Sleep   |    1 |       | NULL             ||  2 | zabbix | localhost:48086 | zabbix | Sleep   |   38 |       | NULL             ||  3 | zabbix | localhost:48088 | zabbix | Sleep   |    7 |       | NULL             ||  4 | zabbix | localhost:48090 | zabbix | Sleep   |    2 |       | NULL             |
原创粉丝点击