Mysql安裝:html
1、經過二進制的方式安裝 二進制安裝方式中,包括rpm版本以及glibc版本。 rpm版本就是在特定linux版本下編譯的,若是你的linux版本匹配,就能夠安裝; glibc版本是基於特定的glibc版本編譯的; glibc是GNU發佈的libc庫,即c運行庫。glibc是linux系統中最底層的api,幾乎其它任何運行庫都會依賴於glibc。 優勢:安裝和維護都比較方便,不須要編譯。 缺點:可定製性差,可移植性差,不靈活 二、經過源代碼編譯的安裝(mysql-xx.tar.gz) 優勢:可定製性強(安裝能夠根據用戶的需求,只安裝所須要的功能) 缺點:安裝複雜,所須要的時間比二進制的安裝要長得多 3、構建本身的二進制rpm包 優勢:根據需求定製成本身的rpm包,方便安裝維護。 缺點:前期構建耗時較長,相對比較複雜
MySQL國內鏡像下載地址:
http://mirrors.sohu.com/mysql/java
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/node
開源鏡像站點彙總mysql
http://mirrors.ustc.edu.cn/linux
1、redhat mysql(RPM) 光盤中mysql老舊,漏洞多,若是必定要使用此版本,請到紅帽的源碼目錄樹中下載最新版本 # yum list|grep ^mysql mysql.x86_64 客戶端 mysql-bench.x86_64 壓力測試工具包 mysql-connector-java.noarch 鏈接器 mysql-connector-odbc.x86_64 mysql-devel.i686 開發包 mysql-devel.x86_64 mysql-libs.i686 庫包(*.dll),可讓其餘第三方程序調用這些庫文件,擴充軟件功能 mysql-libs.x86_64 mysql-server.x86_64 服務器 mysql-test.x86_64 測試庫 安裝服務端和客戶端軟件: # yum -y install mysql mysql-server 啓動服務: mysql啓動原理 執行mysqld(服務端命) ----> 根據參數讀取配置文件或者直接給命令傳遞參數 ----> 讀取相應的數據文件和其餘的物理文件(日誌文件等) ----> 生成socket文件和在相應的端口上進行監聽 說明:rhel系統自動的數據庫的啓動腳本會判斷數據目錄是否爲空,若是爲空,它會自動初始化 # service mysqld start 測試是否啓動ok: # lsof -i:3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 7321 mysql 10u IPv4 54236 0t0 TCP *:mysql (LISTEN) # netstat -nltp|grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 7321/mysqld # mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.71 Source distribution mysql> show databases; 查看有哪些庫 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) information_schema數據庫: 對象信息數據庫,其中保存着關於MySQL服務器所維護的全部其餘數據庫的信息。在INFORMATION_SCHEMA中,有數個只讀表。它們其實是視圖,而不是基本表,所以,你將沒法看到與之相關的任何文件。 視圖:是一個虛表,即視圖所對應的數據不進行實際存儲,數據庫中只存儲視圖的定義,在對視圖的數據進行操做時,系統根據視圖的定義去操做與視圖相關聯的基本表。 mysql數據庫: 這個是mysql的核心數據庫,主要負責存儲數據庫的用戶、權限設置、關鍵字等mysql本身須要使用的控制和管理信息;不能夠刪除,也不要輕易修改這個數據庫裏面的表息。 test數據庫: 這個是安裝時候建立的一個測試數據庫,和它的名字同樣,是一個徹底的空數據庫,沒有任何表,能夠刪除。
默認讀取的首選配置文件 /etc/my.cnf # cat /etc/my.cnf [mysqld] 用中括號括起來的叫參數組,用來針對不一樣的工具設定參數 datadir=/var/lib/mysql 數據文件存放的目錄 socket=/var/lib/mysql/mysql.sock socket文件是用於本地鏈接mysql數據庫的接口文件,遠程鏈接的話就要經過TCP/IP協議 user=mysql 管理mysql的系統用戶 # Disabling symbolic-links is recommended to prevent assorted security risks 禁止使用符號連接,防止安全隱患 symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log # 錯誤日誌文件 pid-file=/var/run/mysqld/mysqld.pid # pid文件 注意:pid文件和socket文件是服務啓動成功後纔會有的
2、mysql AB (RPM) -mysql官方的RPM包,提供版本比較多,像suse/redhat/oracle linux MySQL-client-5.6.19-1.el6.x86_64.rpm 客戶端 MySQL-devel-5.6.19-1.el6.x86_64.rpm 開發工具包(其餘軟件須要調用到mysql的頭文件或者庫文件時安裝)eg:proxy、監控軟件等 MySQL-embedded-5.6.19-1.el6.x86_64.rpm 嵌入式工具包 MySQL-server-5.6.19-1.el6.x86_64.rpm 服務端 MySQL-shared-5.6.19-1.el6.x86_64.rpm 工具包 MySQL-shared-compat-5.6.19-1.el6.x86_64.rpm MySQL-test-5.6.19-1.el6.x86_64.rpm 測試庫 安裝服務端和客戶端: 1> 卸載掉已經安裝的mysql # rpm -aq|grep mysql|xargs rpm -e --nodeps # rm -rf /var/lib/mysql/* 或者 # rpm -e `rpm -aq|grep mysql` --nodeps 2> 安裝5.6.19rpm包 # rpm -ivh MySQL-client-5.6.19-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:MySQL-client ########################################### [100%] # rpm -ivh MySQL-server-5.6.19-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:MySQL-server ########################################### [100%] # cat /root/.mysql_secret 默認root用戶的密碼 # The random password set for the root user at Wed Aug 16 11:10:58 2017 (local time): KTVnqzD2lUVvMBYP # /usr/bin/mysql_secure_installation Enter current password for root (enter for none): ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 緣由:數據庫沒有啓動 解決:啓動數據庫 # /usr/bin/mysql_secure_installation 對數據庫作安全配置 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. You already have a root password set, so you can safely answer 'n'. Change the root password? [Y/n] y 是否更改mysql數據庫的root密碼 New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y 是否移除匿名用戶 ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n 是否禁止root用戶遠程登陸|生產禁止 ... skipping. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y 移除test庫 - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y 刷新受權表 ... Success! All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL! Cleaning up... 啓動數據庫: # service mysql start Starting MySQL.... [ OK ] mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 說明: 從mysql的5.1版本後,官方所發佈的mysql的服務名稱發生了變化,之前是mysqld如今是mysql,可是redhat 發行版本中依然是mysqld。 安全配置: # mysql_secure_installation 說明:默認狀況下mysql數據庫安裝在/usr下;數據文件在/var/lib/mysql下
參考:http://www.javashuo.com/article/p-taowqfql-ec.htmlc++
四、源碼包安裝 5.6.25 install_dir: /mysql25 data_dir: /mysql25/data socket:/mysql25/mysql.sock port:3308 config_file:/mysql25/etc/my.cnf # md5sum mysql-5.6.25.tar.gz 37664399c91021abe070faa700ecd0ed mysql-5.6.25.tar.gz 步驟: 1、建立相應的數據目錄 mkdir /mysql25/data -p 二、解壓相應的軟件包到指定的臨時目錄裏(/usr/src) # tar -xf mysql-5.6.25.tar.gz -C /usr/src/ # cd /usr/src/mysql-5.6.25/ 3、根據需求進行配置 參考官方文檔 默認狀況下載當前目錄下直接能夠配置 shell> cmake . -LAH 查看全部支持的配置選項 shell> cmake . CMAKE_INSTALL_PREFIX /usr/local/mysql 安裝路徑 DEFAULT_CHARSET latin1 拉丁 默認字符集 DEFAULT_COLLATION latin1_swedish_ci 指定服務器默認的校對規則(排序規則) ENABLED_LOCAL_INFILE OFF 是否開啓內部加載外部文件功能 默認off 1表明開啓0表明關閉 MYSQL_DATADIR 數據文件目錄 SYSCONFDIR 初始化參數文件目錄(主配置文件路徑(默認優先去/etc目錄去找)) MYSQL_TCP_PORT 服務端口號,默認3306 MYSQL_UNIX_ADDR socket文件路徑,默認/tmp/mysql.sock WITHOUT_xxx_STORAGE_ENGINE 指定不編譯的存儲引擎 WITH_xxx_STORAGE_ENGINE 指定靜態編譯到mysql的存儲引擎,MyISAM,MERGE,MEMBER以及CSV四種引擎默認即被編譯至服務器,不須要特別指定。 WITH_EXTRA_CHARSETS 擴展字符集 字符集與字符編碼 字符是各類文字和符號的總稱,包括各個國家文字、標點符號、圖形符號、數字等。字符集是多個字符的集合,字符集種類較多,每一個字符集包含的字符個數不一樣, 計算機要準確的處理各類字符集文字,須要進行字符編碼,以便計算機可以識別和存儲各類文字。也就是說字符編碼是字符集的實現方式。 但須要注意的是:有的字符編碼和字符集的名稱是一致的。 常見的字符集 Unicode:也叫統一字符集,它包含了幾乎世界上全部的已經發現且須要使用的字符(如中文、日文、英文、德文等)。 ASCII:早期的計算機系統只能處理英文,因此ASCII也就成爲了計算機的缺省字符集,包含了英文所須要的全部字符。 GB2312:中文字符集,包含ASCII字符集。ASCII部分用單字節表示,剩餘部分用雙字節表示。 GBK:GB2312的擴展,但完整包含了GB2312的全部內容。 GB18030:GBK字符集的超集,常叫大漢字字符集,也叫CJK(Chinese,Japanese,Korea)字符集,包含了中、日、韓三國語言中的全部字符。 常見的字符編碼: UTF-8 UTF-16 UCS-2 UCS-4 GBK/GB2312 GB18030 字符編碼 每一個字符字節數 ASCII 1 UCS-2(Unicode) 2 UCS-4(Unicode) 4 UTF-8(Unicode) 1 - 6 UTF-16(Unicode) 2 - 4 GBK/GB2312(中文) 1 - 2 GB18030(CJK) 1- 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 編寫cmake腳本: vim cmake.sh #!/bin/bash cmake . \ -DCMAKE_INSTALL_PREFIX=/mysql25 \ -DENABLED_LOCAL_INFILE=1 \ -DMYSQL_DATADIR=/mysql25/data \ -DSYSCONFDIR=/mysql25/etc \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DMYSQL_UNIX_ADDR=/mysql25/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_EXTRA_CHARSETS=all \ -DMYSQL_USER=mysql \ -DMYSQL_TCP_PORT=3308 chmod +x cmake.sh # ./cmake.sh ./cmake.sh: line 2: cmake: command not found yum -y install cmake # ./cmake.sh -- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH) CMake Error at cmake/readline.cmake:85 (MESSAGE): Curses library not found. Please install appropriate package, remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel. Call Stack (most recent call first): # yum -y install ncurses-devel 說明:若是第一次配置出錯,再次配置前刪掉上一次的緩存文件 rm -f CMakeCache.txt 4、編譯 指定cpu個數編譯 cat /proc/cpuinfo |grep processor|wc -l make -j 2--------------------------指定多個CPU快速表編譯 5、安裝 make install 6、後續配置 初始化數據庫(安裝默認的庫和表): cd /mysql25 # ./mysql_install_db --help --no-defaults:不要從任何的配置文件中讀取相應的參數,忽略掉mysql安裝過程當中的默認配置,如建立默認用戶並設置默認密碼等 # ./mysql_install_db --basedir=/mysql25 --datadir=/mysql25/data --no-defaults --user=mysql 啓動數據庫: 報錯: # service mysql25 start Starting MySQL.The server quit without updating PID file (/mysql/data/vm1.uplook.com.pid).[FAILED] 解決: chown mysql.mysql /mysql -R 環境變量: export PATH=$PATH:/usr/local/mysql/bin # mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) [root@vm1 data]# mysql --socket=/data/mysql.sock # ln -s /data/mysql.sock /var/lib/mysql/mysql.sock # mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.25 Source distribution Copyright (c) 2000, 2014, 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. mysql> exit Bye ############################################################################## 總結: 1、修改配置文件從新指定了pid文件的路徑 2、mysql的數據目錄的權限問題或者pid文件的權限 3、mysql數據庫沒有正常關閉致使 5.7.17安裝: shell> groupadd mysql shell> useradd -r -g mysql -s /bin/false mysql shell> cd /usr/local shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz shell> ln -s full-path-to-mysql-VERSION-OS mysql shell> cd mysql shell> mkdir mysql-files shell> chmod 750 mysql-files shell> chown -R mysql . shell> chgrp -R mysql . shell> bin/mysql_install_db --user=mysql # MySQL 5.7.5 shell> bin/mysqld --initialize --user=mysql # MySQL 5.7.6 and up shell> bin/mysql_ssl_rsa_setup # MySQL 5.7.6 and up shell> chown -R root . shell> chown -R mysql data mysql-files shell> bin/mysqld_safe --user=mysql & # Next command is optional shell> cp support-files/mysql.server /etc/init.d/mysql.server
1. 編譯安裝 [root@mysql1 ~]# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake: mysql: [root@mysql1 ~]# groupadd mysql [root@mysql1 ~]# useradd -r -g mysql -s /bin/false mysql [root@mysql1 ~]# tar xvf mysql-5.7.17.tar.gz [root@mysql1 ~]# cd mysql-5.7.17 [root@mysql-5.7.17 ~]# cmake . \ -DWITH_BOOST=boost/boost_1_59_0/ \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DSYSCONFDIR=/etc \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DINSTALL_MANDIR=/usr/share/man \ -DMYSQL_TCP_PORT=3306 \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_READLINE=1 \ -DWITH_SSL=system \ -DWITH_EMBEDDED_SERVER=1 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 [root@mysql1 ~]# make [root@mysql1 ~]# make install 2. 初始化 [root@mysql1 local]# cd mysql [root@mysql1 mysql]# chown -R mysql . [root@mysql1 mysql]# chgrp -R mysql . [root@mysql1 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data [root@mysql1 mysql]# bin/mysql_ssl_rsa_setup [root@mysql1 mysql]# chown -R root . [root@mysql1 mysql]# chown -R mysql data mysql-files [root@mysql1 mysql]# \cp -rf support-files/my-default.cnf /etc/my.cnf [root@mysql1 mysql]# bin/mysqld_safe --user=mysql & [root@mysql1 mysql]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile [root@mysql1 mysql]# source /etc/profile [root@mysql1 mysql]# mysql -uroot -p'xxxx' mysql> alter user root@'localhost' identified by 'tianyun';
MySQL5.6.31源碼安裝 下載連接:https://pan.baidu.com/s/1yPGylgq-BbiGwZr-QN4ztw 密碼:18dc 安裝前的準備: 建立用戶: [root@Admin ~]# groupadd mysql && useradd mysql -g mysql -s /bin/false # 建立數據目錄: [root@Admin ~]# mkdir -p /data/DB # 安裝必備軟件: [root@Admin ~]# yum install unzip cmake ncurses-devel gcc gcc-c++ -y # 安裝mysql [root@Admin ~]# unzip mysql-5.6.31.zip && cd mysql-5.6.31 [root@Admin mysql-5.6.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var/ -DWITH_DEBUG=0 -DEXTRA_CHARSETS=latin1,gb2312,big5,utf8,GBK -DEXTRA_CHARSETS=all -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 [root@Admin mysql-5.6.31]# make && make install [root@Admin mysql-5.6.31]# cp support-files/mysql.server /etc/init.d/mysqld # 編寫配置文件 [root@Admin mysql-5.6.31]# vim /etc/my.cnf [client] socket = /var/lib/mysql/mysql.sock default-character-set=utf8 [mysqld] port=3306 lower_case_table_names=1 #大小寫區分代表 thread_concurrency=64 #經過比較 Connections 和 Threads_created 狀態的變量 slow_query_log=1 slow_query_log_file=/data/mysqldata/slowquery.log #爲MySQL慢查詢日誌存放的位置 long_query_time=1 #2表示查詢超過兩秒才記錄 character-set-server=utf8 explicit_defaults_for_timestamp #開查詢緩存 socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 innodb_buffer_pool_size=50M innodb_additional_mem_pool=16M log-bin=mysql-bin datadir=/data/DB log-bin-trust-function-creators=1 lower_case_table_names=1 skip-external-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M binlog_format=mixed server-id = 1 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid # 配置,初始化 [root@Admin mysql-5.6.31]# chmod +x scripts/mysql_install_db && chmod +x /etc/init.d/mysqld [root@Admin mysql-5.6.31]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/DB/ [root@Admin mysql-5.6.31]# chkconfig --add mysqld && chkconfig --level 35 mysqld on [root@Admin mysql-5.6.31]# cp /usr/local/mysql/bin/mysql /usr/bin/ && cp /usr/local/mysql/bin/mysqldump /usr/bin/ && cp /usr/local/mysql/bin/mysqladmin /usr/bin/ # 啓動並進入 [root@Admin mysql-5.6.31]# service mysqld restart MySQL server PID file could not be found! [失敗] Starting MySQL. [肯定] [root@Admin mysql-5.6.31]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.31-log Source distribution Copyright (c) 2000, 2016, 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. mysql>
1》密碼設置 新創建的數據庫默認管理員賬號是沒有密碼,任何人均可以鏈接 修改密碼三種方法: 第一種: # mysqladmin -u root password "123" --把root用戶登陸密碼改成123 # mysqladmin -u root password 'newpasswd' -p123 --有密碼後,再使用這個命令改密碼,就須要原密碼 第二種: 使用sql語句在數據庫內部直接修改用戶密碼錶,password()是一個密碼加密函數 mysql> update mysql.user set password=password("789") where user="root" and host="localhost"; 或者 mysql> use mysql; mysql> update user set password=password(123) where user='root' and host='localhost'; mysql> flush privileges; --修改過密碼後都要記得刷新權限表 第三種: mysql> set password for 'root'@'localhost'=password('123'); --使用此操做語句也能夠修改密碼,修改後不須要刷新權限表
# 忘記密碼怎麼辦? 方法1: 修改配置文件,在配置文件裏面添加跳過受權表 skip-grant-tables 方法2: 經過mysqld_safe 命令啓動傳遞參數 --skip-grant-tables