设置/更改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 id is 1
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 |
+-------+----------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| 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,查看数据库的操作)
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 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