Linux下安裝MySQL

來源:互聯網
上載者:User

安裝環境:Linux伺服器CentOS 5.5

安裝版本:mysql-5.5.8.tar.gz

1、安裝 cmake 編譯器。

1)、下載cmake

#cd /usr/local/src

#wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz

2)、解壓cmake

#tar -zvxf cmake-2.8.4.tar.gz

3)、配置編譯

#cd cmake-2.8.4
#yum -y install gcc
#yum -y install gcc-c++
#yum -y install ncurses-devel
#./configure
#make
#make install

2、安裝MySQL

1)、下載MySQL。

#cd /usr/local/src

#wget http://sdk.ruiya.com/linux/mysql-5.5.9.tar.gz

2)、添加必要的組和擁有者

#groupadd mysql

#useradd -r -g mysql mysql

3)、解壓MySQL

#tar -zvxf mysql-5.5.9.tar.gz

4)、配置編譯

如果是重裝MySql,請先刪除my.cnf如: rm -rf /etc/my.cnf

#mkdir /usr/local/mysql

#mkdir /usr/local/mysql/data

#cd /usr/local/src/mysql-5.5.9

#cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DINSTALL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1

參數說明:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql        //安裝目錄
-DINSTALL_DATADIR=/usr/local/mysql/data         //資料庫存放目錄
-DDEFAULT_CHARSET=utf8                        //使用utf8字元
-DDEFAULT_COLLATION=utf8_general_ci            //校正字元
-DEXTRA_CHARSETS=all                            //安裝所有擴充字元集
-DENABLED_LOCAL_INFILE=1                        //允許從本地匯入資料

#make

#make install

注意事項:

重新編譯時間,需要清除舊的對象檔案和緩衝資訊。 

# make clean
# rm -f  CMakeCache.txt
# rm -rf /etc/my.cnf 

4)、設定目錄許可權

# cd /usr/local/mysql

# chown -R root:mysql . //把目前的目錄中所有檔案的所有者所有者設為root,所屬組為mysql

# chown -R mysql:mysql data
 

5)、設定檔

# cp support-files/my-medium.cnf /etc/my.cnf //這個配置僅適合小記憶體系統(32M - 64M)

開啟如下注釋

innodb_data_home_dir = /usr/local/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/data

innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M

innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

添加預設字元集

[client]
default-character-set = utf8    // 添加編碼支援
[mysqld]
default-character-set = utf8   // 添加編碼支援
max_connections = 10000     //根據伺服器效能調節
basedir = /usr/local/mysql //設定安裝目錄,這樣在系統啟動時才能正確運行到/etc/rc.d/init.d/mysql start

6)、建立系統資料庫的表

# cd /usr/local/mysql

# scripts/mysql_install_db --user=mysql

7)、設定許可權啟動

設定環境變數

# vi /root/.bash_profile

在PATH=$PATH:$HOME/bin添加參數為:

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib

#source /root/.bash_profile

手動啟動MySQL: 

# cd /usr/local/mysql

# ./bin/mysqld_safe --user=mysql &   //啟動MySQL,但不能停止

開機記錄寫在此檔案下:/usr/local/mysql/data/localhost.err

關閉MySQL服務

# mysqladmin -u root -p shutdown  //這裡MySQL的root使用者還沒有配置密碼,所以為空白值。

通過指令碼啟動MySQL

# ln -s /usr/local/mysql/support-files/mysql.server /usr/local/mysql
//必須注意,是放在mysql目錄下,不是bin目錄下
# cp /usr/local/mysql/support-files/mysql.server /usr/local/mysql  
# mysql.server start //啟動mysql
# mysql.server stop //停止mysql

在引導時啟動MySQL

# ln -s /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql

# ln -s /usr/local/mysql/mysql.server /etc/rc.d/init.d/mysql 
# cd /etc/rc.d/init.d
# chkconfig --add mysql       //配置是否自動啟動, chkconfig --del mysql 可刪除
# chmod +x /etc/rc.d/init.d/mysql    //添加如執行許可權

Tips

Linux運行層級:
分成了8種運行層級,其中常用7種。可在/etc/inittab檔案中設定。
0 - halt
1 - Single user mode
2 - Multiuser, without NFS
3 - Full multiuser mode
4 - unused
5 - x11
6 - reboot
預設設定為:id:3:initdefault:
每一種動行層級都有自已獨立的檔案夾,例如:
/etc/rc.d/rc3.d 表示運行層級為3的配置都存放在這個檔案俠中。

# chkconfig --list |grep mysql      //檢查看是否設定為自啟動
mysql 0:關閉 1:關閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關閉
表示: 運行層級2、3、4、5都會自動啟動mysql

另一種手動設定自已啟動

可能會出現這種情況,如果你試圖在/etc/rc.d/rc3.d目錄下運行../init.d/mysql start可能會收到如下錯誤

Starting MySQLCouldn't find MySQL server (./bin/mysqld_safe[失敗]

可見mysql.server內部引用了一個相對路徑./bin/mysqld_safe,所以這樣就導致失敗。

這樣我們可以直接在rc.local檔案中添加啟動指令碼:

# chkconfig --del mysql

# cd /etc/rc.d

# vi rc.local //添加: /usr/local/mysql/bin/mysqld_safe --user=mysql &

解決辦法:在/etc/my.cnf 設定檔中添加:

basedir = /usr/local/mysql

8)、修改MySQL的root使用者的密碼

# mysql -u root mysql

mysql>use mysql;
mysql>desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";  //為root添加遠端連線的能力。
mysql>update user set Password = password('xxxxxx') where User='root';
mysql>select Host,User,Password  from user where User='root';
mysql>flush privileges;
mysql>exit

重新登入:mysql -u root -p

9)、添加軟連結

# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
# ln -s /usr/local/mysql/include/mysql /usr/include/mysql

注意事項:

<1>、MySQL5.5 預設使用InnoDB作為儲存引擎,所以可以不設定DWITH_MYISAM_STORAGE_ENGINE值

參考:

如何開啟MySQL中root賬戶的遠程登入 

mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";
注意%對應的密碼,如果不相同,可以通過 update user set Password = password('xxxxxx') where User='root'; 修改。

同時注意防火牆是否已關閉或者添加例外。

# /etc/rc.d/init.d/iptables stop

關閉或開啟Linux/CentOS上的防火牆 

匯出資料庫產生SQL指令碼
mysqldump -h 192.168.200.18  -u root -p TestDB > TestDB.sql

<2>、測試mysql守護進程。

#cd /usr/local/mysql/mysql-test ;

#perl mysql-test-run.pl

<3>、注意事項:

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:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h localhost password 'new-password'

Alternatively you can run:
/usr/local/mysql/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 /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

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

Please report any problems with the /usr/local/mysql/scripts/mysqlbug script!

參考:

mysql5.5.8安裝問題解決方案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.