mysql基本操作

一、以RPM包方式安装的mysql安装好后目录结构如下:

  1、工具程序在/usr/bin目录中---ls /usr/bin/mysql*

  2、服务器程序/usr/sbin/mysqld

  3、数据目录/var/lib/mysql

  4、默认情况下mysql将错误日志文件、二进制日志文件及进程文件写在/var/lib/mysql目录中,如localhost.err、localhost.pid、localhost-bin.001等

  5、要改变这些情况可以修改/etc/my.cnf文件

     如将日志文件写在/var/log目录中,可以在my.cnf文件中加入下面两行:

     [mysqld_safe]

     error-log = /var/log/mysqld.log
   
    6、卸载mysql,先stop掉mysql服务,如果是64位系统,卸载时会提示error: "mysql" specifies multiple packages;因为安装的时候还装了32位mysql,所以因为有2个mysql,卸载失败;可以这样rpm -e --allmatches mysql

二、有个实用程序/usr/bin/mysql_install_db,该程序可以用来初始化 mysql数据库,即创建/var/log/mysql目录,及创建mysql数据库(mysql授权表等信息)及test数据库(空库),如果不小心删 除了/var/log/mysql目录可以通过该程序来初始化.

三、基础操作
    1、查看mysql使用的何种引擎
    mysql> show engines;
+------------+---------+----------------------------------------------------------------+
| Engine     | Support | Comment                                                        |
+------------+---------+----------------------------------------------------------------+
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance         |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables      |
| InnoDB     | NO      | Supports transactions, row-level locking, and foreign keys     |
| BerkeleyDB | NO      | Supports transactions and page-level locking                   |
| BLACKHOLE  | NO      | /dev/null storage engine (anything you write to it disappears) |
| EXAMPLE    | NO      | Example storage engine                                         |
| ARCHIVE    | NO      | Archive storage engine                                         |
| CSV        | NO      | CSV storage engine                                             |
| ndbcluster | NO      | Clustered, fault-tolerant, memory-based tables                 |
| FEDERATED  | NO      | Federated MySQL storage engine                                 |
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                          |
| ISAM       | NO      | Obsolete storage engine                                        |
+------------+---------+----------------------------------------------------------------+
12 rows in set (0.00 sec)
表示默认当前使用的是“MyISAM”引擎;   但是不支持“InnoDB”引擎

    2、修改引擎为innodb <***前提是支持InnoDB引擎***>
    更灵活的方式是在随MySQL服务器发布同时提供的MySQL客户端时指定使用的存储引擎。最直接的方式是在创建表时指定存储引擎的类型,向下面这样:

    mysql> CREATE TABLE mytable (id int, title char(20)) ENGINE = INNODB

    你还可以改变现有的表使用的存储引擎,用以下语句:

    mysql> ALTER TABLE mytable ENGINE = MyISAM

    ***如果不支持“Innodb”引擎,则重新编译,编译时加上参数 --with-plugins=innobase
    在配置文件my.cnf中的 [mysqld] 下面加入default-storage-engine = INNODB 一句,保存;重启mysql

    mysql> show engines;
+------------+---------+----------------------------------------------------------------+
| Engine     | Support | Comment                                                        |
+------------+---------+----------------------------------------------------------------+
| MyISAM     | YES     | Default engine as of MySQL 3.23 with great performance         |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables      |
| InnoDB     | DEFAULT | Supports transactions, row-level locking, and foreign keys     |
| BerkeleyDB | NO      | Supports transactions and page-level locking                   |
| BLACKHOLE  | NO      | /dev/null storage engine (anything you write to it disappears) |
| EXAMPLE    | NO      | Example storage engine                                         |
| ARCHIVE    | NO      | Archive storage engine                                         |
| CSV        | NO      | CSV storage engine                                             |
| ndbcluster | NO      | Clustered, fault-tolerant, memory-based tables                 |
| FEDERATED  | NO      | Federated MySQL storage engine                                 |
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                          |
| ISAM       | NO      | Obsolete storage engine                                        |
+------------+---------+----------------------------------------------------------------+
12 rows in set (0.07 sec)


    3、mysql修改默认字符
    mysql> show variables like 'character%';
