在Ubuntu中安裝MySQL

前言mysql

安裝MySQL是個老話題,我安裝MySQL服務器已不下百次了,爲了博客文章結構的連貫性,仍是再寫一篇作爲環境基礎,同時也給本身一個備忘。sql

目錄數據庫

  1. MySQL在Windows中安裝
  2. MySQL在Linux Ubuntu中安裝
  3. 經過命令行客戶端訪問MySQL
  4. 修改MySQL服務器的配置
  5. 新建數據庫並設置訪問帳號
  6. 改變數據存儲位置

1. MySQL在Windows中安裝

在Windows系統上安裝MySQl數據庫是件很是簡單的事情,下載壓縮包,解壓便可。下載地址:http://dev.mysql.com/downloads/mysql/ubuntu

  • MySQL服務器運行命令:MySQL安裝目錄/bin/mysqld.exe
  • MySQL客戶端運行命令:MySQL安裝目錄/bin/mysql.exe

2. MySQL在Linux Ubuntu中安裝

本文使用的Linux是Ubuntu 12.04.2 LTS 64bit的系統,安裝MySQL數據庫軟件包能夠經過apt-get實現。安全

在Linux Ubuntu中安裝MySQL數據庫服務器

#安裝MySQL服務器端
~ sudo apt-get install mysql-server

安裝過程會彈出提示框,輸入root用戶的密碼,我在這裏設置密碼爲mysql。網絡

安裝完成後,MySQL服務器會自動啓動,咱們檢查MySQL服務器程序app

# 檢查MySQL服務器系統進程
~ ps -aux|grep 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

# 檢查MySQL服務器佔用端口
~ netstat -nlt|grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN

# 經過啓動命令檢查MySQL服務器狀態
~ sudo /etc/init.d/mysql status
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

# 經過系統服務命令檢查MySQL服務器狀態
~ service mysql status
mysql start/running, process 3205

3. 經過命令行客戶端訪問MySQL

安裝MySQL服務器,會自動地一塊兒安裝MySQL命令行客戶端程序。tcp

在本機輸入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)

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的一些簡單的命令操做。

# 查看全部的數據庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)

# 切換到information_schema庫
mysql> use information_schema
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

# 查看information_schema庫中全部的表
mysql> show tables;
+---------------------------------------+
| Tables_in_information_schema          |
+---------------------------------------+
| CHARACTER_SETS                        |
| COLLATIONS                            |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS                               |
| COLUMN_PRIVILEGES                     |
| ENGINES                               |
| EVENTS                                |
| FILES                                 |
| GLOBAL_STATUS                         |
| GLOBAL_VARIABLES                      |
| KEY_COLUMN_USAGE                      |
| PARAMETERS                            |
| PARTITIONS                            |
| PLUGINS                               |
| PROCESSLIST                           |
| PROFILING                             |
| REFERENTIAL_CONSTRAINTS               |
| ROUTINES                              |
| SCHEMATA                              |
| SCHEMA_PRIVILEGES                     |
| SESSION_STATUS                        |
| SESSION_VARIABLES                     |
| STATISTICS                            |
| TABLES                                |
| TABLESPACES                           |
| TABLE_CONSTRAINTS                     |
| TABLE_PRIVILEGES                      |
| TRIGGERS                              |
| USER_PRIVILEGES                       |
| VIEWS                                 |
| INNODB_BUFFER_PAGE                    |
| INNODB_TRX                            |
| INNODB_BUFFER_POOL_STATS              |
| INNODB_LOCK_WAITS                     |
| INNODB_CMPMEM                         |
| INNODB_CMP                            |
| INNODB_LOCKS                          |
| INNODB_CMPMEM_RESET                   |
| INNODB_CMP_RESET                      |
| INNODB_BUFFER_PAGE_LRU                |
+---------------------------------------+
40 rows in set (0.01 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)

4. 修改MySQL服務器的配置

接下來,我須要作一些配置,讓MySQL符合基本的開發要求。

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

4.2 讓MySQL服務器被遠程訪問

默認狀況下,MySQL服務器不容許遠程訪問,只容許本機訪問,因此咱們須要設置打開遠程訪問的功能。

