标签:中间 rest pass 主机 脚本 my.cnf insert ade tmp
13.1 设置更改root密码设置更改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 -uroot就可以连接上去,不需要输入密码,但是不安全,所以就需要设置一个密码
为了方便使用mysql服务,将mysql目录加入到环境变量里"export PATH=/usr/local/mysql/bin:$PATH" 永久生效加入到"/etc/profile"里面,执行source /etc/profile 命令
1、查看mysql是否启动 ps aux|grep mysql,如果没有启动,执行"/etc/init.d/mysqld start"
[root@lnmp-server ~]# ps aux |grep mysql
root 841 0.0 0.1 115432 1728 ? S 10:33 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/ --pid-file=/data/mysql//lnmp-server.pid
mysql 1057 6.4 45.1 1296356 451644 ? Sl 10:33 0:04 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/data/mysql//lnmp-server.pid --socket=/tmp/mysql.sock --port=3306
root 1231 0.0 0.0 112720 972 pts/0 R+ 10:34 0:00 grep --color=auto mysql
2、设置mysql的root密码
安装完mysql后,默认root用户是没有密码的,我们可以通过mysql自带的命令mysqladmin给root设置一个密码
格式:mysqladmin -uroot passwd ‘123456‘
[root@lnmp-server ~]# mysqladmin -uroot password ‘123456‘
Warning: Using a password on the command line interface can be insecure.
在设置密码的时候,会看到有输出信息,但这不是报错信息,这是告诉你 你现在密码在当前命令行显示出来了,这样不×××全
3、知道密码的情况下更改密码
格式:mysqladmin -uroot -p‘123456‘ password ‘654321‘
[root@lnmp-server ~]# mysqladmin -uroot -p‘123456‘ password ‘654321‘
Warning: Using a password on the command line interface can be insecure.
4、忘记密码的情况修改
第一步:修改mysql的配置文件 /etc/my.cnf 在mysqld模块下加入一行skip-grant,表示忽略授权
vi /etc/my.cnf #在mysqld模块下新增一行
skip-gant
[root@lnmp-server ~]# cat /etc/my.cnf
[mysqld]
port = 3306
basedir=/usr/local/mysql
datadir=/data/mysql/
socket=/tmp/mysql.sock
user=mysql
default-time-zone=system
default-storage-engine=InnoDB
log-error=/var/log/mysqld.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-grant #增加这一行
第二步:在更改配置文件后,重启mysql服务 /etc/init.d/mysqld restart
[root@lnmp-server ~]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL. SUCCESS!
第三步:连接mysql,这时候在输入mysql -uroot ,会发现直接进入mysql,而不需要密码了
[root@lnmp-server ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 Source distribution
Copyright (c) 2000, 2017, 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>
第四步:更新密码,进入mysql后,输入:update mysql.user set password=password(‘123456‘) where user=‘root‘;
mysql> update mysql.user set password=password(‘123456‘) where user=‘root‘;
Query OK, 0 rows affected (0.06 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges; #刷新权限,让它生效
Query OK, 0 rows affected (0.00 sec)
mysql> quit #输入quit,退出mysql
Bye
注:提示说4行修改完毕,即使有些行是空的
第五步:更新完后修改mysql的配置文件 /etc/my.cnf 删除增加的那一行skip-grant,去掉忽略授权,并重启mysql服务 /etc/init.d/mysqld restart
[root@lnmp-server ~]# cat /etc/my.cnf
[mysqld]
port = 3306
basedir=/usr/local/mysql
datadir=/data/mysql/
socket=/tmp/mysql.sock
user=mysql
default-time-zone=system
default-storage-engine=InnoDB
log-error=/var/log/mysqld.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#skip-grant #这一行删掉或者注释掉
第六步:用更新完后密码登陆mysql -uroot -p‘123456‘
[root@lnmp-server ~]# mysql -uroot -p‘123456‘
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 Source distribution
Copyright (c) 2000, 2017, 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>
1、本地连接,默认使用sock连接
格式:mysql -uroot -p‘123456’
[root@lnmp-server ~]# mysql -uroot -p‘123456‘
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 Source distribution
Copyright (c) 2000, 2017, 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>
2、使用ip端口连接远程机器
格式:mysql -uroot -p‘111111‘ -h[远程mysql主机IP] -P[端口],mysql默认端口3306
[root@lnmp-server ~]# mysql -uroot -p‘123456‘ -h127.0.0.1 -P3306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.36 Source distribution
Copyright (c) 2000, 2017, 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>
3、指定sock文件(只适合本机登陆)
格式:mysql -uroot -p‘123456‘ -S/tmp/mysql.sock
[root@lnmp-server ~]# mysql -uroot -p‘123456‘ -S/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.
Your MySQL connection id is 5
Server version: 5.6.36 Source distribution
Copyright (c) 2000, 2017, 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>
4、不登陆mysql执行sql语句(常用于shell脚本)
格式:mysql -uroot -p123456 -e ‘show databases;‘
[root@lnmp-server ~]# mysql -uroot -p‘123456‘ -e ‘show databases;‘
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
查询库 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;
1、创建库db1,并查看库
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)
2、使用db1库,创建表tb1(新建字段id 整数型长度为4 ,字段name 字符型长度为40),并查看表
mysql> use db1;
Database changed
mysql> create table tb1 (id int(4),name char(40));
Query OK, 0 rows affected (0.13 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| tb1 |
+---------------+
1 row in set (0.00 sec)
3、查看表的字段
mysql> desc tb1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | char(40) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
4、查看建表语句
mysql> show create table tb1\G;
*************************** 1. row ***************************
Table: tb1
Create Table: CREATE TABLE `tb1` (
`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
注:\G是为了竖型显示,更清晰
5、查看当前用户和库
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> select database();
+------------+
| database() |
+------------+
| db1 |
+------------+
1 row in set (0.00 sec)
6、查询数据库版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.36 |
+-----------+
1 row in set (0.00 sec)
7、查看各参数 show variables; show variables like ‘max_connect%‘; // mysql下 % 为通配符
mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
8、修改参数 set global max_connect_errors=1000; ——>仅在内存中生效,若想重启生效修改/etc/my.cnf
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)
9、查看队列
show processlist; //查看库的状况,比如,那些用户在连,做了些什么操作,是否锁表
show full processlist; //查看到的对列,最后一个会非常完成的显示出来
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 14 | 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中也支持上下方向键查看执行过的命令,命令历史保存在用户家目录下.mysql_history文件中
1、创建一个用户并授权所有库和表的所有权限
格式:grant all on . to ‘user1‘ identified by ‘passwd‘;
mysql> grant all on *.* to ‘luo‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
mysql> select user from mysql.user;
+------+
| user |
+------+
| luo |
| root |
| root |
| |
| root |
| |
| root |
+------+
7 rows in set (0.00 sec)
2、针对指定的条件授权
格式:grant SELECT,UPDATE,INSERT on db1.* to ‘user2‘@‘192.168.180.1‘ identified by ‘passwd‘;
语句说明:授权查询 更新 插入 在数据库 db1所有表上 给来源ip为192.168.180.1的用户user2,并设定密码
mysql> grant SELECT,UPDATE,INSERT on db1.* to ‘user2‘@‘192.168.133.132‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
3、针对指定的条件授权
格式:grant all on db1.* to ‘user3‘@‘%‘ identified by ‘passwd‘;
语句说明:授权所有权限在数据库 db1所有表上 给来源ip为所有的用户user2,并设定密码 ,%表示通配,即所有的
mysql> grant all on db1.* to ‘user2‘@‘%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
4、show grants;
show grants;看的是root
mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
5、查看刚才授权的用户user2@‘192.168.133.132‘
mysql> show grants for ‘user2‘@‘192.168.133.132‘;
+--------------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.133.132 |
+--------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘user2‘@‘192.168.133.132‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO ‘user2‘@‘192.168.133.132‘ |
+--------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
增删改查,就是mysql和其他关系型数据库常用的select语句操作命令
备份库
备份mysql库 mysqlbak.sql文件就是mysql的备份库文件
[root@lnmp-server ~]# mysqldump -uroot -p123456 mysql>/tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@lnmp-server ~]# ll /tmp/mysqlbak.sql
-rw-r--r-- 1 root root 657033 7月 12 13:14 /tmp/mysqlbak.sql
我们可以通过mysqlbak.sql来恢复数据库,还可以恢复到另外一个数据库里面去
恢复库
这里把数据恢复到新的库里,创建一个新的库mysql2
[root@lnmp-server ~]# mysql -uroot -p123456 -e "create database mysql2;"
Warning: Using a password on the command line interface can be insecure.
[root@lnmp-server ~]# mysql -uroot -p123456 mysql2 </tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
进入到数据库里面,在后面加一个mysql2 就会进入到mysql2数据库里面
[root@lnmp-server ~]# mysql -uroot -p123456 mysql2
Warning: Using a password on the command line interface can be insecure.
查看数据库
mysql> select database();
+------------+
| database() |
+------------+
| mysql2 |
+------------+
1 row in set (0.00 sec)
备份表
针对库里面的某一个表去做备份,只需要在 库后面 加上 表名字 即可备份,先库 在表,中间是空格
备份表格式:mysqldump -uroot -p123456 databasename tablename > /tmp/user.sql
[root@lnmp-server ~]# mysqldump -uroot -p123456 mysql user >/tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
恢复表
恢复表的时候,只需要写库的名字,不需要去写表的名字
恢复表格式:mysql -uroot -p123456 mysql < /tmp/user.sql
[root@lnmp-server ~]# mysql -uroot -p123456 mysql </tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
恢复表到mysql2库
[root@lnmp-server ~]# mysql -uroot -p123456 mysql2 </tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
备份所有的库
格式:mysqldump -uroot -p123456 -A >/tmp/mysql_all.sql
-A 表示all所有的意思
[root@lnmp-server ~]# mysqldump -uroot -p111111 -A >/tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.
只备份表结构
格式:mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql
不需要表的数据,只需要表的结构
备份mysql2的表结构
[root@lnmp-server ~]# mysqldump -uroot -p123456 -d mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
两个机器的库备份,一个库备份到另一台机器上
解决:
首先两台机器能够通信
然后mysqldump -h 远程mysql-ip -uuser-ppassword dbname > /本地backup.sql
这样即可备份
标签:中间 rest pass 主机 脚本 my.cnf insert ade tmp
原文地址:http://blog.51cto.com/13736286/2141022