CentOS下MYSQL數據庫的安裝

        關於在Centos系統下安裝MYSQL數據庫,網絡上資料有很多,在此主要感謝該文章的博主:http://www.cnblogs.com/zhoulf/archive/2013/01/25/zhoulf.html,因爲相對來說,該篇文章具有很強的操作性。此文將在彙集衆多資料的基礎上,記錄本人安裝MYSQL的全過程。

        一、配置環境與軟件版本:

          1、Centos版本:6.3版32位系統;

          2、CMake版本:cmake-2.8.4.tar.gz;

          3、Mysql版本:mysql-5.5.11.tar.gz;

        二、安裝過程如下:

                該安裝過程採用CMake編譯的方式安裝Mysql,所以需要安裝CMake工具,再安裝Mysql數據庫,在安裝這兩個軟件之前,需要安裝gcc與ncurses-devel。

          1、安裝gcc編譯工具,執行如下命令:

                [root@localhost soft]# yum install gcc-c++

                如果提示安裝:Complete! 表示安裝成功。 

          2、安裝ncurses-devel編譯工具,執行如下命令:

                [root@localhost soft]# yum install ncurses-devel

                如果提示安裝:Complete! 表示安裝成功。

          3、安裝CMake工具,執行軟件解壓命令和剪切到/usr/local目錄下:

                [root@localhost soft]# tar -zxvf cmake-2.8.4.tar.gz

                [root@localhost soft]# mv cmake-2.8.4 /usr/local/cmake

                進入到/usr/local/cmake目錄下進行安裝:

                [root@localhost cmake]# ./configure

                [root@localhost cmake]# make

                [root@localhost cmake]# make install

                將cmake永久加入系統環境變量:

                [root@localhost cmake]# vi /etc/profile

                在/etc/profile文件中添加與設置相應的環境變量:

                export CMAKE_HOME=/usr/local/cmake

                export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin:$CMAKE_HOME/bin

                注:紅色部分的分號不要丟掉。

                使/etc/profile文件立即生效:

                [root@localhost cmake]# . /etc/profile

                注:.與/etc/profile之間有空格。

                查看設置的環境變量是否已生效:

                [root@localhost cmake]# echo $PATH

                顯示結果:

                /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.6.0_30/bin:/usr/local/ant/bin:/root/bin:/usr/java/jdk1.6.0_30/bin

:/usr/local/ant/bin:/usr/local/cmake/bin

                紅色部分顯示我們設置的環境變量已經生效。

          4、安裝Mysql數據庫,執行軟件解壓命令和剪切到/usr/local目錄下:

                [root@localhost soft]# tar -zxvf mysql-5.5.11.tar.gz

                [root@localhost soft]# mv mysql-5.5.11 /usr/local/mysql-5.5.11

                創建mysql的安裝目錄及數據庫存放目錄:

                [root@localhost soft]# mkdir -p /usr/local/mysql

                [root@localhost soft]# mkdir -p /usr/local/mysql/data

                創建mysql用戶及用戶組:

                [root@localhost soft]# groupadd mysql

                [root@localhost soft]# useradd -r -g mysql mysql

                進入mysql-5.5.11主目錄,編譯安裝mysql:

                [root@localhost mysql-5.5.11]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

                > -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \

                > -DDEFAULT_CHARSET=utf8 \

                > -DDEFAULT_COLLATION=utf8_general_ci \

                > -DWITH_MYISAM_STORAGE_ENGINE=1 \

                > -DWITH_INNOBASE_STORAGE_ENGINE=1 \

                > -DWITH_MEMORY_STORAGE_ENGINE=1 \

                > -DWITH_READLINE=1 \

                > -DENABLED_LOCAL_INFILE=1 \

                > -DMYSQL_DATADIR=/usr/local/mysql/data \

                > -DMYSQL_USER=mysql \

                > -DMYSQL_TCP_PORT=3306

                注:“>” 標識符是在行尾鍵入“\”符號時自動產生的。

                [root@localhost mysql-5.5.11]# make

                [root@localhost mysql-5.5.11]# make install

                進入Mysql數據庫的安裝主目錄/usr/local/mysql,設置相關的目錄權限:

                [root@localhost mysql]# chown -R root:mysql .

                [root@localhost mysql]# chown -R mysql:mysql data

                注:把mysql數據庫主目錄中所有文件的所有者設爲root,所屬組爲mysql。

                將mysql的啓動服務添加到系統服務中:

                [root@localhost mysql]# cp support-files/my-medium.cnf /etc/my.cnf

                cp: overwrite `/etc/my.cnf'? y

                創建系統數據庫的表:

                [root@localhost mysql]# 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 localhost.localdomain 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!

                設置MYSQL的環境變量並做生效處理:

                [root@localhost mysql]# vi /root/.bash_profile

                在修改PATH=$PATH:$HOME/bin爲:PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib

                最終如圖:

               

                [root@localhost mysql]# source /root/.bash_profile

                注:紅色部分是文件生效處理,很重要。

                手動啓動Mysql數據庫:

                [root@localhost mysql]# ./bin/mysqld_safe --user=mysql & mysqladmin -u root -p shutdown

                注:啓動MySQL,提示輸入密碼時,不要輸入密碼,直接回車即可

                提示:

mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/usr/local/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/usr/local/mysql/mysql.sock' exists!
[root@localhost mysql]# 140223 21:59:54 mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended

[1]+  Done                    ./bin/mysqld_safe --user=mysql

                將Mysql的啓動服務添加到系統服務中:

                [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql

                啓動Mysql服務:

                [root@localhost mysql]# service mysql start

                最終結果:

Starting MySQL... ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).

                注:這裏是權限問題,先改變權限

                [root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql

                再次啓動Mysql服務:

                [root@localhost mysql]# /etc/init.d/mysql start

Starting MySQL... SUCCESS!

                修改MySQL的root用戶的密碼以及打開遠程連接:

                [root@localhost mysql]# mysql -u root mysql

                進入MySQL數據庫管理系統:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.11-log Source distribution

Copyright (c) 2000, 2010, 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> use mysql;

                Database changed

                mysql> desc user;

                

                mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; 

Query OK, 0 rows affected (0.00 sec)

                mysql> update user set Password = password('admin') where User='root';

Query OK, 5 rows affected (0.01 sec)
Rows matched: 5  Changed: 5  Warnings: 0

                mysql> select Host,User,Password from user where User='root';

                

                mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

                mysql> exit

Bye

                重新登錄:

                [root@localhost mysql]# mysql -u root -p

                  

                從圖中可以看到,成功登錄,至此Mysql數據庫的安裝與權限配置完畢。

        三、驗證數據庫的基本功能:

          1、查看數據庫表:

                mysql> show tables;

                結果如圖:

                 

                結果正常。

        四、問題:

                本人在配置的過程中,一開始先改變了系統的主機名,發現在執行登陸的時候登陸不了,報“鏈接不上 localhost”,但是在取消主機名之後,安裝過程順利,如果有大牛知道這個問題,望不吝賜教。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章