第一次写博客,也是刚刚开始学习mysql,总结的不好不对的地方,请大牛多多指点。

1、官网下载二进制安装包:mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz <=====安装有多种方法,之前已试过编译安装,比较麻烦,下次有心情了再总结编译安装吧。
2、解压到mysql安装目录/u01/mysql
3、创建mysql数据存放目录/data/3306/data和/data/3307/data <====打算创建2个实例,一个3306一个3307
4、分别在3306、3307目录下创建my.cnf配置文件和mysql启动脚本 <====mysql文件是自己写的一个启动、停止mysql的shell脚本

好,上面的内容都比较简单,二进制的文件,解压就能用,我们启动下3306试试看:

[root@mysql mysql]# /data/3306/mysql start <==启动
Starting MySQL...
[root@mysql mysql]# ps -ef|grep mysql <===看看相关进程
root 4485 1 0 10:40 pts/0 00:00:00 /bin/sh /u01/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf
mysql 5226 4485 0 10:40 pts/0 00:00:00 /u01/mysql/bin/mysqld --defaults-file=/data/3306/my.cnf --basedir=/u01/mysql --datadir=/data/3306/data --plugin-dir=/u01/mysql/lib/plugin --user=mysql --log-error=/data/3306/mysql_3306.err --open-files-limit=1024 --pid-file=/data/3306/mysqld.pid --socket=/data/3306/mysql.sock --port=3306
root 5252 3434 0 10:41 pts/0 00:00:00 grep mysql
[root@mysql mysql]# 
[root@mysql mysql]# netstat -lntup|grep 330
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 5226/mysqld 
[root@mysql mysql]#

6、进入数据库看看,多实例需要指定sock文件,不然会报错:

[root@mysql Desktop]# mysql <====提示需要一个sock文件
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/mysql/mysql-5.5.32/tmp/mysql.sock' (2)

[root@mysql Desktop]# mysql -uroot -S /data/3306/mysql.sock  <====这里报错是因为我设置了root的密码,所以登录时要指定root密码登录
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@mysql Desktop]# mysql -uroot -pyuanqk -S /data/3306/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.60-log MySQL Community Server (GPL) <===进来了,这里显示用的是社区版5.5.60

Copyright (c) 2000, 2018, 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> show databases;  
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.06 sec)

mysql>

7、没什么问题,我们创建个数据库看看,如果不清楚命令,可以使用help create database查看帮助

mysql> help create database;
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.

URL: http://dev.mysql.com/doc/refman/5.5/en/create-database.html


mysql> 
-----------------也可使用show create database mysql查看已安装的数据库使用的命令----------------
mysql> show create database mysql <===这个命令还可以这样写:show create database mysql\G
    -> ;
+----------+------------------------------------------------------------------+
| Database | Create Database                                                  |
+----------+------------------------------------------------------------------+
| mysql    | CREATE DATABASE `mysql` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)mysql>
好,命令知道了,我们创建个数据库试试
mysql> create database yuanqk;  <====就这么简单,比oracle简单的不止一点点
Query OK, 1 row affected (0.00 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| yuanqk             |
+--------------------+
5 rows in set (0.00 sec)mysql> show create database yuanqk
    -> ;
+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| yuanqk   | CREATE DATABASE `yuanqk` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)mysql>

8、刚才这么创建数据库什么也不指定的话,默认字符集是latin1,如果是编译安装的话,可以通过DDEFAULT_CHARSET指定默认的字符集。

9、那我想创建其他字符集的数据库怎么办,也非常简单

mysql> create database yuanqk_gbk default character set gbk collate gbk_chinese_ci; <===创建个gbk的数据库
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| yuanqk             |
| yuanqk_gbk         |
+--------------------+
6 rows in set (0.00 sec)

mysql> 

mysql> show databases like '%yuanqk%'; <===对了,这个地方其实可以使用like的
+---------------------+
| Database (%yuanqk%) |
+---------------------+
| yuanqk              |
| yuanqk_gbk          |
+---------------------+
2 rows in set (0.00 sec)

mysql> show create database yuanqk_gbk
    -> ;
+------------+--------------------------------------------------------------------+
| Database   | Create Database                                                    |
+------------+--------------------------------------------------------------------+
| yuanqk_gbk | CREATE DATABASE `yuanqk_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ |
+------------+--------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 


mysql> create database yuanqk_utf8 default character set utf8 collate utf8_general_ci;  <====创建个utf8的数据库
Query OK, 1 row affected (0.00 sec)

mysql> show create database yuanqk_utf8;
+-------------+----------------------------------------------------------------------+
| Database    | Create Database                                                      |
+-------------+----------------------------------------------------------------------+
| yuanqk_utf8 | CREATE DATABASE `yuanqk_utf8` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+-------------+----------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

10、那为什么要设置不同的字符集,因为字符集不一致会导致数据库中文内容出现乱码,但是如果是编译安装的话,得指定DEXTRA_CHARSETS参数,使其支持这些字符集,否则数据库不支持,指定了也没用。

-DEXTRA_CHARSETS=gbk、gb2312、utf8、ascii

11、那在生产环境中如何选择该安装什么字符集呢,这个要看开发程序,一般使用utf8的比较多。

12、好,mysql安装就总结到这,下面学学mysql的一些操作命令再回来总结。

13、在创建数据库时可以指定默认字符集default character同时还有个参数collate,这个collate是字符集校验规则,目前不清楚具体是干啥用的,不过可以通过下面命令获取所有字符集对应的校验规则;

mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                    | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                 | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
| macce    | Mac Central European        | macce_general_ci    |      1 |
| macroman | Mac West European           | macroman_general_ci |      1 |
| cp852    | DOS Central European        | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |
| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset       | binary              |      1 |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
+----------+-----------------------------+---------------------+--------+
39 rows in set (0.04 sec)

mysql>