#mysql -uroot -p123456 使用socket连接登录;
查看socket文件位置 ps -aux | grep mysqld
[root@localhost ~]# ps aux |grep mysqld root 941 0.0 0.1 6268 1416 ? S 21:01 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pid mysql 1056 0.0 4.4 392208 45456 ? Sl 21:01 0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --log-error=/data/mysql/localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
指定socket文件位置的方法:/etc/my.cnf配置文件、编译的时候
#mysql -uroot -p123456 -S /socket文件的路径 使用指定socket文件登录;
# /usr/local/mysql/bin/mysql -uroot -p123456 -S /tmp/mysql.sock
mysql -uroot -p123456 -h127.0.0.1 -P3306 使用ip地址 3306端口连接,端口可以自定义;
# /usr/local/mysql/bin/mysql -uroot -p123456 -h127.0.0.1 -P3306
直接在shell里面执行mysql语句,需要加-e 参数
# mysql -uroot -p123456 -e "use mysql;select host,user from user where user=‘root‘";
650) this.width=650;" src="https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com//e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" /> 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6C/AC/wKiom1VPYr6gO7i9AAE_HVFikEg388.jpg" title="-e.jpg" alt="wKiom1VPYr6gO7i9AAE_HVFikEg388.jpg" />
查看在mysql执行的命令历史 cat /root/.mysql_history
首先删除文件,然后将文件软连接指向黑洞,防止命令泄露。
ln -s /dev/null /root/.mysql_history
2、mysql的常用操作:
查看所有的库 show databases;
650) this.width=650;" src="https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com//e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" /> 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/AC/wKiom1VPY7Cia8T0AACoqsVyTSM768.jpg" title="12.jpg" alt="wKiom1VPY7Cia8T0AACoqsVyTSM768.jpg" />
查看某个库的表 use db; show tables; db为database列出来的库名;
mysql> use discuz;
mysql> show tables;
查看表的字段 desc tb; tb为tables列出来的表的名字;
650) this.width=650;" src="https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com//e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" /> 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6C/A7/wKioL1VPZAGglzrBAAIBYXilGBU286.jpg" title="desc.jpg" alt="wKioL1VPZAGglzrBAAIBYXilGBU286.jpg" />
查看建表语句 show create table tb; 可以复制命令自己建立其他表;可以 查看表的默认引擎,以及默认字符集;
只写表名是在本数据库下面,也可以库.表查看其它库的表;
# show create table pre_home_show;
# show create table mysql.user;
ENGINE=MyISAM DEFAULT CHARSET=utf8
查看当前是哪个用户 select user();
查看当前所在的库 select database();
创建库 create database db1;
创建表 create table t1 (`id` int(4), `name` char(40));
查看数据库版本 select version();
查看mysql状态 show status;
查看mysql提供的存储引擎 show engines;
650) this.width=650;" src="https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com//e/u261/themes/default/images/spacer.gif" style="border:1px solid rgb(221,221,221);background-position:50% 50%;" alt="spacer.gif" /> 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/AC/wKiom1VPYnDx9iGcAAS-q5_PNnM944.jpg" title="engines.jpg" alt="wKiom1VPYnDx9iGcAAS-q5_PNnM944.jpg" />
修改mysql参数:
查看所有的变量 show variables;
show variables like ‘max_connect%‘; %代表统配符,与 * 一样,多个任意字符;
show variables like ‘‘; 引号里为空,也可以显示所有的变量;引号内可以写关键字加%,查看关键字的项目;
修改参数的值 set global max_connect_errors = 1000;
mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.01 sec)
mysql> set global max_connect_errors=10000;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10000 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
查看当前默认的存储引擎 show variables like ‘%storage_engine%‘;
650) this.width=650;" src="https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com//e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" /> 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/A7/wKioL1VPY9DwmovWAADk2tGL_8E093.jpg" title="storage_engine.jpg" alt="wKioL1VPY9DwmovWAADk2tGL_8E093.jpg" />
/data/mysql/ 目录下面存放的数据库的内容,有数据库名对应的目录;
myisam存储一个文件有3种格式(.frm .MYD .MYI )
查看mysql队列 show processlist; show full processlist;显示全部的信息。
650) this.width=650;" src="https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com//e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" /> 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6C/A7/wKioL1VPZ2-jDWZoAAF--RJnKiY575.jpg" title="11.jpg" alt="wKioL1VPZ2-jDWZoAAF--RJnKiY575.jpg" />
创建普通用户并授权
grant all on *.* to user1 identified by ‘123456‘;
grant all on db1.* to ‘user2‘@‘10.0.2.100‘ identified by ‘111222‘;
grant all on db1.* to ‘user3‘@‘%‘ identified by ‘231222‘;
更改密码 UPDATE mysql.user SET password=PASSWORD("newpwd") WHERE user=‘username‘ ;
查询 显示表的行数 select count(*) from mysql.user; myisam 显示速度比较快;
查询表的所有内容 select * from mysql.db;
条件查询表的内容 select * from mysql.db where host like ‘10.0.%‘;
mysql> select * from mysql.db\G; 如显示乱码,需要加\G,列表显示;
更新记录 update db1.t1 set name=‘aaa‘ where id=1;
mysql> update huang.name set name=‘aaa‘ where id=1;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0 Changed: 0 Warnings: 0
删除表的数据 delete form tb where id=1;
清空表 truncate table db1.t1;
删除表 drop table db1.tb1;
删除数据库 drop database db1;
修复表 repair table tb1 [use frm]; [use frm]为索引文件,索引重建一下;
3、mysql备份与恢复
使用mysqldump备份,适合数据量较小的;
备份 mysqldump -uroot -p123456 db > /tmp/1.sql 重定向到一个文件里面;
恢复 mysql -uroot -p123456 db < 1.sql 反向重定向,相当于复制了一个数据库;db必须真实存在;
只备份一个表 mysqldump -uroot -p db tb > 2.sql
恢复一个表 mysql -uroot -p123456 db < 2.sql
mysqldump -uroot -p123456 -d db tb > 3.sql 只备份表的语句;
备份时指定字符集 mysqldump -uroot -p123456 --default-character-set=utf8 db >1.sql 指定字符集防止乱码;
恢复也指定字符集 mysql -uroot -p123456 --default-character-set=utf8 db < 1.sql
本文出自 “模范生的学习博客” 博客,请务必保留此出处http://8802265.blog.51cto.com/8792265/1650084
mysql基本操作讲解
标签:lamp linux mysql