欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

mysql的设置更改root密码、连接、常用命令

发布时间:2024/5/15 数据库 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 mysql的设置更改root密码、连接、常用命令 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

13.1 设置更改root密码

  • 更改环境变量PATH ,增加mysql绝对路径
    首次直接使用mysql会提示‘该命令不存在’,原因是还没有将该命令加入环境变量,如果要使用该命令,需要使用其绝对路径:/usr/local/mysql/bin/mysql,为了方便,先将其加入系统环境变量:
[root@taoyuan ~]# export PATH=$PATH:/usr/local/mysql/bin/

mysql命令路径暂时加入环境变量,系统重启后该变量会失效,若要永久生效,需要将其加入环境变量配置文件:

vi /etc/profile#在配置文件最后 把上面的命令加#执行source命令生效 [root@taoyuan ~]# source /etc/profile
  • 首次登陆
[root@taoyuan ~]# mysql -uroot

注: -p=passwd,使用密码登录,在此可以将密码直接输入在命令行(跟在-p后面,不加空格:-p'123456'<此处单引号可以不加,但是当密码中有特殊符号时必须加,所以在命令行输入密码时养成习惯:加单引号>),也可以不在命令行输入,只跟-p选项,然后根据提示信息:“Enter password”,输入密码进行登录(此方法不会暴露用户密码,安全)。

  • 设置mysql 的root密码 && 更改
[root@taoyuan ~]# mysqladmin -uroot password '123456' Warning: Using a password on the command line interface can be insecure.[root@taoyuan ~]# mysql -uroot -p Enter password: #输入密码#更改新的密码 [root@taoyuan ~]# mysqladmin -uroot -p'123456' password 'taoyuan' Warning: Using a password on the command line interface can be insecure. [root@taoyuan ~]# mysql -uroot -p'taoyuan' Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g.
  • 密码重置
[root@taoyuan ~]# vi /etc/my.cnf#my.cnf 配置文件内容 [mysqld] skip-grant datadir=/data/mysql #增加skip-grant #忽略授权,意思是不用密码登陆#重启mysql服务 [root@taoyuan ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! #登陆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 -ADatabase changed#查看表 mysql> select * from user;#查看user表 mysql> select password from user; +-------------------------------------------+ | password | +-------------------------------------------+ | *758ABA8398EF87C993D2C4420DACD8946907C873 | | | | | | | | | | | +-------------------------------------------+ 6 rows in set (0.00 sec)#修改密码 mysql> update user set password=password('Aa123456') where user='root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0#quit ,把/etc/my.cnf 配置文件修改回去 ,重启mysql服务 [root@taoyuan ~]# vi /etc/my.cnf [root@taoyuan ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@taoyuan ~]# mysql -uroot -p'Aa123456' Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g.

注: 完成该操作之后就可以任意登录mysql了(无需密码),所以此时mysql安全性很差,平时配置文件中一定不要添加该参数!!

13.2 连接mysql

  • 本机直接登陆
[root@taoyuan ~]# mysql -uroot -pAa123456
  • 通过端口连接(TCP/IP)
[root@taoyuan ~]# mysql -uroot -pAa123456 -h127.0.0.1 -P3306 # -P 指定端口
  • 使用sock的连接
[root@taoyuan ~]# mysql -uroot -pAa123456 -S/tmp/mysql.sock #适用于本机连接
  • 连接mysql操作一个命令
[root@taoyuan ~]# mysql -uroot -pAa123456 -e "show databases" Warning: Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ #shell脚本使用中比较方便,可以直接获取数据

13.3 mysql常用命令

#查询库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)#切换库 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 -ADatabase changed#查看库里的表 mysql> show tables;#查看字段 mysql> desc user;#查看建表语句 mysql> show create table user\G; #\G 表示:竖排显示#查看当前用户 mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)#查看当前使用的数据库 mysql> select database(); +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec)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 -ADatabase changed mysql> select database(); +------------+ | database() | +------------+ | mysql | +------------+ 1 row in set (0.00 sec)#创建库 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.02 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 #ENGINE=InnoDB DEFAULT CHARSET=latin1 可以自定义 #如下 mysql> drop table t1; #删除t1表 Query OK, 0 rows affected (0.01 sec) mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.01 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 status;#查看各参数 mysql> show variables #指定的参数 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 processlist; +----+------+-----------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+-------+------------------+ | 9 | 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 | +----+------+-----------+------+---------+------+-------+-----------------------+ | 9 | root | localhost | db1 | Query | 0 | init | show full processlist | +----+------+-----------+------+---------+------+-------+-----------------------+ 1 row in set (0.00 sec) #full 查看完整的队列

小常识:
敲过的命令历史在mysql_history 文件里

转载于:https://blog.51cto.com/3622288/2060499

总结

以上是生活随笔为你收集整理的mysql的设置更改root密码、连接、常用命令的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。