设置更改root密码 连接mysql 及mysql常用命令
13.1 设置更改root密码
13.2 连接mysql
13.3 mysql常用命令
扩展
mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/
mysql 配置详解: http://blog.linuxeye.com/379.html
mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同学分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.html
设置更改root密码目录概要
/usr/local/mysql/bin/mysql -uroot
更改环境变量PATH,增加mysql绝对路径
mysqladmin -uroot password '123456'
mysql -uroot -p123456
密码重置
vi /etc/my.cnf//增加skip-grant
重启mysql服务 /etc/init.d/mysqld restart
mysql -uroot
use mysql;
update user set password=password('aminglinux') where user='root';
设置更改root密码
root用户是mysql的超级管理员用户,和linux系统的root用户类似,不过和Linux的不一样
默认mysql的 root 用户密码是空的,直接就可以连接上去,不需要输入密码,但是不安全,所以就需要设置一个密码
为了方便使用mysql服务,将mysql目录加入到环境变量里
打开系统,查看mysql是否启动 ps aux|grep mysql
若是没有启动mysql的话,将mysql启动起来 service mysqld start
在启动mysql后,使用mysql -uroot命令,但是mysql命令会提示不存在,因为安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录并不在环境变量PATH里面,所以它会报错 ,若想要这个命令直接运行,需要把PATH做一个更改
[root@yong-01 ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@yong-01 ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[root@yong-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@yong-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@yong-01 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
若想要变量永久生效,还需要将export PATH=$PATH:/usr/local/mysql/bin/ 放入到 /etc/profile 最后面
假设若是没有运行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能运行mysql,因为变量还没有生效,想要这个变量生效,在配置文件中加入命令后,还需要执行source /etc/profile 命令,重新加载
一般是使用mysql -uroot -p命令 -p,表示指定密码
密码为空的时候,直接回车就可进入到mysql,并可以在其中操作一些mysql的一些行为
退出mysql,输入 quit 即可
设置mysql密码,命令为mysqladmin -uroot passwd '123456' 在 ' ' 为密码
[root@yong-01 ~]# mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure.
[root@yong-01 ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@yong-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
知道mysql的root密码,去更改密码
若是这时知道mysql密码,去修改mysql密码,看到输出的提示信息不用去理会
格式
[root@yong-01 ~]# mysqladmin -uroot -p'123456' password '654321'
Warning: Using a password on the command line interface can be insecure.
[root@yong-01 ~]# mysql -uroot -p'654321'
Warning: Using a password on the command line interface can be insecure.
不知道mysql的root密码,去更改密码
在不知道mysql的root用户密码的时候,先去更改 /etc/my.cnf 下配置文件中加入skip-grant
skip-grant ,表示忽略授权,也就是说操作mysql的时候不需要用户名和密码了,能直接登录
在更改配置文件后,重启mysql服务 service mysqld restart
这时候在输入mysql -uroot ,会发现直接进入mysql,而不需要密码了
[root@yong-01 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
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>
查看user表,输入select * from user; 命令,会看到输出乱七八糟的乱码,里面存放的就是用户名和密码,还有权限和授权等信息
查看password表,会看到密码都是加密的
mysql> select password from user where user='root';
+-------------------------------------------+
| password |
+-------------------------------------------+
| *2A032F7C5BA932872F0F045E0CF6B53CF702F2C5 |
| |
| |
| |
+-------------------------------------------+
4 rows in set (0.05 sec)
密码字段 函数 //用于加密密码 高亮部分:为条件语句
mysql> update user set password=password('111111') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
提示说4行修改完毕,即使有些行是空的
这样密码就更改成功了,输入quit退出数据库即可
再去 /etc/my.cnf 配置文件中删除免授权配置,即删除skip-grant——>若是不删除,那么之后所有的用户都不需要输入密码,就可以登录进去,这样安全性太低
重启mysql服务 service mysqld restart
重启完之后,再用新的密码测试下,会看到新的密码可以登录 mysql -uroot -p'111111'
这样就是成功更改mysql密码
连接mysql
[root@yong-01 ~]# mysql -uroot -p'111111' -h127.0.0.1 -P3306
[root@yong-01 ~]# mysql -uroot -p'111111' -S/tmp/mysql.sock
[root@yong-01 ~]# mysql -uroot -p'111111' -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql常用命令
查询库 show databases;
切换库 use mysql;
查看库里的表 show tables;
查看表里的字段 desc tb_name;
查看建表语句 show create table tb_name\G;
查看当前用户 select user();
查看当前使用的数据库 select database();
创建库 create database db1;
创建表 use db1; create table t1(id
int(4), name
char(40));
查看当前数据库版本 select version();
查看数据库状态 show status;
查看各参数 show variables; show variables like 'max_connect%';
修改参数 set global max_connect_errors=1000;
查看队列 show processlist; show full processlist;
创建库
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use db1;
Database changed
mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.03 sec)
mysql> show create table t1\G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR:
No query specified
在数据库里面取消命令,即在命令的最前面加一个 # 号就不会生效了
删除表 drop table t1;
mysql> drop table t1;
Query OK, 0 rows affected (0.02 sec)
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.06 sec)
mysql> show create table t1\G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35 |
+-----------+
1 row in set (0.00 sec)
mysql> show variables like 'max_connect%';
+--------------------+-------+
| 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%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
查看队列
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 5 | root | localhost | db1 | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 14 | root | localhost | db1 | Query | 0 | init | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
mysql>
推荐本站淘宝优惠价购买喜欢的宝贝:
本文链接:https://sg.hqyman.cn/post/9223.html 非本站原创文章欢迎转载,原创文章需保留本站地址!
打赏

微信支付宝扫一扫,打赏作者吧~
休息一下~~