ubuntu12.04安裝mysql

本人博客開始遷移,博客整個架構本身搭建及編碼 轉自:http://www.cookqq.com/listBlog.action java

Linux下安裝MySQL有三種方式: mysql

第一種以rpm的二進制文件分個安裝, linux

第二種是本身編譯源碼後安裝, sql

第三種是以二進制tar.gz文件來安裝 數據庫

因爲最後一種是統一的總體文件,故本文將採用此方式來進行安裝
ubuntu

一、首先到mysql的下載中心上下載最新的tar.gz包,網站:http://www.mysql.com/downloads/
安全

二、下載後獲得文件mysql-5.6.10-linux-glibc2.5-i686.tar.gz,而後將其解壓,並重命名爲mysql,使用mv命令將其移到/usr/local目錄下 架構

sudo mv ~/下載/mysql  /usr/local

提示:其中文本文件INSTALL-BINARY詳細的記錄了mysql在Linux下的安裝方法,英文好的同鞋能夠直接的查看 socket


三、mysql默認的安裝目錄就是在/usr/local/mysql,這就是上面爲何咱們要將其移動在/uer/local下的緣由;若是在你的機器上之前安裝有老闆本的mysql,須要先將它的文件刪除,同時注意刪除老闆本的etc/my.cnf文件和/etc/mysql目錄,這兩個文件控制的是mysql的一些配置屬性。
ide

四、先要建立的一個名爲mysql的用戶組和用戶,來承載mysql數據庫的運行,使用以下命令:

建立用戶組:

sudo groupadd mysql

在建立的用戶組中建立一個用戶:

sudo useradd -r -g mysql mysql

這裏使用sudo命令是確保以root權限執行此命令,若是你登入本機的用戶是root用戶,則直接的使用groupadd和useradd命令

題外話:對應刪除用戶組及用戶的命令是groupdel和userdel。

五、接着進入mysql目錄,修改mysql目錄的擁有者,爲mysql用戶:

cd /usr/local/mysql
sudo chown -R mysql .
sudo chgrp -R mysql .

這裏的點「.」表明的就是當前目錄,選項-R表示遞歸當前目錄及其子目錄

六、安裝mysql,執行命令:

sudo scripts/mysql_install_db --user=mysql

正確輸出:

root@tianbaoxing-virtual-machine:/usr/local/mysql# sudo scripts/mysql_install_db --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h tianbaoxing-virtual-machine password 'new-password'

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!


注意:在Ubuntu 12.04下安裝mysql 5.5.28版本執行此命令時,會提示以下錯誤的信息:


root@tianbaoxing-virtual-machine:/usr/local/mysql# sudo scripts/mysql_install_db  --user=mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: 
libaio.so.1: cannot open shared object file: No such file or directory

這說明還要安裝一個libaio的依賴庫,執行以下命令:

sudo apt-get install libaio-dev

當我執行上面的命令有報錯錯誤2

root@tianbaoxing-virtual-machine:/usr/local/mysql# sudo apt-get install libaio-dev

正在讀取軟件包列表... 有錯誤!
E: Encountered a section with no Package: header
E: Problem with MergeList /var/lib/apt/lists/cn.archive.ubuntu.com_ubuntu_dists_precise_main_i18n_Translation-en
E: 沒法解析或打開軟件包的列表或是狀態文件。

解決:

提示的是上面的mirror站點的packages入口文件沒法解析或打開.

我乾脆就直接把lists文件夾裏的文件所有刪除掉, 而後再update一下就行了.

sudo rm -rf /var/lib/apt/lists/*
sudo apt-get update


7/執行完上面的命令後,其實就已經完成了mysql的安裝,但爲了數據庫的安全,能夠將mysql目錄的擁有者改成root用戶,並將生成的系統依賴數據賦給mysql用戶,執行以下命令:

chown -R root .

chown -R mysql data

8/安裝好mysql後,就能夠試着啓動它,使用以下命令:

sudo ./support-files/mysql.server start

一樣重啓和中止,只須要將上面命令的start改成restart或stop。

9/啓動完mysql後,使用「./bin/mysql」命令來進入mysql數據庫的控制檯,執行SQL命令

sudo show databases;

結果:

mysql>  show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql>

十、修改mysql密碼:

數據庫初始密碼是空的,設置密碼:

sudo ./bin/mysqladmin -u root -p password 'root'
Enter password: 
root@tianbaoxing-virtual-machine:/usr/local/mysql#

設置這個密碼命令,費了好大的力氣,最後仍是查看安裝mysql後輸出的提示命令。

11/查看mysql版本:

root@tianbaoxing-virtual-machine:/usr/local/mysql# ./bin/mysqladmin -u root -p version
Enter password: 
./bin/mysqladmin  Ver 8.42 Distrib 5.5.28, for linux2.6 on i686
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.

Server version		5.5.28
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/tmp/mysql.sock
Uptime:			9 min 30 sec

Threads: 1  Questions: 42  Slow queries: 0  Opens: 48  Flush tables: 1  Open tables: 41  Queries per second avg: 0.073
或者 在終端機使用指令 mysql --version 或者登錄 mysql 用戶端後執行  select version();

12/ 容許root遠程登錄:

    1)本機登錄mysql:mysql -u root -p (-p必定要有);改變數據庫:use mysql;

    2)從全部主機:grant all privileges on *.* to root@"%" identified by "passw0rd" with grant option;

    3)從指定主機:grant all privileges on *.* to root@"192.168.1.101" identified by "passw0rd" with grant option; flush privileges;

    4)  進mysql庫查看host爲%的數據是否添加:use mysql; select * from user;

打算指定特定主機登錄,根據第三條設置的,結果連接的時候報錯

ERROR 1130 (HY000): Host '192.168.67.1' is not allowed to connect to this MySQL s
erver

而虛擬機的地址是192.168.67.23,怎麼報鏈接不上192.168.67.1呢?應該仍是權限沒有設置好,我試了試:

grant all privileges on *.* to root@"192.168.67.1" identified by "root" with grant option; flush privileges

就行了,具體爲何,我沒有想明白

相關文章
相關標籤/搜索