+--------------------------+----------------------------------------+
| Variable_name            | Value                                  |
+--------------------------+----------------------------------------+
| character_set_client     | latin1                                 |
| character_set_connection | latin1                                 |
| character_set_database   | latin1                                 |
| character_set_filesystem | binary                                 |
| character_set_results    | latin1                                 |
| character_set_server     | latin1                                 |
| character_set_system     | utf8                                   |
| character_sets_dir       | /usr/local/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------------+
8 rows in set (0.00 sec)

<1. character_set_client,终端字符集,告诉Server客户端提交的SQL语句的编码格式

<2. character_set_connection,连接字符集,是服务器翻译SQL语句时用到的编码格式

<3. character_set_results,返回的结果集的字符集,是服务器返回结果集之前把结果集转换成的编码格式

在linux下修改3个my.cnf的1个/etc/mysql/my.cnf文件

找到客户端配置[client] 在下面添加
default-character-set=utf8 默认字符集为utf8
在找到[mysqld] 添加
default-character-set=utf8 默认字符集为utf8
init_connect='SET NAMES utf8' (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)

修改好后,重新启动mysql 即可,查询一下show variables like 'character%';
mysql> show variables like 'character%';
+--------------------------+----------------------------------------+
| Variable_name            | Value                                  |
+--------------------------+----------------------------------------+
| character_set_client     | utf8                                   |
| character_set_connection | utf8                                   |
| character_set_database   | utf8                                   |
| character_set_filesystem | binary                                 |
| character_set_results    | utf8                                   |
| character_set_server     | utf8                                   |
| character_set_system     | utf8                                   |
| character_sets_dir       | /usr/local/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------------+
8 rows in set (0.00 sec)

     4、二进制日志恢复数据

默认情况下MYSQL是没有打开二进制日志的。你需要在my.cnf文件的
[mysqld]语句内添加log-bin参数,例如:
[mysqld]
log-bin=server1


# mysql -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13 to server version: 5.0.27-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use test
Database changed
mysql> insert into emp(id,name) values(1,'z1');
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye
# mysqldump -u root -p test > /root/test.emp
Enter password:
# mysql -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15 to server version: 5.0.27-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use test
Database changed
mysql> insert into emp(id,name) values(2,'z2');
Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;
+------+------+
| id   | name |
+------+------+
|    1 | z1   |
|    2 | z2   |
+------+------+
2 rows in set (0.00 sec)

mysql> delete from emp;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from emp;
Empty set (0.00 sec)

mysql> quit
Bye
# mysql -u root -p test < /root/test.emp
Enter password:
# mysql -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17 to server version: 5.0.27-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use test
Database changed
mysql> select * from emp;
+------+------+
| id   | name |
+------+------+
|    1 | z1   |
+------+------+
1 row in set (0.00 sec)

只能恢复到做了备份的那个时段
mysql> quit
Bye
下面就要用到二进制日志来恢复从备份到数据库损坏这段时间的操作数据

  
  
  
  
  1. # pwd  
  2. /var/lib/mysql  
  3. # mysqlbinlog mysql-bin.000001 > /tmp/bin.log | vi /tmp/bin.log  
  4. 查找到刚清空emp表的语句“delete from emp”  
  5. # at 200  
  6. #110412 13:49:26 server id 1  end_log_pos 302   Query   thread_id=15    exec_time=0     error_code=0 
  7. SET TIMESTAMP=1302587366;  
  8. insert into emp(id,name) values(2,'z2');  
  9. # at 302  
  10. #110412 13:49:47 server id 1  end_log_pos 380   Query   thread_id=15    exec_time=0     error_code=0 
  11. SET TIMESTAMP=1302587387;  
  12. delete from emp;  
  13. # mysqlbinlog --start-position="200" --stop-position="302" mysql-bin.000001 | mysql -u root -p test  

Enter password:
# mysql -u root -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21 to server version: 5.0.27-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use test;
Database changed
mysql> select * from emp;
+------+------+
| id   | name |
+------+------+
|    1 | z1   |
|    2 | z2   |
+------+------+
2 rows in set (0.00 sec)

mysql>quit
***mysql每重启一次就会多出一条二进制日志mysql-bin.0000*

清空二进制日志方法有多种
mysql>reset master;
mysql>quit
此时/var/lib/mysql中的日志从mysql-bin.000001开始<已被初始化,其中无任何操作记录>

你可能感兴趣的:(mysql,数据库,职场,基本操作,休闲)