MySQL官方壓縮包安裝:mysql
1:下載mysql官方版本,此處以目前最新版本5.7.14爲例,下載的64位版本文件爲:linux
mysql-5.7.14-linux-glibc2.5-x86_64.tarsql
2:解壓文件數據庫
mv mysql-5.7.14-linux-glibc2.5-x86_64.tar /opt/mysql-5.7.14-linux-glibc2.5-x86_64.tar tar -zvxf mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz mv mysql-5.7.14-linux-glibc2.5-x86_64 /data1/
mv mysql-5.7.14-linux-glibc2.5-x86_64 mysql
這裏/data1是服務器上的掛載擴展點,所以放在該掛載點下,並將解壓文件夾更名爲mysqlbootstrap
3:建立mysql用戶及用戶組vim
groupadd mysql
useradd -r -g mysql mysql
這裏-r表示該用戶爲系統用戶,不能用於登陸,可經過如下查看組及用戶信息:服務器
cat /etc/group | grep mysql cat /etc/passwd | grep mysql
4:變動mysql文件夾所屬用戶及組併發
chown -R mysql ./mysql chgrp -R mysql ./mysql
5:mysql初始化less
cd /data1/mysql
./bin/mysql_install_db --user=mysql --basedir=/data1/mysql --datadir=/data1/mysql/data/
正常狀況下會出現以下結果:socket
2016-09-05 16:09:33 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2016-09-05 16:09:35 [WARNING] The bootstrap log isn't empty: 2016-09-05 16:09:35 [WARNING] 2016-09-05T08:09:33.109112Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead 2016-09-05T08:09:33.109668Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) 2016-09-05T08:09:33.109675Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
6:編輯啓動及配置文件
vim /data1/mysql/support-files/my-default.cnf
將配置文件內容按照以下修改:
[client] default-character-set=gbk [mysqld] character-set-server=gbk max_allowed_packet=60MB max_connections=2000 datadir=/data1/mysql socket=/data1/mysql/mysql.sock symbolic-links=0 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysql] default-character-set=gbk socket=/data1/mysql/mysql.sock [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
也能夠將log及pid文件都改成/data1目錄(有人反映會遇到/var/run權限不足等問題)。
vim /data1/mysql/support-files/mysql.server
啓動文件內修改basedir及datadir內容:
basedir=/data1/mysql
datadir=/data1/mysql/data
同時,爲了在首次啓動後有權限登陸mysql,先在啓動指令(start)中取消權限驗證:
case "$mode" in 'start') # Start daemon # Safeguard (relative paths, core dumps..) cd $basedir echo $echo_n "Starting MySQL" if test -x $bindir/mysqld_safe then # Give extra arguments to mysqld with the my.cnf file. This script # may be overwritten at next upgrade. $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args --skip-grant-tables >/dev/null 2>&1 & wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$? # Make lock for RedHat / SuSE if test -w "$lockdir" then touch "$lock_file_path" fi exit $return_value else log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)" fi ;;
在mysqld_safe啓動指令的參數中,暫時加入「--skip-grant-tables」參數,這一步很是重要,不然會報各類相似錯誤:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES). ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO).
即便正確地用mysql臨時生成的密碼(/root/.mysql_secret)也是如此。
而後將配置文件、啓動文件copy至目的地:
cp -af ./my-default.cnv /etc/my.cnf cp -af ./mysql.server /etc/init.d/mysqld
7:配置mysql的環境變量
vim /etc/profile
在末尾添加:
export PATH=$PATH:/data1/mysql/bin
生效環境變量:
source /etc/profile
8:配置mysql啓動服務
chkconfig --add mysqld chkconfig --level 35 mysql on chkconfig
正常狀況下mysqld已設置爲系統服務:
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
9:建立自定義的日誌目錄(可選)
若是以前配置文件中日誌目錄修改成/data1/mysql/log,則須要手動建立該目錄:
mkdir /data1/mysql/log chown -R mysql /data1/mysql/log chgrp -R mysql /data1/mysql/log
10:啓動mysql服務
在centOS7中,直接用systemctl啓動便可:
systemctl start mysql
11:更改root用戶密碼並分配遠程訪問權限
因爲以前在start中取消了權限驗證,因此如今能夠不輸入密碼直接連上數據庫,這裏咱們先更改root用戶密碼,並分配遠程訪問權限,成功後將去除start啓動指令中的取消權限驗證參數。
mysql -u root mysql
無需驗證直接鏈接mysql,成功後出現mysql>提示符:
mysql> use mysql mysql> update user set authentication_string=password('123456') where user='root'; mysql> flush privileges; mysql> grant all on *.* to root@'%' identified by '123456';
第一行切換當前數據庫;
第二行修改root帳戶密碼,這裏的「authentication_string」是較新的版本,老版本的密碼字段是"password";
第三行刷新權限;
第四行分配遠程訪問權限給root用戶,這裏的密碼是「123456」。
接下來修改/etc/rc.d/init.d/mysqld文件,去除腳本中start部分的"--skip-grant-tables"參數,並重啓mysql服務,取消匿名登陸
'start') # Start daemon # Safeguard (relative paths, core dumps..) cd $basedir echo $echo_n "Starting MySQL" if test -x $bindir/mysqld_safe then # Give extra arguments to mysqld with the my.cnf file. This script # may be overwritten at next upgrade. $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args>/dev/null 2>&1 & wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$? # Make lock for RedHat / SuSE if test -w "$lockdir" then touch "$lock_file_path" fi exit $return_value else log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)" fi ;;
注意 mysqld_safe 啓動指令後的 --skip-grant-tables 參數已經刪除。
刷新服務文件並重啓mysql服務:
systemctl daemon-reload
systemctl restart mysql
完成mysql的安裝,而且已經能夠經過root用戶用指定的密碼'123456'進行遠程訪問。
注:若是開啓了防火牆,則可經過開啓3306端口,或開啓mysql服務的方式來配置firewalld:
firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --zone=public --add-service=mysql --permanent firewall-cmd --reload
MySQL主從複製配置:
目標:兩臺mysql服務器互爲主從,達到數據互備的目的。
1:修改mysql配置文件
服務器A配置文件(/etc/my.cnf),在mysqld標籤下新增以下內容:
log-bin=/data1/mysql/binlog binlog_format=mixed server-id=153 slave-skip-errors=1062,1051,1146,1032 read-only=0 binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=mysql auto-increment-increment=10 auto-increment-offset=1 replicate-ignore-db=information_schema replicate-ignore-db=performance_schema replicate-ignore-db=mysql relay_log=/data1/mysql/relaylog log-slave-updates=ON
服務器B配置文件(/etc/my.conf),在mysql標籤下新增以下內容:
log-bin=/data1/mysql/binlog binlog_format=mixed server-id=154 slave-skip-errors=1062,1051,1146,1032 read-only=0 binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=mysql auto-increment-increment=10 auto-increment-offset=2 replicate-ignore-db=information_schema replicate-ignore-db=performance_schema replicate-ignore-db=mysql relay_log=/data1/mysql/relaylog log-slave-updates=ON
兩臺服務器的區別在server-id(可用IP最後一節),以及auto-increment-offset上,自增間隔10,offset分別爲1,2可保證對不一樣服務器併發寫操做的id不會衝突。
2:mysql服務器備份用戶建立及受權
以命令行模式鏈接服務器A,建立用戶repl並受權:
mysql> create user 'repl'@'server B IP address' identified by '123456'; mysql> grant replication slave on *.* to 'repl'@'server B IP address' identified by '123456'; mysql> flush privileges;
以命令行模式鏈接服務器B,建立用戶repl並受權:
mysql> create user 'repl'@'server A IP address' identified by '123456'; mysql> grant replication slave on *.* to 'repl'@'server A IP address' identified by '123456'; mysql> flush privileges;
3:重啓mysql服務器並設置雙機互備
在兩臺mysql服務器上分別重啓mysql服務:
systemctl restart mysql
在mysql服務器A上查看master信息:
mysql> show master status;
通常會看到以下信息:
+---------------+----------+--------------+---------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+---------------------------------------------+-------------------+ | binlog.000001 | 329 | | information_schema,performance_schema,mysql | | +---------------+----------+--------------+---------------------------------------------+-------------------+ 1 row in set (0.00 sec)
此時連上服務器B的mysql客戶端,配置主服務器:
change master to master_host='server A IP address', master_user='repl', master_password='123456', master_log_file='binlog.000001', master_log_pos=329; start slave;
同理,鏈接上mysql服務器B的mysql控制檯,執行以下指令:
mysql> show master status;
獲取服務器B的master信息,相似以下:
+---------------+----------+--------------+---------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+---------------------------------------------+-------------------+ | binlog.000002 | 329 | | information_schema,performance_schema,mysql | | +---------------+----------+--------------+---------------------------------------------+-------------------+ 1 row in set (0.00 sec)
此時連上服務器A的mysql客戶端,配置主服務器:
change master to master_host='server B IP address', master_user='repl', master_password='123456', master_log_file='binlog.000002', master_log_pos=329; start slave;
到如今,mysql服務器A與服務器B之間已正確配置了雙機主從互備,能夠經過show slave status指令查看,其中的Slave_IO_Running是否爲Connecting,Slave_SQL_Running是否爲Yes。
也能夠在服務器A上對數據庫作出變動,好比create table操做,會在短期內自動同步至服務器B。
2016-10-9更新:
在mysql控制檯執行change master指令有可能因爲以前已配置過主從複製而致使失敗,此時須要先中止主從複製的相關READ操做:
STOP SLAVE IO_THREAD;
STOP SLAVE SQL_THREAD;
以後再執行change master指令便可。同時要注意在變動主從複製配置以前,請先確認主從複製用戶的權限及密碼正確性,並重啓mysql。
同時,mysql的錯誤日誌會提供大量信息。
2016-10-18更新:
在mysql配置文件(默認路徑/etc/my.cnf)中,須要經過配置來開啓函數腳本執行,同時該配置項也會影響主從複製,以下爲典型的錯誤提示:
Error Code: 1418 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
方案1:經過設置全局變量來開啓,但重啓mysqld後會失效
set global log_bin_trust_function_creators=TRUE
方案2:經過修改mysql配置文件(默認路徑/etc/my.cnf),在mysqld配置節中加入配置信息
log_bin_trust_function_creators=true
重啓mysqld守護進程便可。