摘要:本文記錄了在CentOS 6.3上,把MySQL從5.5.28升級到5.6.19的過程。
在我做的一個項目中,最近我對生產服務器上的一系列系統軟件進行了升級,包括Git、Nginx、MySQL和PHP。這篇文章講的是升級MySQL的過程,其他軟件的升級,可見下面列出的文章。
在我加入這個項目之前,服務器上的MySQL已經安裝設置好了,我只是正常使用而已。現在過去1年了,應該適當升級服務器上的軟件了。升級這種事情是應當經常做的,倒不是為了追最新版本,而是當正式版本發放出來的時候,應該及時更新, 以便獲得最新的更正、補丁,避免服務器上的漏洞,減少安全隱患。
升級是在今年6月11-12日進行的,到今天才有空做個記錄,便於日後參考。
動手之前,查找了很多資料,因為MySQL要比之前升級的Git和Nginx要復雜多了。我在參考資料中列出了我所能找到作為依據的文檔,可供參考。
我們使用的服務器是阿裡雲的,服務器的操作系統是CentOS 6.3。登錄管理界面,發現共有兩塊物理硬盤,一塊系統盤,一塊數據盤,而我們只使用了系統盤,數據盤一直空著沒用。所以,計劃就變成:
下面的操作需要以超級管理員root的身份登錄服務器進行操作。
首先,查看硬盤使用情況:
# fdisk -l Disk /dev/xvda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Device Boot Start End Blocks Id System /dev/xvda1 * 1 2550 20480000 83 Linux /dev/xvda2 2550 2611 490496 82 Linux swap / Solaris Disk /dev/xvdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 # df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 20G 9.4G 11G 48% / tmpfs 498M 0 498M 0% /dev/shm
可以看到,邏輯盤/dev/xvda1是位於根目錄(/),/dev/xvda2是系統緩存(swap)。第二塊物理硬盤/dev/xvdb的大小是21.5 GB,沒有加載和使用,我們可以把它加載,並把MySQL的數據遷移到第二塊物理硬盤上,把對系統盤和數據庫的硬盤訪問分流,希望這樣一定程度上可以提高訪問MySQL數據庫的性能。
格式化第二塊物理硬盤:
# mke2fs -j /dev/xvdb mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1310720 inodes, 5242880 blocks 262144 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 160 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 24 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
格式化順利完成。
下面,加載第二塊物理硬盤為/data目錄:
# mount /dev/xvdb /data
這個命令沒有反饋,當時我有點兒吃驚和失落。因為這許多命令我也都是第一次從網上查到,第一次使用,對於它們的輸出和結果,都有一種第一次坐過山車般的期待、興奮、好奇和不確定,每一次的輸出對於我都是新的,你不知道翻過面前這個坡之後會出現什麼!
再用下面的命令查看新的邏輯盤:
# df -kh Filesystem Size Used Avail Use% Mounted on /dev/xvda1 20G 9.3G 11G 48% / tmpfs 498M 0 498M 0% /dev/shm /dev/xvdb 20G 173M 19G 1% /data
可以看到/dev/xvdb是第二塊物理硬盤!激動,成功了!
用下面的命令查看加載(mount)的類型:
# mount /dev/xvda1 on / type xfs (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/xvdb on /data type ext3 (rw)
到這裡,第二塊物理硬盤加載成功,下面該yum出場了。
不好意思,yum同學,編劇說你還要等下一幕。
升級MySQL時,又做了兩個計劃:
下面大部分命令都需要在超級管理員root的權限下運行。
用mysqldump來備份所有的數據庫數據:
# mysqldump -u <db管理員用戶名> -p --all-databases > 2014-06-12_all-db.sql
會提示輸入密碼,然後2014-06-11_all-db.sql就是備份數據,如果不想放在當前目錄下,可以在上面的命令行上指定路徑。
當然,也建議你單獨備份每個用戶數據庫:
# mysqldump -u <db管理員用戶名> -p mydb > 2014-06-12_mydb.sql
妥善保存這些非常重要的數據庫備份文件。
用下面的命令查看MySQL的版本:
# mysql --version mysql Version 14.14 Distrib 5.5.28, for Linux (x86_64) using reeadline 5.1
可見,當前的MySQL的版本是5.5.28。
這是參照參考資料[7]來做的,後來發現[6]的方法更好:
# wget http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm # rpm -ivh mysql-community-release-el6-5.noarch.rpm # yum list | grep mysql
這樣很多更新的MySQL安裝包就都有了。
如有必要,運行下面的命令確保yum安裝庫是最新的:
# yum update mysql-community-release
用下面的命令查看安裝了什麼:
# yum list installed | grep ^mysql mysql-community-release.noarch
其中,^mysql是指以mysql開頭的安裝包。
用下面的命令查看安裝了的和可用的安裝包:
# yum info mysql-server Installed Packages Name : MySQL-Server Arch : x86_64 Version : 5.5.28 Release : 1.linux2.6 Repo : installed Available Packages Name : mysql-server Arch : x86_64 Version : 5.1.73 Release : 3.el6_5 Repo : updates
這裡不知道為啥可用的安裝包沒有顯示MySQL 5.6的。
檢查可用的安裝包:
# yum check-update mysql-server Obsoleting Packages mysql-community-client.x86_64 5.6.19-2.el6 mysql56-community MySQL-client.x86_64 5.5.28-1.linux2.6 installed mysql-community-devel.x86_64 5.6.19-2.el6 mysql56-community MySQL-devel.x86_64 5.5.28-1.linux2.6 installed mysql-community-server.x86_64 5.6.19-2.el6 mysql56-community MySQL-server.x86_64 5.5.28-1.linux2.6 installed
可見,MySQL 5.6的安裝包都算community版本了。
運行下面的命令:
# yum update mysql-server Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mysql-community-client x86_64 5.6.19-2.el6 mysql56-community 18 M replacing MySQL-client.x86_64 5.5.28-1.linux2.6 mysql-community-server x86_64 5.6.19-2.el6 mysql56-community 52 M replacing MySQL-server.x86_64 5.5.28-1.linux2.6 Installing for dependencies: mysql-community-common x86_64 5.6.19-2.el6 mysql56-community 298 k mysql-community-libs x86_64 5.6.19-2.el6 mysql56-community 1.9 M Transaction Summary ================================================================================ Install 4 Package(s)
後面的輸出省略了。可見有4個安裝包(共70多MB)要下載、安裝。結果,下載用了80分鐘。
但安裝遇到問題了:
Running rpm_check_debug Running Transaction Test Transaction Check Error: file /usr/bin/mysql_config from install of mysql-community-client-5.6.19-2.el6 .x86_64 conflicts with file from package MySQL-devel-5.5.28-1.linux2.6.x86)64 Error Summary -------------
可以看到出錯了,搜索了一陣兒,也沒搞明白。隨即停止A計劃(和成龍的電影沒關系),改用B計劃。
可以預料,這個計劃要多折騰一些。仍然需要在超級管理員root的權限下運行下面的命令。
這時為了避免用戶訪問網站時,看到數據庫連接失敗這樣的錯誤信息。到這時我才想起這一點來,還是對用戶關懷不夠,應該在有可能宕掉網站的操作之前就做的。
因為使用的是Nginx,所以運行下面的命令:
# service nginx stop
Stopping nginx: [ OK ]
一共有3個包要卸載。
先卸載mysql-server包:
# yum remove mysql mysql-server Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Removing: MySQL-server x86_64 5.5.28-1.linux2.6 installed 153 M Transaction Summary ================================================================================ Remove 1 Package(s) Installed size: 153 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Erasing : MySQL-server-5.5.28-1.linux2.6.x86_64 1/1 Verifying : MySQL-server-5.5.28-1.linux2.6.x86_64 1/1 Removed: MySQL-server.x86_64 0:5.5.28-1.linux2.6 Complete!
再卸載mysql-client:
# yum remove mysql-client Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Removing: MySQL-client x86_64 5.5.28-1.linux2.6 installed 57 M Transaction Summary ================================================================================ Remove 1 Package(s) Installed size: 57 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : MySQL-client-5.5.28-1.linux2.6.x86_64 1/1 Verifying : MySQL-client-5.5.28-1.linux2.6.x86_64 1/1 Removed: MySQL-client.x86_64 0:5.5.28-1.linux2.6 Complete! View Code最後卸載mysql-devel:
# yum remove mysql-devel Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Removing: MySQL-devel x86_64 5.5.28-1.linux2.6 installed 20 M Transaction Summary ================================================================================ Remove 1 Package(s) Installed size: 20 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : MySQL-devel-5.5.28-1.linux2.6.x86_64 1/1 Verifying : MySQL-devel-5.5.28-1.linux2.6.x86_64 1/1 Removed: MySQL-devel.x86_64 0:5.5.28-1.linux2.6 Complete! View Code3個包的卸載都順利完成。
安裝比卸載更簡單,因為yum會自動檢查依賴安裝包:
# yum install mysql-server Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mysql-community-server x86_64 5.6.19-2.el6 mysql56-community 52 M Installing for dependencies: mysql-community-client x86_64 5.6.19-2.el6 mysql56-community 18 M mysql-community-common x86_64 5.6.19-2.el6 mysql56-community 298 k mysql-community-libs x86_64 5.6.19-2.el6 mysql56-community 1.9 M Transaction Summary ================================================================================ Install 4 Package(s) Total size: 72 M Installed size: 323 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : mysql-community-common-5.6.19-2.el6.x86_64 1/4 Installing : mysql-community-libs-5.6.19-2.el6.x86_64 2/4 Installing : mysql-community-client-5.6.19-2.el6.x86_64 3/4 Installing : mysql-community-server-5.6.19-2.el6.x86_64 4/4 warning: /etc/my.cnf created as /etc/my.cnf.rpmnew /bin/chmod: cannot access `': No such file or directory Verifying : mysql-community-common-5.6.19-2.el6.x86_64 1/4 Verifying : mysql-community-libs-5.6.19-2.el6.x86_64 2/4 Verifying : mysql-community-client-5.6.19-2.el6.x86_64 3/4 Verifying : mysql-community-server-5.6.19-2.el6.x86_64 4/4 Installed: mysql-community-server.x86_64 0:5.6.19-2.el6 Dependency Installed: mysql-community-client.x86_64 0:5.6.19-2.el6 mysql-community-common.x86_64 0:5.6.19-2.el6 mysql-community-libs.x86_64 0:5.6.19-2.el6 Complete!
由於篇幅關系,輸出有所縮略。一切順利,繼續!
預告:繁瑣的事情這才開始。
更改\etc\my.cnf,下面只列出更改的相關設置:
[mysqld] # Uncomment the following if you are using InnoDB tables #innodb_data_home_dir = /var/lib/mysql # data path changed to 2nd physical hard disk /dev/xvdb after upgrading from MySQL 5.5 to MySQL 5.6 innodb_data_home_dir = /data/mysql #innodb_data_file_path = ibdata1:10M:autoextend #innodb_log_group_home_dir = /var/lib/mysql datadir = /data/mysql
如果你也更改了數據目錄,最後一行的 datadir = /data/mysql 切不可遺漏,我就是因為這一行實際上沒有及時改,還是指向舊的數據目錄,導致數據升級失敗,直到升級數據之後才發現,在下文中有詳細論述。
啟動服務:
# service mysql start mysql: unrecognized service
居然出錯了!只好直接運行MySQL的daemon:
# /etc/init.d/mysqld start Starting mysqld: [ OK ]
後來才知道,mysql是MySQL 5.5的服務名稱,到了MySQL 5.6服務就改名為mysqld了。所以,其實這裡可以運行service mysqld start的。
按照參考資料[6],到這時,MySQL的程序已經升級完畢,要升級數據了。
先把舊數據目錄下的所有文件都拷貝到新的數據目錄下。可惜,這裡的命令在我的工作日志中沒有記錄,日後找到再不上。不過這是基本的Linux命令,就算你不知道,也一定能在網上找到。
然後運行下面的命令:
# mysql_upgrade -uroot -p Enter password: Looking for 'mysql' as: mysql Looking for 'mysqlcheck' as: mysqlcheck Running 'mysqlcheck' with connection arguments: '--port=<為安全故略去>' '--socket=/var/lib/mysql/mysql.sock' Warning: Using a password on the command line interface can be insecure. Running 'mysqlcheck' with connection arguments: '--port=<為安全故略去>' '--socket=/var/lib/mysql/mysql.sock' Warning: Using a password on the command line interface can be insecure. mysql.columns_priv OK mysql.db OK mysql.event OK mysql.func OK # ... ... 為省略篇幅故略去 ... ... mysql.time_zone_transition OK mysql.time_zone_transition_type OK mysql.user OK Running 'mysql_fix_privilege_tables'... Warning: Using a password on the command line interface can be insecure. Running 'mysqlcheck' with connection arguments: '--port=<為安全故略去>' '--socket=/var/lib/mysql/mysql.sock' Warning: Using a password on the command line interface can be insecure. Running 'mysqlcheck' with connection arguments: '--port=<為安全故略去>' '--socket=/var/lib/mysql/mysql.sock' Warning: Using a password on the command line interface can be insecure. mydb.table1 OK mydb.table2 OK # ... ... 為省略篇幅故略去 ... ... mydb.table793 OK mydb.table794 OK OK
到這裡發現,數據庫有問題,數據升級並未成功。原因就是之前提及的,升級數據時\etc\my.cnf中的數據目錄(即datadir = 這一行)還是指向舊的數據目錄,我猜想這導致一些操作發生了混亂。
這時我感覺到難免的慌張和混亂,之後我做了一些嘗試:
不得不就此作罷。
看起來由於我之前的疏忽,數據庫已經在上一步升級數據中被損毀了!
這時候,你能做什麼!谷歌(或者百度)就是你當前最好的朋友!
在冒汗和揪頭發的折磨中,總算找到了根救命稻草mysql_install_db,詳見參考資料[9],這個命令可以重新創建數據目錄,這就意味著重新創建系統數據庫mysql,也意味著現有的用戶數據庫都將被清除。這時,你會感謝上帝、佛祖、聖母和玉皇大帝,如果你在最開始對所有的數據庫做了備份的話。回頭看一下我們在5.1 備份所有的數據庫做的事情,就知道這是多麼重要了!有了備份,我們就可以恢復所有的數據庫了!
先要做的,是在MySQL的配置文件中設置正確的數據目錄。再次更改\etc\my.cnf,下面只列出更改的相關設置:
[mysqld] datadir = /data/mysql
然後,祭出我們的救命稻草mysql_install_db來重新創建數據目錄(如果你想知道該命令的詳細參數,可見參考資料[9]):
# /usr/bin/mysql_install_db --user=mysql --datadir=/data/mysql Installing MySQL system tables...2014-06-13 00:35:17 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2014-06-13 00:35:17 11574 [Note] InnoDB: Using atomics to ref count buffer pool pages 2014-06-13 00:35:17 11574 [Note] InnoDB: The InnoDB memory heap is disabled 2014-06-13 00:35:17 11574 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2014-06-13 00:35:17 11574 [Note] InnoDB: Compressed tables use zlib 1.2.3 2014-06-13 00:35:17 11574 [Note] InnoDB: Using Linux native AIO 2014-06-13 00:35:17 11574 [Note] InnoDB: Using CPU crc32 instructions 2014-06-13 00:35:17 11574 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2014-06-13 00:35:17 11574 [Note] InnoDB: Completed initialization of buffer pool 2014-06-13 00:35:17 11574 [Note] InnoDB: Highest supported file format is Barracuda. 2014-06-13 00:35:17 11574 [Note] InnoDB: 128 rollback segment(s) are active. 2014-06-13 00:35:17 11574 [Note] InnoDB: Waiting for purge to start 2014-06-13 00:35:17 11574 [Note] InnoDB: 5.6.19 started; log sequence number 44199093 2014-06-13 00:35:17 11574 [Warning] InnoDB: Cannot open table mysql/innodb_table_stats from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem. ERROR: 1146 Table 'mysql.innodb_table_stats' doesn't exist 2014-06-13 00:35:17 11574 [ERROR] Aborting 2014-06-13 00:35:17 11574 [Note] Binlog end 2014-06-13 00:35:18 11574 [Note] InnoDB: FTS optimize thread exiting. 2014-06-13 00:35:18 11574 [Note] InnoDB: Starting shutdown... 2014-06-13 00:35:19 11574 [Note] InnoDB: Shutdown completed; log sequence number 44199103 2014-06-13 00:35:19 11574 [Note] /usr/sbin/mysqld: Shutdown complete
又出錯了!別慌,這是因為數據目錄並未清空,其中還有之前損毀的數據庫文件。
刪除當前的數據目錄。我實際做的是把 /data/mysql 改名為 /data/mysql.bak2,效果一樣,/data/mysql 目錄不存在了,只是我做了個物理文件備份。
再次重建數據目錄:
# /usr/bin/mysql_install_db --user=mysql --datadir=/data/mysql Installing MySQL system tables...2014-06-13 00:46:06 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2014-06-13 00:46:06 11654 [Note] InnoDB: Using atomics to ref count buffer pool pages 2014-06-13 00:46:06 11654 [Note] InnoDB: The InnoDB memory heap is disabled 2014-06-13 00:46:06 11654 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2014-06-13 00:46:06 11654 [Note] InnoDB: Compressed tables use zlib 1.2.3 2014-06-13 00:46:06 11654 [Note] InnoDB: Using Linux native AIO 2014-06-13 00:46:06 11654 [Note] InnoDB: Using CPU crc32 instructions 2014-06-13 00:46:06 11654 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2014-06-13 00:46:06 11654 [Note] InnoDB: Completed initialization of buffer pool 2014-06-13 00:46:06 11654 [Note] InnoDB: The first specified data file /data/mysql/ibdata1 did not exist: a new database to be created! 2014-06-13 00:46:06 11654 [Note] InnoDB: Setting file /data/mysql/ibdata1 size to 12 MB 2014-06-13 00:46:06 11654 [Note] InnoDB: Database physically writes the file full: wait... 2014-06-13 00:46:06 11654 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB 2014-06-13 00:46:07 11654 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB 2014-06-13 00:46:09 11654 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 2014-06-13 00:46:09 11654 [Warning] InnoDB: New log files created, LSN=45781 2014-06-13 00:46:09 11654 [Note] InnoDB: Doublewrite buffer not found: creating new 2014-06-13 00:46:09 11654 [Note] InnoDB: Doublewrite buffer created 2014-06-13 00:46:09 11654 [Note] InnoDB: 128 rollback segment(s) are active. 2014-06-13 00:46:09 11654 [Warning] InnoDB: Creating foreign key constraint system tables. 2014-06-13 00:46:09 11654 [Note] InnoDB: Foreign key constraint system tables created 2014-06-13 00:46:09 11654 [Note] InnoDB: Creating tablespace and datafile system tables. 2014-06-13 00:46:09 11654 [Note] InnoDB: Tablespace and datafile system tables created. 2014-06-13 00:46:09 11654 [Note] InnoDB: Waiting for purge to start 2014-06-13 00:46:09 11654 [Note] InnoDB: 5.6.19 started; log sequence number 0 2014-06-13 00:46:10 11654 [Note] Binlog end 2014-06-13 00:46:10 11654 [Note] InnoDB: FTS optimize thread exiting. 2014-06-13 00:46:10 11654 [Note] InnoDB: Starting shutdown... 2014-06-13 00:46:12 11654 [Note] InnoDB: Shutdown completed; log sequence number 1625977 OK Filling help tables...2014-06-13 00:46:12 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2014-06-13 00:46:12 11678 [Note] InnoDB: Using atomics to ref count buffer pool pages 2014-06-13 00:46:12 11678 [Note] InnoDB: The InnoDB memory heap is disabled 2014-06-13 00:46:12 11678 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2014-06-13 00:46:12 11678 [Note] InnoDB: Compressed tables use zlib 1.2.3 2014-06-13 00:46:12 11678 [Note] InnoDB: Using Linux native AIO 2014-06-13 00:46:12 11678 [Note] InnoDB: Using CPU crc32 instructions 2014-06-13 00:46:12 11678 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2014-06-13 00:46:12 11678 [Note] InnoDB: Completed initialization of buffer pool 2014-06-13 00:46:12 11678 [Note] InnoDB: Highest supported file format is Barracuda. 2014-06-13 00:46:12 11678 [Note] InnoDB: 128 rollback segment(s) are active. 2014-06-13 00:46:12 11678 [Note] InnoDB: Waiting for purge to start 2014-06-13 00:46:12 11678 [Note] InnoDB: 5.6.19 started; log sequence number 1625977 2014-06-13 00:46:12 11678 [Note] Binlog end 2014-06-13 00:46:12 11678 [Note] InnoDB: FTS optimize thread exiting. 2014-06-13 00:46:12 11678 [Note] InnoDB: Starting shutdown... 2014-06-13 00:46:14 11678 [Note] InnoDB: Shutdown completed; log sequence number 1625987 OK
後面還有一些輸出,只是一些說明,關於如何准備MySQL的服務、如何設置管理員密碼、等等。
好了,到這裡,看起來總算恢復都正常了。
繼續,設置管理員密碼:
# mysqladmin -u root password '********'
這時,只是重建了系統數據庫mysql,原先的用戶數據庫都沒了。要先創建(空的)用戶數據庫,然後從備份恢復。
進入MySQL命令行:
# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.6.19 MySQL Community Server (GPL)
再創建用戶數據庫:
mysql> CREATE DATABASE `mydb`; Query OK, 1 row affected (0.00 sec) mysql> exit Bye
這時,mydb數據庫還是空的。從之前的數據庫備份恢復mydb:
# mysql -u root -p mydb < 2014-06-12_mydb.sql Enter password:
用戶數據庫順利恢復。
開啟mysqld服務的開機啟動狀態:
# chkconfig mysqld on
再查看:
# chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
正確!繼續。
對應於6.1 停止web服務器,我們要再次開啟網站、允許用戶訪問了:
# service nginx start Starting nginx: [ OK ]
測試網站,一切正常!
其實網站還有點兒小問題,不過這和MySQL升級無關,只是作為完整的實際過程,記錄在這裡。
實際上這時還是無法訪問網站的,訪問網站主頁時遇到下面的錯誤:
2014-06-13 01:07:23 Warning: Warning (2): mysql_connect() [<a href='http://php.net/function.mysql-connect'>function.mysql-connect</a>]: No such file or directory in [/usr/share/nginx/.../cake/libs/model/datasources/dbo/dbo_mysql.php, line 561]
根據參考資料[11],修改CakePHP 1.3代碼中的app/config/database.php:
class DATABASE_CONFIG { public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'username', 'password' => 'password', 'database' => 'mydb', 'prefix' => '' ,'encoding' => 'utf8' ,'port' => '/data/mysql/mysql.sock' );
如上所示,增加了port的設置。注意,這只適用於*nix系統,Windows上是不需要這樣的。另,CakePHP 2.x的設置與此略有不同,請參考相關文檔。
這樣網站就能正常訪問了。
源碼編譯咯,當然注意數據備份,官方有這方面的文檔的
附:修改數據文件存儲
1、停止服務;
2、移動MySQL數據文件
[root@localhost ~]# mv /var/lib/mysql /home/
3、修改my.cnf配置
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
port = 3306
#socket = /var/lib/mysql/mysql.sock(原內容,為了更穩妥用“#”注釋此行)
socket = /home/mysql/mysql.sock
4、修改啟動腳本
[root@localhost ~]# vi /etc/init.d/mysql
#datadir=/var/lib/mysql(注釋原內容)
datadir=/home/data/mysql
5、重啟服務如啟動失敗,檢查目錄/文件權限。