设为首页 收藏本站
查看: 1499|回复: 0

[经验分享] mysql(设置/更改mysql密码,连接MySQL,MySQL常用命令,MySQL两种引擎区别)

[复制链接]

尚未签到

发表于 2018-9-30 08:10:24 | 显示全部楼层 |阅读模式
  设置/更改MySQL的密码问题
  一,设置mysql密码
  我们安装MySQL时,把它放在了/usr/local/mysql/下,在当前的环境中并没有这个目录,所以我们要把目录添加到当前目录下。
  [root@lnmp ~]# vim /etc/profile
  export PATH=$PATH:/usr/local/mysql/bin/
  [root@lnmp ~]# source /etc/profile
  这样我们就可以在任意环境下进入MySQL,第一次进入MySQL时,是没有密码的。
  [root@lnmp ~]# mysql -uroot                           (用这个命令可以直接进入mysql。)
  Welcome to the MySQL monitor.  Commands end with ; or \g.

  Your MySQL connection>  mysql> quit                           (quit可以直接退出MySQL)
  Bye
  [root@lnmp ~]# ll /usr/local/mysql/bin/mysqladmin            (在Mysql/bin下,这个文件用来配置或更改mysql密码)
  -rwxr-xr-x. 1 7161 31415 8055556 3月  18 2017 /usr/local/mysql/bin/mysqladmin
  [root@lnmp ~]# mysqladmin -uroot password 'westos123';      (添加密码westos123,下面warning是警告你把密码显示出来了)
  Warning: Using a password on the command line interface can be insecure.
  我们不输入密码登录看看:
  [root@lnmp ~]# mysql -uroot                        (提示你没有输入密码)
  ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  [root@lnmp ~]# mysql -uroot -p                         (加-p输入刚才的密码即可进入)
  Enter password:
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  二、如何更改MySQL密码
  [root@lnmp ~]# mysqladmin -uroot -p'westos123' password 'westos321'       (这里需要注意-p后不用加空格)
  Warning: Using a password on the command line interface can be insecure.
  [root@lnmp ~]# mysql -uroot -p'westos123'     (用旧密码登录时报错)
  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@lnmp ~]# mysql -uroot -p'westos321'          (新密码可以正常登录)
  Warning: Using a password on the command line interface can be insecure.
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  三、如何重置MySQL密码?(忘记了MySQL密码)
  [root@lnmp ~]# vim /etc/my.cnf       (修改配置文件)
  [mysqld]                         (在mysqld的下面加上一行)
  skip-grant
  [root@lnmp ~]# /etc/init.d/mysqld restart           (重新启动MySQL服务)
  Shutting down MySQL.. SUCCESS!
  Starting MySQL.. SUCCESS!
  进入mysql库里更改一个表。
  mysql> select password from user;                     (可以查看密码)
  +-------------------------------------------+
  | password                                  |
  +-------------------------------------------+
  | *1836D7557E753782F1509748BD403456701A0D2F |
  | *1836D7557E753782F1509748BD403456701A0D2F |
  | *1836D7557E753782F1509748BD403456701A0D2F |
  | *1836D7557E753782F1509748BD403456701A0D2F |
  |                                           |
  |                                           |
  +-------------------------------------------+
  6 rows in set (0.00 sec)
  下面这条命令就是用来修改密码的,第一个password是字符,第二个password是函数,我们看到上面的密码都是加密的,就是因为函数
  mysql> update user set password=password('aminglinux') where user='root';
  Query OK, 4 rows affected (0.00 sec)
  Rows matched: 4  Changed: 4  Warnings: 0
  修改完配置文件以后,我们应该把刚才的配置文件改回来,并重新启动MySQL,否则任何人登录MySQL都不用密码。
  [root@lnmp ~]# vim /etc/my.cnf
  删除 skip-grant
  [root@lnmp ~]# /etc/init.d/mysqld restart
  Shutting down MySQL.. SUCCESS!
  Starting MySQL. SUCCESS!
  我们再尝试用新密码登录,发现配置完成、
  [root@lnmp ~]# mysql -uroot -p'aminglinux'
  Warning: Using a password on the command line interface can be insecure.
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  连接MySQL
  一、连接mysql的二种方法。
  第一种就是我们经常访问本机的方式,直接使用账户和密码登录
  [root@lnmp ~]# mysql -uroot -paminglinux
  Warning: Using a password on the command line interface can be insecure.
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  其实这种方法,默认是用sock的连接的,如果把命令全部敲出来,应该是
  [root@lnmp ~]# mysql -uroot -paminglinux -S/tmp/mysql.sock      (用S来指定sock文件,默认监听的是/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来做实验,本机的ip是127.0.0.1)
  [root@lnmp ~]# mysql -uroot -paminglinux -h127.0.0.1 -P3306 (-h来指定ip地址,-P指定端口号,MySQL默认外网访问3306端口)
  Warning: Using a password on the command line interface can be insecure.
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  二、用命令行直接操作mysql命令。(在shell脚本时非常实用)
  [root@lnmp ~]# mysql -uroot -paminglinux -e "show databases" (-e直接加命令,可以不用进入mysql直接操作,查看所有数据库)
  Warning: Using a password on the command line interface can be insecure.
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mysql              |
  | performance_schema |
  | test               |
  +--------------------+
  
  MySQL常用命令
  
  mysql> create database rxr;                           (创建一个rxr的数据库)
  Query OK, 1 row affected (0.49 sec)
  mysql> show databases;                                (查看本地所有数据库)
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | lty                |
  | mysql              |
  | performance_schema |
  | rxr                |
  | test               |
  +--------------------+
  6 rows in set (0.00 sec)
  mysql> use rxr;                       (进入到rxr数据库中)
  Database changed
  mysql> create table lty(`id`int(4),`name`char(40)); (创建一个lty的表,Id和Name只是表里的参数,括号里定义最大字符)
  Query OK, 0 rows affected (0.01 sec)
  mysql> show tables;                             (查看所在数据库里的表)
  +---------------+
  | Tables_in_rxr |
  +---------------+
  | lty           |
  +---------------+
  1 row in set (0.00 sec)
  mysql> show create table lty;             (查看建表的语句)
  +-------+------------------------------------------------------------------------------------------------------------------------+
  | Table | Create Table                                                                                                           |
  +-------+------------------------------------------------------------------------------------------------------------------------+
  | lty   | CREATE TABLE `lty` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
  +-------+------------------------------------------------------------------------------------------------------------------------+
  1 row in set (0.00 sec)
  mysql> desc lty;              (查看表里的字段)
  +-------+----------+------+-----+---------+-------+
  | Field | Type     | Null | Key | Default | Extra |
  +-------+----------+------+-----+---------+-------+

  |>  | name  | char(40) | YES  |     | NULL    |       |
  +-------+----------+------+-----+---------+-------+
  mysql> select user();              (查看链接数据库的用户)
  +----------------+
  | user()         |
  +----------------+
  | root@localhost |
  +----------------+
  1 row in set (0.00 sec)
  mysql> select database();                     (查看当前使用的数据库)
  +------------+
  | database() |
  +------------+
  | rxr        |
  +------------+
  1 row in set (0.00 sec)
  mysql> select ();                 (查看当前数据库版本)
  +-----------+
  | version() |
  +-----------+
  | 5.6.36    |
  +-----------+
  1 row in set (0.00 sec)
  mysql> show status;          (查看数据库状态)因为比较长,所以就不列出来内容了
  mysql> show variables;   (查看各项参数,一般这里的参数都可以用vim在/etc/my.cnf里修改)因为比较长,所以就不列出来内容了mysql> show variables like 'max_connect_errors';       (列出来指定选项)
  +--------------------+-------+
  | Variable_name      | Value |
  +--------------------+-------+
  | max_connect_errors | 100   |
  +--------------------+-------+
  1 row in set (0.00 sec)
  mysql> set global max_connect_errors=1000;      (直接在数据库里修改参数,但是如果想要永久修改,还是要去配置文件里)
  Query OK, 0 rows affected (0.01 sec)
  mysql> show variables like 'max_connect_errors';   (可以看到max_connect_errors被修改成1000)
  +--------------------+-------+
  | Variable_name      | Value |
  +--------------------+-------+
  | max_connect_errors | 1000  |
  +--------------------+-------+
  1 row in set (0.00 sec)
  mysql> show processlist;          (这个命令相当于ps或者top,查看数据库的操作)
  +----+------+-----------+------+---------+------+-------+------------------+

  |>  +----+------+-----------+------+---------+------+-------+------------------+
  |  3 | root | localhost | rxr  | Query   |    0 | init  | show processlist |
  +----+------+-----------+------+---------+------+-------+------------------+
  1 row in set (0.00 sec)
  
  MySQL引擎myisam和innodb的区别:
  http://blog.csdn.net/xifeijian/article/details/20316775


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-606375-1-1.html 上篇帖子: openwrt安装nginx+php+mysql教程 下篇帖子: freebsd下pureftpd结合mysql详细配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表