MySQL資料庫(一)編譯安裝、安裝後優化操作及超戶忘記資料庫密碼的解決方法
MySQL的下載地址:http://www.dev.mysql.com/downloads
準備工作:解除安裝rpm方式安裝的mysql-server、mysql
rpm -qa | grep mysql
若存在,用rpm -e mysql-server mysql --nodeps命令解除安裝
安裝ncurses-devel與cmake包
yum -y install ncurses-devel
tar xf cmake-2.8.6.tar.gz -C /usr/src/
cd /usr/src/cmake-2.8.6/
./configure && gmake && gmake install
原始碼編譯及安裝:
增加程式使用者mysql:
useradd -M -s /sbin/nologin mysql
解壓mysql-5.5.22.tar.gz:
tar xf mysql-5.5.22.tar.gz -C /usr/src
cmake 配置,編譯及安裝:
cd /usr/src/mysql-5.5.22/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all && make && make install
安裝後優化操作:
修改mysql安裝目錄的屬主和屬組:
chown -R mysql:root /sur/local/mysql/
建立修改my.cnf配置檔案:
/bin/cp support-file/my-medium.cnf /etc/my.cnf
my.cnf配置檔案相關介紹,在mysql解壓路徑support-files/下提供的模板:
my-huge.cnf 巨大資料量
my-innodb-heavy-4G.cnf innodb引擎
my-large.cnf 大資料量
my-medium.cnf 測試使用
my-small.cnf 小資料量
MySQL資料庫儲存引擎:
myisam
innodb
不同的引擎備份方法不同,在備份時一定要先看引擎在備份!!!
新增系統服務:
cp support-files/mysql.server /etc/int.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig mysqld --add
chkconfig mysqld --list
新增MySQL命令執行的路徑到PATH環境變數:
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
. /etc/profile
echo $PATH
執行mysql_install_db指令碼初始化資料庫:
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
啟動mysql服務,並檢視執行狀態:
/etc/init.d/mysqld start
netstat -anpt | grep :3306
連線並登陸到mysql操作環境:
mysqladmin -uroot password'123123'(首次登陸需建立密碼)
退出登陸:
quit、exit、ctrl+D
設定資料庫使用者名稱密碼:
mysqladmin -uroot password'123123';history -c
忘記MySQL超戶密碼的解決方法
方法一:
1.暫停MySQL服務:
/etc/init.d/mysqld stop
2.跳過grant表授權,進入安全模式,並在後臺執行:
mysqld_safe --skip-grant-tables &
jobs(檢視後臺程序)
3.進入安全模式修改密碼:
mysql
use mysql:
update user set Password=password('123123') where user='root';
flush privieges;
4.重啟mysql服務,嘗試用新密碼登入:
/etc/init.d/mysqld restart
mysql -uroot -p123123
方法二:
1.修改配置檔案:
vim /etc/my.cnf
2.重啟:
/etc/init.d/mysqld restart
3.進入mysql修改密碼:
mysql
use mysql;
update user set Password=password('123123') where user='root'
flush privileges;
4.修改/etc/my.cnf配置檔案,把skip_grant_tables行刪掉,重啟MySQL服務:
mysql -uroot -p123123