MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),目前屬于 Oracle 旗下產(chǎn)品。MySQL 最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),在 WEB 應(yīng)用方面MySQL是最好的 應(yīng)用軟件之一.
創(chuàng)新互聯(lián)建站專注于網(wǎng)站建設(shè)|網(wǎng)站維護(hù)|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計服務(wù),案例作品覆蓋木包裝箱等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身策劃品質(zhì)網(wǎng)站。
mysql 數(shù)據(jù)庫安裝方式有多種,本文介紹源碼安裝
系統(tǒng)環(huán)境:
Linux mysql 2.6.32-642.6.1.el6.x86_64 #1 SMP Wed Oct 5 00:36:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@mysql ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
創(chuàng)建用戶和組
groupadd mysql
useradd mysql -g mysql -M -s /sbin/nologin
[root@mysql ~]# id mysql
uid=502(mysql) gid=502(mysql) 組=502(mysql)
2.解壓源碼并編譯安裝
tar xf mysql-5.1.72.tar.gz
cd mysql-5.1.72
#編譯
./configure \
--prefix=/application/mysql5.1.72 \
--with-unix-socket-path=/application/mysql5.1.72/tmp/mysql.sock \
--localstatedir=/application/mysql5.1.72/data \
--enable-assembler \
--enable-thread-safe-client \
--with-mysqld-user=mysql \
--with-big-tables \
--without-debug \
--with-pthread \
--enable-assembler \
--with-extra-charsets=complex \
--with-readline \
--with-ssl \
--with-embedded-server \
--enable-local-infile \
--with-plugins=partition,innobase \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static
make && make install
3.做軟鏈接
ln -s /application/mysql5.1.72/ /application/mysql
ll /application/mysql
-------------------------------
重要:如果mysql不是和apache部署在同一臺電腦上,到此結(jié)束。
-------------------------------
4.數(shù)據(jù)庫的初始化
cd /tools/mysql-5.1.72
ls support-files/my*.cnf #顯示mysql 配置文件
cd support-files
cp my-small.cnf /etc/my.cnf #copy 配置文件
mkdir -p /application/mysql/data # 建立mysql數(shù)據(jù)文件存放目錄
chown -R mysql:mysql /application/mysql #授權(quán)Mysql 用戶能訪問mysql 的安裝目錄
ll /application/mysql/data
/application/mysql/bin/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
#出現(xiàn)2個OK,表示成功
ll /application/mysql/data
#######################################
------------------------------------------------
vim /etc/init.d/mysqld
basedir=/application/mysql
datadir=/application/mysql/data
------------------------------------------------
#######################################
5.copy啟動文件
cd /tools/mysql-5.1.72/support-files
cp mysql.server /etc/init.d/mysqld
6.加入開機(jī)自啟動
chkconfig mysqld on
啟動mysql數(shù)據(jù)庫
/etc/init.d/mysqld start (/application/mysql/bin/mysqld_safe & 這個命令也可以啟動mysql)
如果啟動失敗則執(zhí)行下面命令:
ps -ef |grep mysql #查看Mysql 進(jìn)程是否在運(yùn)行,如果在的話,啟動前要Kill 或者 mysqladmin shutdown
然后再啟動mysql
[root@mysql ~]#mysql
出現(xiàn)下面錯誤:
#-bash:mysql:command not found
mysql 對應(yīng)的路徑不在path目錄下面
使用全路徑進(jìn)入mysql:
/application/mysql/bin/mysql
或者在文件最后加上:
vi /etc/profile
PATH="/application/mysql/bin/:$PATH"
[root@mysql ~]#. /etc/profile #使之生效
#設(shè)置密碼,不要在mysql> 下面設(shè)置密碼,在#后面設(shè)置密碼
/application/mysql/bin/mysqladmin -u root password 'passwd123'
#重新登錄,輸入用戶名和密碼
mysql -u root -p
passwd123
7.數(shù)據(jù)優(yōu)化:
select version(); #查看數(shù)據(jù)庫版本
select user(); #查看當(dāng)前的用戶
mysql> show databases; #查看數(shù)據(jù)庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
mysql>drop database test; #安全設(shè)置,刪除沒有用的數(shù)據(jù)庫
最終優(yōu)化為:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
+--------------------+
mysql> select user,host from mysql.user; #查詢表中的用戶
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| | localhost |
| root | localhost |
| | weblamp |
| root | weblamp |
+------+-----------+
drop user ""@weblamp; #刪除主機(jī)名是weblamp,用戶名=""。
mysql> drop user ""@localhost;
mysql> drop user "root"@weblamp;
最終優(yōu)化為:
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
如果刪除不了,就使用delete 刪除。
delete from mysql.user where(host="weblamp");
最后刷新:
flush privileges;
#如果忘記登錄密碼,可以使用這個跳過登錄密碼的輸入
/application/mysql/bin/mysqld_safe --skip-grant-table &
#修改密碼
mysql>update mysql.user set password=PASSWORD("oldboy123") where host='localhost' and user='root';
flush privileges;
[root@mysql ~]#lsof -i :3306 #查看3306端口現(xiàn)在運(yùn)行的情況
#查看mysql 的日志
cat /application/mysql/data/oldboy.err