一、更改Mysql数据库密码
1、登录mysql,默认mysql没有设置密码,需要设置密码。quit 退出登录。
/usr/local/mysql/bin/mysql -uroot
2、设置mysql环境变量,并永久生效。就可以直接mysql -uroot 直接登录了。
[root@localhost ~]# PATH=$PATH:/usr/local/mysql/bin
[root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@localhost ~]# source /etc/profile
3、设置root密码 mysqladmin -uroot password 'qwerty1'
此时再用 mysql -uroot 就无法直接登录了,需要 mysql -uroot -p ,再输入密码,才能登录
4、更改root密码
mysqladmin -uroot -p'qwerty1' password 'asdfgh2'
此时root用户的密码就是asdfgh2了。
5、忘记或不知道root密码时重置密码。
1)修改配置文件/etc/my.cnf,增加如下内容
skip-grant #忽略授权,不用输入密码就能登录
2)重启mysql服务,登录mysql,此时不需要密码就能登陆了
/etc/init.d/mysqld restart
3)更改mysql库中user表中root的密码。
use mysql; #切换到mysql库
select * from user; #查看user表所有信息
select password from user where user='root'; # 查看root用户的密码
修改root密码
update user set password=password('aminglinux') where user='root';
4、在修改/etc/my.cnf,删掉skip-grant,重启服务登录就OK了。
二、连接Mysql
1、常用的连接Mysql的命令
mysql -uroot -paminglinux
mysql -uroot -paminglinux -h192.168.134.141 -P3306 #-h指定远程主机的IP,-P指定端口
mysql -uroot -paminglinux -S/tmp/mysql.sock #以sock的方式连接,-S指定sock,只适合本机
mysql -uroot -paminglinux -e "show databases" #-e 指定命令,在shell脚本中使用
三、Mysql的常用命令
1、查看都有什么数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.12 sec)
mysql>
2、查询库里的表,首先切换到库 use 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> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| fun
3、查看表的字段 desc user;
4、查看表的创建,\G的作用是让列出来的结果竖排显示。
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',
5、查看当前的是哪个用户 select user();
6、查看当前使用的库;
7、创建库
8、创建表
9、修改字符集,如下图使用的是latin1,可以修改为utf8.
10、查看当前数据库的版本;
11、查看mysql当前状态
12、查看mysql的参数
show variables ; 查询所有
查询指定的,可以使用like 和%来模糊查询,%通配符,当你没有记全参数时。
13、修改mysql的参数 set global max_connect_errors =200;使用set global 可以临时修改参数,重启后会消失。想永久生效,需在/etc/my.cnf中定义。
14、查看队列,使用它可以查看当前mysql在干什么。