附录B MySQL数据库安装

B.1 MySQL在Windows中安装

在Windows系统上安装MySQl数据库是件非常简单的事情,下载压缩包,解压即可。下载地址:http://dev.mysql.com/downloads/mysql/

  • MySQL服务器运行命令:MySQL安装目录/bin/mysqld.exe
  • MySQL客户端运行命令:MySQL安装目录/bin/mysql.exe

B.2 MySQL在Linux Ubuntu中安装

本书使用的Linux是Ubuntu 12.04.2 LTS 64bit的系统,安装MySQL数据库软件包可以通过apt-get实现。首先在Linux Ubuntu中安装MySQL数据库。

~ sudo apt-get install mysql-server    #安装MySQL服务器端

安装过程会弹出提示框,输入root用户的密码,我在这里设置密码为mysql。安装完成后,MySQL服务器会自动启动,我们检查MySQL服务器程序

~ ps -aux|grep mysql   # 检查MySQL服务器系统进程
mysql     3205  2.0  0.5 549896 44092 ?        Ssl  20:10   0:00 /usr/sbin/mysqld
conan     3360  0.0  0.0  11064   928 pts/0    S+   20:10   0:00 grep --color=auto mysql

~ netstat -nlt|grep 3306  # 检查MySQL服务器占用端口
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN

~ sudo /etc/init.d/mysql status  # 通过启动命令检查MySQL服务器状态
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql status

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the status(8) utility, e.g. status mysql
mysql start/running, process 3205

~ service mysql status  # 通过系统服务命令检查MySQL服务器状态
mysql start/running, process 3205

B.3 通过命令行客户端访问MySQL

安装MySQL服务器,会自动地一起安装MySQL命令行客户端程序。在本机输入mysql命令就可以启动,客户端程序访问MySQL服务器。

~ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

Copyright (c) 2000, 2013, 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 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)
mysql>

MySQL的一些简单的命令操作。

# 查看所有的数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)
# 查看数据库的字符集编码
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

B.4 修改MySQL服务器的配置

接下来,我需要做一些配置,让MySQL符合基本的开发要求。

B.4.1 将字符编码设置为UTF-8

默认情况下,MySQL的字符集是latin1,因此在存储中文的时候,会出现乱码的情况,所以我们需要把字符集统一改成UTF-8。用vi打开MySQL服务器的配置文件my.cnf:

~ sudo vi /etc/mysql/my.cnf

[client]   #在[client]标签下,增加客户端的字符编码
default-character-set=utf8

 [mysqld]  #在[mysqld]标签下,增加服务器端的字符编码
character-set-server=utf8
collation-server=utf8_general_ci

B.4.2 让MySQL服务器被远程访问

默认情况下,MySQL服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。用vi打开MySQL服务器的配置文件my.cnf:

~ sudo vi /etc/mysql/my.cnf
#bind-address            = 127.0.0.1    #注释bind-address

修改后,重启MySQL服务器。

~ sudo /etc/init.d/mysql restart

重新登陆服务器

~ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

mysql> show variables like '%char%';   # 再次查看字符串编码
+--------------------------+----------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

我们检查MySQL的网络监听端口

~ netstat -nlt|grep 3306   # 检查MySQL服务器占用端口
  tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN

我们看到网络监听从127.0.0.1:3306变成0 0.0.0.0:3306,表示MySQL已经允许远程登陆访问。 通过上面的操作,我们就把MySQL数据库服务器,在Linux Ubuntu系统中安装完成。

results matching ""

    No results matching ""