mysql设置更改root密码、mysql服务器的连接、mysql常用命令
1、设置更改root密码
查看mysql 启动与否,若没启动就运行:/usr/local/mysql56/bin/mysql
ps aux |grep mysql 或 netstat -tulnp |grep 3306
运行mysql 命令,会出现:
-bash: mysql: command not found
就把mysql 添加到环境变量:
临时添加:PAHT=$PATH:/usr/local/mysql56/bin
永久添加:echo "export PATH=$PATH:/usr/local/mysql56/bin" >>/etc/profile
再运行soucre /etc/profile 或 . /etc/profile
mysql密码的设置,给root用户设置密码
mysql -uroot password 'new_password' 或用下面的方式
如何修改密码:
mysqladmin -uroot -p 'old_password' password 'new_password' 或用下面的方式
忘记密码了如何修改:
首先,得修改配制文件:vim /etc/my.cnf
在[mysqld]模块下加入skip-grant ,然后重启mysql服务:sed -i '/3306/a\skip-grant' /etc/my.cnf
重启:/etc/init.d/mysqld restart
然后,切换数据库:use mysql;
然后,再在数据库里面修改root密码:update user set password=password('new_password') where user='root';
这样就把密码给改回来了
重新测试,就把my.cnf给改回来:
2、mysql的连接
mysql -uroot -p'password'
mysql -h127.0.0.1 -uroot -p'password'
mysql -S/tmp/mysql.sock -uroot -p'password'
mysql -uroot -p -e "show databases"
3、mysql常用命令
查询有多少库:show databases;
使用某个库:use database_name;
查看有多少个表:show tables;
查看某张表里的字段:desc table_name:
查看某张表创建时输入的命令:show create table table_name\G;
查看当前登陆有哪些用户:select user();
查看当前使用的哪个库:select database();
查看当前数据库的版本:select version();
查看配制文件可定义的各种参数:show variables;
查看目前的进程数:show processlist; 或 show full processlist; #这是用得最多的(即查看数据库正在进行的操作)
创建一个数据库:create database db_name;
创建一张表:create table table_name('name' types(length),'name' type(length),........)
创建一张表:create table table_test(id int(10),name char(32),sex int(4));
第一部分完成。
13.4 mysql用户管理
创建mysql用户和授权,对单独的用户进行授权,因为不可能每一个用户都去连接root,root的权限是很大的,如果让每个用户都能够使用root,这样是非常不安全的。
创建用户:
[root@jimmylinux-001 ~]# mysql -uroot -pabcd1234 登录到mysql
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456a';
Query OK, 0 rows affected (0.03 sec)
grant是授权,all表示所有权限,前面的*表示库名,例如mysql.*就表示mysql库名下面所有的表,*.*就是所有库名的表。@后面指定来源IP,也可以使用%表示所有IP。
也可以指定localhost方式登录,首先用root登录,然后把127.0.0.1改为localhost,这样就可以不用加-h127.0.0.1也能够登录了。
可以针对具体的权限去授权
grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.52.1' identified by 'passwd';
还可以针对所有的IP去授权
grant all on db1.* to 'user3'@'%' identified by 'passwd';
mysql> show grants for user1@'127.0.0.1'; 查看指定用户的授权是什么
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for user1@127.0.0.1 |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看user2的授权
mysql> grant all on *.* to 'user2'@'192.168.52.1' identified by '123456a'; 首先要创建一个user2用户
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for user2@'192.168.52.1'; 查看user2用户的授权
+--------------------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.52.1 |
+--------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user2'@'192.168.52.1' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.52.1' |
+--------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
如果遇到IP不够用,用户除了要在192.168.52.1上登录,还想在192.168.52.2的IP登录,这个时候只需要把授权的命令全部执行一遍,只需要把IP改为192.168.52.2
如果需要复制一个用户,假如知道用户名,但是密码不知道,就可以通过上面的方式进行设置,把2条命令复制执行,更改一下IP就可以了。
13.5 常用sql语句
select count(*) from mysql.user; 查看这个表的行数,库和表中间有一个.作为分隔符。
select * from mysql.db; 查看所有内容,如果一个表内容很多,不建议使用*查看所有。
select db from mysql.db; 查看字段
select db,user from mysql.db; 查看2个字段
select * from mysql.db where host like '192.168.%'; 模糊查询,使用这样方式可以树形显示
insert into db1.t1 values (1, 'abc'); 插入字段内容
update db1.t1 set name='aaa' where id=1; 更新字段内容
truncate table db1.t1; 清空一个表里面的内容,但是表的结构框架还会保留。
drop table db1.t1; 彻底清空一个表
drop database db1; 彻底清空一个数据库
查看表行数
查看所有内容
mysql> select * from mysql.db\G;
查看字段
插入字段内容
更新字段内容
删除一个表
清空一个表里面的内容,但是表的结构框架还会保留。
彻底清空一个表和一个数据库
在做彻底清空表或者数据库时,一定要非常谨慎,如果没有做备份,最好不要轻易执行清空操作。
13.6 mysql数据库备份恢复
为了避免误操作删除一个表或数据库,平常一定要养成数据库备份的习惯。
备份数据库
[root@jimmylinux-001 ~]# mysqldump -uroot -pabcd1234 mysql > /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
恢复数据库
[root@jimmylinux-001 ~]# mysql -uroot -pabcd1234 -e "create database mysql2" 创建一个新的数据库mysql2
Warning: Using a password on the command line interface can be insecure.
[root@jimmylinux-001 ~]# mysql -uroot -pabcd1234 mysql2 < /tmp/mysqlbak.sql 把之前备份的数据库恢复到新建的数据库mysql2里面去
Warning: Using a password on the command line interface can be insecure.
备份一个表
[root@jimmylinux-001 ~]# mysqldump -uroot -pabcd1234 mysql user > /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
恢复一个表
[root@jimmylinux-001 ~]# mysql -uroot -pabcd1234 mysql2 < /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
备份所有数据库
[root@jimmylinux-001 ~]# mysqldump -uroot -pabcd1234 -A >/tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.
只备份表结构
[root@jimmylinux-001 ~]# mysqldump -uroot -pabcd1234 -d mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
总结:
-A 备份所有的数据库
-d 备份数据库里面的表结构,不备份数据。
备份一个表,先库再表,中间用空格做一个分隔,恢复的时候只需要写mysql就行了,不用写表。
MySQLdump 只适合数据量不大的,如果数据量很大有上百G或者上T的数据量就不适用了。