RPM方式安裝MySQL5.7 (CentOS 6.7)
a. 檢查MySQL及相關RPM包,是否安裝,如果有安裝,則移除(rpm –e 名稱)
1 |
[root@localhost
~] #
rpm -qa | grep -i mysql |
2 |
mysql-libs-5.1.66-2.el6_3.x86_64 |
3 |
... |
4 |
[root@localhost
~] #rpm
-e --nodeps mysql-libs* //*號指代實際包名, 請實際情況輸入完整包名 |
5 |
[root@localhost
~] #rpm
-e mysql-server* |
6 |
#如果有早期版本mysql, 則卸載相關組件 |
7 |
[root@localhost
~] #rpm
-e mysql-client* |
檢查並刪除老版本mysql的開發頭文件和庫
命令:rm -fr /usr/lib/mysql
rm -fr /usr/include/mysql
注意:卸載後/var/lib/mysql中的數據及/etc/my.cnf不會刪除,如果確定沒用後就手工刪除
rm -f /etc/my.cnf
rm -fr /var/lib/mysql
rm -fr /usr/share/mysql
b. 下載Linux對應的RPM包然後解壓縮,如:CentOS 6.7_64對應的RPM包(mysql-5.7.10-1.el6.x86_64.rpm-bundle.tar),如下:
1 |
[root@localhost
rpm] #
cd /usr |
2 |
[root@localhost
rpm] #
tar -xvf MySQL-5.7.26-1.el6.x86_64.rpm-bundle.tar |
c. 安裝MySQL (安裝包步驟請自己多多嘗試)
1 |
[root@localhost
rpm] #
rpm -ivh MySQL-server-5.7.15-1.el6.x86_64.rpm |
2 |
[root@localhost
rpm] #
rpm -ivh MySQL-devel-5.7.15-1.el6.x86_64.rpm |
3 |
[root@localhost
rpm] #
rpm -ivh MySQL-client-5.7.15-1.el6.x86_64.rpm |
4 |
#修改配置文件位置 |
5 |
[root@localhost
rpm] #
cp /usr/share/mysql/my-default.cnf /etc/my.cnf |
如果需要更改默認的data文件夾, 請提前更改my.cnf中datadir目錄,
socket和pid-file建議還是使用默認的目錄/var/lib/mysql
(注意: 如果沒有經驗請完全按照默認的配置, 無需對my.cnf做任何更改, 或者按照本文最後的my.cnf配置)
d. 初始化MySQL及設置密碼
1 |
[root@localhost
rpm] #
/usr/sbin/mysqld --user=mysql --initialize //無密碼/usr/sbin/mysqld --initialize
!!!運行以後查看提示密碼, 後續很難查找 如果有修改默認data存放地址, 一定要做修改以下設置, or as default
1.1. 關閉selinux或者設置新數據文件權限, 同時嘗試設置權限755
設置: setenforce 0 1.2 創建data文件存地址, 並修改用戶和組爲mysql
chown -R mysql:mysql /home/mysqldata
1.3 修改默認地址/etc/rc.d/init.d/mysqld (如果啓動失敗, 可以嘗試延後當前步驟) datadir=/home/mysqldata |
2 |
[root@localhost
rpm] #
service mysqld start |
3 |
[root@localhost
~] #
mysql -u root -p qKTaFZnl |
4 |
mysql>
SET PASSWORD = PASSWORD( 'Aa12345' ); #設置密碼爲Aa12345 |
5 |
mysql> exit |
6 |
[root@localhost
~] #
mysql -u root -p
|
e. 允許遠程登陸
01 |
mysql>
use mysql; |
|
13 |
mysql>
update user set host= '%' where
user= 'root' and
host= 'localhost' ; |
14 |
mysql>
flush privileges; |
15 |
mysql> exit |
f. 設置開機自啓動
1 2 |
[root@localhost
~] #chkconfig
--add mysqld
[root@localhost
~] #
chkconfig mysqld on |
3 |
[root@localhost
~] #
chkconfig --list | grep mysql |
|
mysql
0:off 1:off 2:on 3:on 4:on 5:on 6:off |
g. MySQL的默認安裝位置
1 |
/var/lib/mysql/ #數據庫目錄 |
2 |
/usr/share/mysql #配置文件目錄 |
3 |
/usr/bin #相關命令目錄 |
4 |
/etc/init.d/mysql #啓動腳本 |
FAQ:
1. 端口號3306被佔用, 更改端口號, 或者查看當前端口被哪個進程佔用, 然後kill
#netstat -anp | grep "3306"
tcp 0 0 :::3306 :::* LISTEN 4280/mysqld
#kill 4280
2. 執行mysql -u root -p輸入密碼後顯示錯誤
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
2.1 請使用臨時命令 mysql -u root -p -S /home/mysqldata/mysql.sock\
2.2 指定my.cnf >[client] ->socket 路徑
3. 修改默認地址/etc/rc.d/init.d/mysqld
datadir=/home/mysqldata
4. 提示權限問題, 關閉selinux或者設置新數據文件權限
chcon -Rt mysqld_db_t /home/mysqldata
5. 最近新裝好的mysql在進入mysql工具時,總是有錯誤提示:
# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
或者
# mysql -u root -p password 'newpassword'
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
現在終於找到解決方法了。本來準備重裝的,現在不必了。
5.7找回密碼方法操作很簡單,如下:
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
update user set authentication_string=password('Aa12345') where user='root'; //5.7專用, 把空的用戶密碼都修改成非空的密碼就行了。
mysql> FLUSH PRIVILEGES;
mysql> quit # /etc/init.d/mysqld restart
# mysql -u root -p
Enter password: <輸入新設的密碼newpassword>
修改字符集和數據存儲路徑
配置/etc/my.cnf文件,修改數據存放路徑、mysql.sock路徑以及默認編碼utf-8.
#查看字符集
show variables like '%collation%';
show variables like '%char%';
SHOW VARIABLES LIKE '%character%'
#查看默引擎
show engines
#查看默認包大小
show VARIABLES like '%max_allowed_packet%';
#查看log文件地址
ls -l `pgrep mysqld | sed 's@.*@/proc/&/fd/@'` | grep log
#查看插件地址
show variables like 'plugin_dir'
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
character-set-server=utf8
default-storage-engine=INNODB
max_allowed_packet = 32M
slave_max_allowed_packet = 1024M
datadir=/home/mysqldata
socket=/home/mysqldata/mysql.sock
max_connections = 1000
#relay_log_recovery = 1 #從庫開啓中繼日誌relay-log自我修復。
#skip-name-resolve #禁止mysql進行DNS查詢, 可以解決網絡配置變化等情況下訪問緩慢的問題。
#Master configuration-defaults
#server-id=1 #設置服務器id,爲1表示主服務器,注意:如果原來的配置文件中已經有這一行,就不用再添加了。
#log_bin=mysql-bin #啓動MySQ二進制日誌系統,注意:如果原來的配置文件中已經有這一行,就不用再添加了。
#expire_logs_days = 15 #二進制日誌自動刪除的天數。默認值爲0,表示"沒有自動刪除".
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=10000
#Slave configuration-defaults
#server-id=2 #設置服務器id,爲2表示從服務器,注意:如果原來的配置文件中已經有這一行,就不用再添加了。
#expire_logs_days = 15 #二進制日誌自動刪除的天數。默認值爲0,表示"沒有自動刪除".
#slave_skip_errors=1032,1396,1062
#rpl_semi_sync_slave_enabled=1
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 45G
innodb_log_file_size=1024M
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/home/mysqldata/mysqld.pid
[client]
socket=/home/mysqldata/mysql.sock
2 |
[root@localhost
~] #
chkconfig --list | grep mysql |
3 |
mysql
0:off 1:off 2:on 3:on 4:on 5:on 6:off |
RPM方式安裝MySQL5.7 (CentOS 6.7)
a. 檢查MySQL及相關RPM包,是否安裝,如果有安裝,則移除(rpm –e 名稱)
1 |
[root@localhost
~] #
rpm -qa | grep -i mysql |
2 |
mysql-libs-5.1.66-2.el6_3.x86_64 |
3 |
... |
4 |
[root@localhost
~] #rpm
-e --nodeps mysql-libs* //*號指代實際包名, 請實際情況輸入完整包名 |
5 |
[root@localhost
~] #rpm
-e mysql-server* |
6 |
#如果有早期版本mysql, 則卸載相關組件 |
7 |
[root@localhost
~] #rpm
-e mysql-client* |
檢查並刪除老版本mysql的開發頭文件和庫
命令:rm -fr /usr/lib/mysql
rm -fr /usr/include/mysql
注意:卸載後/var/lib/mysql中的數據及/etc/my.cnf不會刪除,如果確定沒用後就手工刪除
rm -f /etc/my.cnf
rm -fr /var/lib/mysql
rm -fr /usr/share/mysql
b. 下載Linux對應的RPM包然後解壓縮,如:CentOS 6.7_64對應的RPM包(mysql-5.7.10-1.el6.x86_64.rpm-bundle.tar),如下:
1 |
[root@localhost
rpm] #
cd /usr |
2 |
[root@localhost
rpm] #
tar -xvf MySQL-5.7.26-1.el6.x86_64.rpm-bundle.tar |
c. 安裝MySQL (安裝包步驟請自己多多嘗試)
1 |
[root@localhost
rpm] #
rpm -ivh MySQL-server-5.7.15-1.el6.x86_64.rpm |
2 |
[root@localhost
rpm] #
rpm -ivh MySQL-devel-5.7.15-1.el6.x86_64.rpm |
3 |
[root@localhost
rpm] #
rpm -ivh MySQL-client-5.7.15-1.el6.x86_64.rpm |
4 |
#修改配置文件位置 |
5 |
[root@localhost
rpm] #
cp /usr/share/mysql/my-default.cnf /etc/my.cnf |
如果需要更改默認的data文件夾, 請提前更改my.cnf中datadir目錄,
socket和pid-file建議還是使用默認的目錄/var/lib/mysql
(注意: 如果沒有經驗請完全按照默認的配置, 無需對my.cnf做任何更改, 或者按照本文最後的my.cnf配置)
d. 初始化MySQL及設置密碼
1 |
[root@localhost
rpm] #
/usr/sbin/mysqld --user=mysql --initialize //無密碼/usr/sbin/mysqld --initialize
!!!運行以後查看提示密碼, 後續很難查找 如果有修改默認data存放地址, 一定要做修改以下設置, or as default
1.1. 關閉selinux或者設置新數據文件權限, 同時嘗試設置權限755
設置: setenforce 0 1.2 創建data文件存地址, 並修改用戶和組爲mysql
chown -R mysql:mysql /home/mysqldata
1.3 修改默認地址/etc/rc.d/init.d/mysqld (如果啓動失敗, 可以嘗試延後當前步驟) datadir=/home/mysqldata |
2 |
[root@localhost
rpm] #
service mysqld start |
3 |
[root@localhost
~] #
mysql -u root -p qKTaFZnl |
4 |
mysql>
SET PASSWORD = PASSWORD( 'Aa12345' ); #設置密碼爲Aa12345 |
5 |
mysql> exit |
6 |
[root@localhost
~] #
mysql -u root -p
|
e. 允許遠程登陸
01 |
mysql>
use mysql; |
|
13 |
mysql>
update user set host= '%' where
user= 'root' and
host= 'localhost' ; |
14 |
mysql>
flush privileges; |
15 |
mysql> exit |
f. 設置開機自啓動
1 2 |
[root@localhost
~] #chkconfig
--add mysqld
[root@localhost
~] #
chkconfig mysqld on |
3 |
[root@localhost
~] #
chkconfig --list | grep mysql |
|
mysql
0:off 1:off 2:on 3:on 4:on 5:on 6:off |
g. MySQL的默認安裝位置
1 |
/var/lib/mysql/ #數據庫目錄 |
2 |
/usr/share/mysql #配置文件目錄 |
3 |
/usr/bin #相關命令目錄 |
4 |
/etc/init.d/mysql #啓動腳本 |
FAQ:
1. 端口號3306被佔用, 更改端口號, 或者查看當前端口被哪個進程佔用, 然後kill
#netstat -anp | grep "3306"
tcp 0 0 :::3306 :::* LISTEN 4280/mysqld
#kill 4280
2. 執行mysql -u root -p輸入密碼後顯示錯誤
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
2.1 請使用臨時命令 mysql -u root -p -S /home/mysqldata/mysql.sock\
2.2 指定my.cnf >[client] ->socket 路徑
3. 修改默認地址/etc/rc.d/init.d/mysqld
datadir=/home/mysqldata
4. 提示權限問題, 關閉selinux或者設置新數據文件權限
chcon -Rt mysqld_db_t /home/mysqldata
5. 最近新裝好的mysql在進入mysql工具時,總是有錯誤提示:
# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
或者
# mysql -u root -p password 'newpassword'
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
現在終於找到解決方法了。本來準備重裝的,現在不必了。
方法操作很簡單,如下:
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root' and host='root' or host='localhost';//把空的用戶密碼都修改成非空的密碼就行了。
mysql> FLUSH PRIVILEGES;
mysql> quit # /etc/init.d/mysqld restart
# mysql -u root -p
Enter password: <輸入新設的密碼newpassword>
修改字符集和數據存儲路徑
配置/etc/my.cnf文件,修改數據存放路徑、mysql.sock路徑以及默認編碼utf-8.
#查看字符集
show variables like '%collation%';
show variables like '%char%';
SHOW VARIABLES LIKE '%character%'
#查看默引擎
show engines
#查看默認包大小
show VARIABLES like '%max_allowed_packet%';
#查看log文件地址
ls -l `pgrep mysqld | sed 's@.*@/proc/&/fd/@'` | grep log
#查看插件地址
show variables like 'plugin_dir'
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
character-set-server=utf8
default-storage-engine=INNODB
max_allowed_packet = 32M
slave_max_allowed_packet = 1024M
datadir=/home/mysqldata
socket=/home/mysqldata/mysql.sock
max_connections = 1000
relay_log_recovery = 1 #開啓中繼日誌relay-log自我修復
#Master configuration-defaults
#server-id=1 #設置服務器id,爲1表示主服務器,注意:如果原來的配置文件中已經有這一行,就不用再添加了。
#log_bin=mysql-bin #啓動MySQ二進制日誌系統,注意:如果原來的配置文件中已經有這一行,就不用再添加了。
#rpl_semi_sync_master_timeout=10000
#Slave configuration-defaults
#server-id=2 #設置服務器id,爲2表示從服務器,注意:如果原來的配置文件中已經有這一行,就不用再添加了。
#rpl_semi_sync_slave_enabled=1# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 45G
innodb_log_file_size=1024M
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/home/mysqldata/mysqld.pid
[client]
socket=/home/mysqldata/mysql.sock
2 |
[root@localhost
~] #
chkconfig --list | grep mysql |
3 |
mysql
0:off 1:off 2:on 3:on 4:on 5:on 6:off |