用vi打開MySQL服務器的配置文件my.cnf

~ sudo vi /etc/mysql/my.cnf

#註釋bind-address
#bind-address            = 127.0.0.1

修改後,重啓MySQL服務器。

~ sudo /etc/init.d/mysql restart
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql restart

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) and then start(8) utilities,
e.g. stop mysql ; start mysql. The restart(8) utility is also available.
mysql start/running, process 3577

從新登錄服務器

~ 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)

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> 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的網絡監聽端口

# 檢查MySQL服務器佔用端口
~ netstat -nlt|grep 3306
  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已經容許遠程登錄訪問。經過root帳號遠程訪問,是很是不安全的操做,所以咱們下一步,將新建一個數據庫,再新建一個用戶進行遠程訪問。

5. 新建數據庫並設置訪問帳號

經過root帳號登錄MySQl服務器

~ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 39
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.

# 新建數據庫abc
mysql> CREATE DATABASE abc;

# 使用數據庫abc
mysql> use abc;
Database changed

# 在數據庫abc中,新建一張表a1
mysql> create table a1(id int primary key,name varchar(32) not null);
Query OK, 0 rows affected (0.05 sec)

# 新建book用戶,密碼爲book,容許book能夠遠程訪問abc數據庫,受權book對abc進行全部數據庫
mysql> GRANT ALL ON abc.* to book@'%' IDENTIFIED BY 'book';
Query OK, 0 rows affected (0.00 sec)

#容許book能夠本地訪問abc數據庫,受權book對abc進行全部數據庫
mysql> GRANT ALL ON abc.* to book@localhost IDENTIFIED BY 'book';
Query OK, 0 rows affected (0.00 sec)

咱們在本地使用book用戶登錄

# 使用book用戶登錄
~ mysql -ubook -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
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.

#進行abc數據庫
mysql> use abc;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

#查看abc數據庫的表
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| a1            |
+---------------+
1 row in set (0.00 sec)

咱們在遠程的另外一臺Linux使用book用戶登錄

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

Copyright (c) 2000, 2012, 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> use abc
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| a1            |
+---------------+
1 row in set (0.00 sec)

5. 改變數據存儲位置

有時候咱們可能還須要改變MySQL數據存儲的位置,一種方法是直接修改配置文件 /etc/mysql/my.cnf,找到datadir屬性修改目錄。

~ vi /etc/mysql/my.cnf

[mysqld]
datadir         = /var/lib/mysql

若是經過這種方法修改,那麼其餘的調用存儲路徑的地方,咱們也都須要進行修改,好比 用到了/usr/bin/mysql_install_db 命令,文件中ldata的屬性也須要修改,關於mysql_install_db 命令的使用能夠參考文章,[MySQL優化]爲MySQL數據文件ibdata1瘦身

還有另外一種修改存儲位置的方法,就是經過Linux系統的軟連(ln -s)接來作的。當咱們新掛載一塊硬盤,中止MySQL服務,而後把/var/lib/mysql目錄移動到新的硬盤存儲,在/var/lib/mysql處創建指定新位置的軟鏈接就好了。

# 中止MySQL服務器
~ /etc/init.d/mysql stop

# 掛載硬盤
~ mount -t ext4 /dev/vdb1 /vdb1

# 創建新存儲目錄
~ mkdir /vdb1/data

# 移動MySQL數據目錄到新目錄
~ mv /var/lib/mysql /vdb1/data/

# 軟鏈接
~ ln -s /vdb1/data/mysql /var/lib/mysql

修改apparmor的別名定義文件

~ vi /etc/apparmor.d/tunables/alias

alias /var/lib/mysql/ -> /vdb1/data/mysql/,

注:若是沒有修改apparmor的配置,MySQL會啓動不了,並一直提示是權限的問題。

# 重啓apparmor服務
~ /etc/init.d/apparmor restart

# 重啓MySQL服務器
~ /etc/init.d/mysql start

這樣就完成了,MySQL數據存儲位置修改。

經過上面的操做,咱們就把MySQL數據庫服務器,在Linux Ubuntu中的系統安裝完成。

相關文章
相關標籤/搜索