在運維工作中經常部署各種運維環境,涉及mysql資料庫的安裝也是時常需要的。mysql資料庫安裝可以選擇yum線上安裝,但是這種安裝的mysql一般是系統自帶的,版本方面可能跟需求不太匹配。首先了解下MySQL各版本的區別
1)MySQL Community Server 社群版本,開源免費,但不提供官方技術支援 2)MySQL Enterprise Edition 企業版本,需付費,可以試用30天 3)MySQL Cluster 叢集版,開源免費。可將幾個MySQL Server封裝成一個Server。這個單獨是沒法用的,要在1或2的基礎上用。當然用來平衡多臺資料庫的。 4)MySQL Cluster CGE 高階叢集版,需付費。 5)MySQL Workbench(GUI TOOL) 一款專為MySQL設計的ER/資料庫建模工具。它是著名的資料庫設計工具DBDesigner4的繼任者。 MySQL Workbench又分為兩個版本,分別是社群版(MySQL Workbench OSS)、商用版(MySQL Workbench SE)。 MySQL Community Server 是開源免費的,這也是我們通常用的MySQL的版本。Mysql5.7是當前使用最多的發行版,也是最穩定版11 Mysql各版本下載地址:http://ftp.ntu.edu.tw/MySQL/Downloads/ -------------------Mysql各版本號的含義--------------------- Alpha: Alpha是內部測試版,一般不向外部發布,會有很多Bug.除非你也是測試人員,否則不建議使用.是希臘字母的第一位,表示最初級的版本,alpha 就是α,beta 就是β , alpha版就是比beta還早的測試版,一般都是內部測試的版本。 Beta: 該版本相對於α版已有了很大的改進,消除了嚴重的錯誤,但還是存在著一缺陷,需要經過多次測試來進一步消除。這個階段的版本會一直加入新的功能。 RC:(Release Candidate) Candidate是候選人的意思,用在軟體上就是候選版本。Release.Candidate就是發行候選版本。和Beta版最大的差別在於Beta階段會一直加入新的功能,但是到了RC版本, 幾乎就不會加入新的功能了,而主要著重於除錯! RC版本是最終發放給使用者的最接近正式版的版本,發行後改正bug就是正式版了,就是正式版之前的最後一個測試版。 GA:(general availability) 比如Apache Struts 2 GA這是Apache Struts 2首次發行穩定的版本,GA意味著General Availability,也就是官方開始推薦廣泛使用了。 Release: 該版本意味“最終版本”,在前面版本的一系列測試版之後,終歸會有一個正式版本,是最終交付使用者使用的一個版本。該版本有時也稱為標準版。一般情況下,Release 不會以單詞形式出現在軟體封面上,取而代之的是符號(R)。
可以通過原始碼安裝的方式,自定義安裝自己所需版本的mysql。
---------------------------------------------------------------------------------------------------------------------------------
mysql資料庫yum線上安裝:
[root@host-test-huanqiu ~]# yum -y install mysql mysql-server
[root@host-test-huanqiu ~]# /etc/init.d/mysqld start
[root@host-test-huanqiu ~]# mysqladmin password "123456"
[root@host-test-huanqiu ~]# mysql -p123456
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.73 |
+-----------+
1 row in set (0.00 sec
---------------------------------------------------------------------------------------------------------------------------------
廢話不多說,以下是centos6x系統下原始碼安裝Mysql的操作記錄,方便參考~
1、解除安裝舊版本
[root@host-test-huanqiu ~]# rpm -qa | grep mysql
mysql-5.1.73-7.el6.x86_64
mysql-libs-5.1.73-7.el6.x86_64
mysql-server-5.1.73-7.el6.x86_64
#普通刪除模式
[root@host-test-huanqiu ~]# rpm -e mysql-5.1.73-7.el6.x86_64 mysql-libs-5.1.73-7.el6.x86_64 mysql-server-5.1.73-7.el6.x86_64
# 強力刪除模式,如果使用上面命令刪除時,提示有依賴的其它檔案,則用該命令可以對其進行強力刪除
[root@host-test-huanqiu ~]# rpm -e --nodeps mysql-5.1.73-7.el6.x86_64 mysql-libs-5.1.73-7.el6.x86_64 mysql-server-5.1.73-7.el6.x86_64
#刪除/etc/my.cnf
[root@host-test-huanqiu ~]# rm /etc/my.cnf
2、安裝編譯程式碼需要的包
[root@host-test-huanqiu ~]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel
3、建立mysql使用者,不建立mysql使用者家目錄(加-M引數),也就是禁用mysql賬號登陸系統
[root@host-test-huanqiu ~]# groupadd mysql
[root@host-test-huanqiu ~]# useradd -g mysql mysql -M -s /sbin/nologin
4、下載MySQL,原始碼編譯安裝
[root@host-test-huanqiu ~]# cd /usr/local/src [root@host-test-huanqiu src]# wget -c http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/mysql-5.6.34.tar.gz [root@host-test-huanqiu src]# tar -zxvf mysql-5.6.34.tar.gz [root@host-test-huanqiu src]# cd mysql-5.6.34/ [root@host-test-huanqiu mysql-5.6.34]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci [root@host-test-huanqiu mysql-5.6.34]# make && make install
5、配置MySQL
修改/usr/local/mysql許可權 [root@host-test-huanqiu mysql-5.6.34]# mkdir -p /data/mysql/data [root@host-test-huanqiu mysql-5.6.34]# chown -R mysql:mysql /usr/local/mysql [root@host-test-huanqiu mysql-5.6.34]# chown -R mysql:mysql /data/mysql/data 執行初始化配置指令碼,建立系統自帶的資料庫和表 [root@host-test-huanqiu mysql-5.6.34]# cd /usr/local/mysql [root@host-test-huanqiu mysql]# ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql //如果後續不小心刪除了這個資料目錄或這個目錄下的檔案被誤操作刪除了,還可以利用這個命令重新初始化
需要特別注意:
在啟動MySQL服務時,會按照一定次序搜尋my.cnf,先在/etc目錄下找,找不到則會搜尋"$basedir/my.cnf",在本例中就是 /usr/local/mysql/my.cnf,這是新版MySQL的配置檔案的預設位置!
另外:
在CentOS 6.4版作業系統最小安裝完成後,預設會在/etc目錄下存在一個my.cnf,需要將此檔案更名為其他的名字,如:/etc/my.cnf.bak,否則,該檔案會干擾原始碼安裝的MySQL的正確配置,造成無法啟動。
在使用"yum update"更新系統後,需要檢查下/etc目錄下是否會多出一個my.cnf,如果多出,將它重新命名成別的。否則,MySQL將使用這個配置檔案啟動,可能造成無法正常啟動等問題。
--------------------------------------------------------------------------------------------------------------------------------------------------------------
啟動MySQL
新增服務,拷貝服務指令碼到init.d目錄,並設定開機啟動
[root@host-test-huanqiu mysql]# cp support-files/mysql.server /etc/init.d/mysql
[root@host-test-huanqiu mysql]# chkconfig mysql on
[root@host-test-huanqiu mysql]# service mysql start //啟動前,要先配置my.cnf檔案(如下), 並確保/data/mysql/data和/usr/local/mysql的mysql許可權
如果啟動報錯: [root@slave-server mysql]# service mysql start Starting MySQL.Logging to '/data/mysql/data/mysql-error.log'. 170416 12:55:28 mysqld_safe Directory '/usr/local/mysql/var' for UNIX socket file don't exists. ERROR! The server quit without updating PID file (/data/mysql/data/mysql.pid). 解決辦法: [root@slave-server mysql]# mkdir /usr/local/mysql/var [root@slave-server mysql]# chown -R mysql:mysql /usr/local/mysql [root@slave-server mysql]# service mysql start Starting MySQL.. SUCCESS!
配置使用者和環境變數
MySQL啟動成功後,root預設沒有密碼,需要設定root密碼,設定之前,需要先設定PATH,否則不能直接呼叫mysql 修改/etc/profile檔案,在檔案末尾新增 [root@host-test-huanqiu mysql]# vim /etc/profile ........ export PATH=$PATH:/usr/local/mysql/bin [root@host-test-huanqiu mysql]# source /etc/profile 到此,就可以直接mysql登陸了 [root@host-test-huanqiu mysql]# mysql -uroot ....... mysql> select version(); +-----------+ | version() | +-----------+ | 5.6.33 | +-----------+ 1 row in set (0.00 sec)
登陸mysql,若是報錯如下:
[root@test2-235 mysql]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 解決辦法 [root@test2-235 mysql]# mkdir /var/lib/mysql/ [root@test2-235 mysql]# ln -s /usr/local/mysql/var/mysql.sock /var/lib/mysql/mysql.sock
修改root密碼,執行命令如下
mysql> SET PASSWORD = PASSWORD('123456'); 若要設定root使用者可以遠端訪問,執行 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; 使授權立即生效 mysql> FLUSH PRIVILEGES; 也可以使用mysqladmin命令設定mysql密碼 [root@host-test-huanqiu mysql]# mysqladmin password 123456 #安裝mysql後第一次設定密碼 [root@host-test-huanqiu mysql]# mysqladmin -p 舊密碼 password 新密碼 #重置密碼
最後配置防火牆
預設防火牆的3306埠預設沒有開啟,若要遠端訪問,需要開啟這個埠.也可以做下白名單,比如只允許192.168.1.0/24網段的客戶機訪問本機的mysql。 [root@host-test-huanqiu mysql]# vim /etc/sysconfig/iptables ....... -A INPUT -s 192.168.1.0/24 -p tcp -m tcp -m state --state NEW -dport 3306 -j ACCEPT
----------------------------------------------------------------------------------------------------------------------------------
下面貼下mysql6.x的my.cnf配置
[client] port = 3306 socket = /usr/local/mysql/var/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/var/mysql.sock basedir = /usr/local/mysql/ datadir = /data/mysql/data pid-file = /data/mysql/data/mysql.pid user = mysql bind-address = 0.0.0.0 server-id = 1 sync_binlog=1 log_bin = mysql-bin skip-name-resolve back_log = 600 max_connections = 3000 max_connect_errors = 3000 table_open_cache = 512 max_allowed_packet = 16M binlog_cache_size = 16M max_heap_table_size = 16M tmp_table_size = 256M read_buffer_size = 1024M read_rnd_buffer_size = 1024M sort_buffer_size = 1024M join_buffer_size = 1024M key_buffer_size = 8192M thread_cache_size = 8 query_cache_size = 512M query_cache_limit = 1024M ft_min_word_len = 4 binlog_format = mixed expire_logs_days = 30 log_error = /data/mysql/data/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /data/mysql/data/mysql-slow.log performance_schema = 0 explicit_defaults_for_timestamp skip-external-locking default_storage_engine = InnoDB innodb_file_per_table = 1 innodb_open_files = 500 innodb_buffer_pool_size = 4096M #這個引數一般設定為伺服器最大記憶體的60% innodb_write_io_threads = 1000 innodb_read_io_threads = 1000 innodb_thread_concurrency = 8 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 4M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 2G myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES port = 3306
針對上面的部署過程,為了便於以後安裝mysql環境,編寫了下面的安裝指令碼,直接執行這個指令碼就能自動化構建mysql環境了。
[root@slave-server ~]# cat mysql.sh (下載地址:https://pan.baidu.com/s/1gfEKC9d 密碼:6ec3)
#!/bin/bash #解除安裝系統自帶的Mysql /bin/rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps /bin/rm -f /etc/my.cnf #安裝編譯程式碼需要的包 /usr/bin/yum -y install make gcc-c++ cmake bison-devel ncurses-devel #編譯安裝mysql5.6 /usr/sbin/groupadd mysql /usr/sbin/useradd -g mysql mysql -M -s /sbin/nologin cd /usr/local/src wget -c http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/mysql-5.6.37.tar.gz /bin/tar -zxvf mysql-5.6.37.tar.gz cd mysql-5.6.37/ /usr/bin/cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci make && make install #修改/usr/local/mysql許可權 mkdir -p /data/mysql/data /bin/chown -R mysql:mysql /usr/local/mysql /bin/chown -R mysql:mysql /data/mysql/data #執行初始化配置指令碼,建立系統自帶的資料庫和表 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql #配置my.cnf cat > /usr/local/mysql/my.cnf << EOF [client] port = 3306 socket = /usr/local/mysql/var/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/var/mysql.sock basedir = /usr/local/mysql/ datadir = /data/mysql/data pid-file = /data/mysql/data/mysql.pid user = mysql bind-address = 0.0.0.0 server-id = 1 sync_binlog=1 log_bin = mysql-bin skip-name-resolve #skip-networking back_log = 600 max_connections = 3000 max_connect_errors = 3000 ##open_files_limit = 65535 table_open_cache = 512 max_allowed_packet = 16M binlog_cache_size = 16M max_heap_table_size = 16M tmp_table_size = 256M read_buffer_size = 1024M read_rnd_buffer_size = 1024M sort_buffer_size = 1024M join_buffer_size = 1024M key_buffer_size = 8192M thread_cache_size = 8 query_cache_size = 512M query_cache_limit = 1024M ft_min_word_len = 4 binlog_format = mixed expire_logs_days = 30 log_error = /data/mysql/data/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /data/mysql/data/mysql-slow.log performance_schema = 0 explicit_defaults_for_timestamp ##lower_case_table_names = 1 skip-external-locking default_storage_engine = InnoDB ##default-storage-engine = MyISAM innodb_file_per_table = 1 innodb_open_files = 500 innodb_buffer_pool_size = 4096M innodb_write_io_threads = 1000 innodb_read_io_threads = 1000 innodb_thread_concurrency = 8 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 4M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 2G myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES port = 3306 EOF #啟動mysql服務 cd /usr/local/mysql /bin/mkdir var /bin/chown -R mysql.mysql var cp support-files/mysql.server /etc/init.d/mysql /sbin/chkconfig mysql on service mysql start #設定環境變數 echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile source /etc/profile #設定mysql登陸密碼,初始密碼為123456 /bin/mkdir -p /var/lib/mysql ln -s /usr/local/mysql/var/mysql.sock /var/lib/mysql/mysql.sock mysql -e "SET PASSWORD = PASSWORD('123456');" mysql -p123456 -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;" mysql -p123456 -e "FLUSH PRIVILEGES;"
編寫mysql登入指令碼
[root@slave-server ~]# vim /etc/passwd ...... mysql:x:1001:1001::/home/mysql:/bin/bash [root@slave-server ~]# mkdir /home/mysql [root@slave-server ~]# chown -R mysql.mysql /home/mysql [root@slave-server ~]# vim /usr/local/mysql/bin/myin #!/bin/bash p=$1 shift mysql -h"127.0.0.1" -P"$p" --default-character-set=utf8mb4 --show-warnings -uqw_cmdb -p'qw_cmdb123' -A --prompt="(\u@\p-\r:\m:\s:)[\d]> " "$@" [root@slave-server ~]# chmod 755 /usr/local/mysql/bin/myin [root@slave-server ~]# su - mysql Last login: Mon Jun 25 17:16:20 CST 2018 on pts/6 -bash-4.2$ myin 3306 #切換到mysql使用者下,使用"myin 3306"命令登入mysql Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 256 Server version: 5.6.40-log Source distribution Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. (bobo@3306-02:51:38:)[(none)]>
================centos7下啟動mysql時的一個報錯=================
在centos7下如上記錄操作後,在啟動mysql服務的時候遇到下面一個報錯: [root@kevin mysql]# service mysql start Starting MySQL.181013 22:18:35 mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'. The server quit without updating PID file (/data/mysql/data[FAILED]id). 該問題為mysql 的一個小bug 。 臨時處理辦法為 直接建立一個 log-error 指定的空檔案,並給適當的屬主、屬組許可權即可。該問題已經在 版本 5.5.55, 5.6.36, 5.7.18, 8.0.1 中修復。 後續可以通過升級版本,來修復該問題。 [root@kevin mysql]# mkdir -p /var/log/mariadb [root@kevin mysql]# touch /var/log/mariadb/mariadb.log [root@kevin mysql]# chown -R mysql.mysql /var/log/mariadb [root@kevin mysql]# service mysql start Starting MySQL... [ OK ] [root@kevin mysql]# ln -s /var/log/mariadb/mariadb.log /data/mysql/data/mysql-error.log [root@zabbix data]# /etc/init.d/mysql restart Shutting down MySQL.. [ 確定 ] Starting MySQL.. [ 確定 ]