1、檢查系統中是否已經安裝過mysql。mysql
rpm -qa | grep mysqllinux
若是存在,則須要刪除。
yum -y remove mysql*sql
繼續檢查一下是否還存在mysql
rpm -qa | grep -i mysqldocker
2、下載rpm包。數據庫
須要下載:
MySQL-client-5.6.15-1.el6.x86_64.rpm
MySQL-devel-5.6.15-1.el6.x86_64.rpm
MySQL-server-5.6.15-1.el6.x86_64.rpm
逐一執行命令:
wget http://cdn.mysql.com/archives/mysql-5.6/MySQL-client-5.6.15-1.el6.x86_64.rpmvim
wget http://cdn.mysql.com/archives/mysql-5.6/MySQL-devel-5.6.15-1.el6.x86_64.rpm服務器
wget http://cdn.mysql.com/archives/mysql-5.6/MySQL-server-5.6.15-1.el6.x86_64.rpmapp
3、依次安裝socket
rpm -ivh MySQL-server-5.6.15-1.el6.x86_64.rpm編碼
rpm -ivh MySQL-devel-5.6.15-1.el6.x86_64.rpm
rpm -ivh MySQL-client-5.6.15-1.el6.x86_64.rpm
4、 初始化MySQL及設置密碼
cp /usr/share/mysql/my-default.cnf /etc/my.cnf
cat /root/.mysql_secret 記錄最後的密碼
mysql -p ---輸入記錄的密碼,便可登陸mysql。
set password for 'root'@'localhost' =password('新密碼');
flush privileges; //刷新權限設置
5、配置數據庫編碼格式
登陸MySQL查看用SHOW VARIABLES LIKE 'character%';下字符集,顯示以下:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
character_set_database和character_set_server的默認字符集仍是latin1。
最簡單的完美修改方法,修改mysql的my.cnf文件中的字符集鍵值(注意配置的字段細節):
一、在[client]字段里加入default-character-set=utf8,以下:
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8
二、在[mysqld]字段里加入character-set-server=utf8,以下:
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
三、在[mysql]字段里加入default-character-set=utf8,以下:
[mysql]
no-auto-rehash
default-character-set=utf8
修改完成後,service mysql restart重啓mysql服務就生效,並flush privileges; //刷新權限設置。
使用SHOW VARIABLES LIKE 'character%';查看,發現數據庫編碼全已改爲utf8。
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
[root@iZ25jr2uqbnZ soft]# wget --no-check-certificate http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz --2017-06-20 18:14:41-- http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz Resolving dev.mysql.com... 137.254.60.11 Connecting to dev.mysql.com|137.254.60.11|:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz [following] --2017-06-20 18:14:41-- https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz Connecting to dev.mysql.com|137.254.60.11|:443... connected. WARNING: certificate common name `www.mysql.com' doesn't match requested host name `dev.mysql.com'. HTTP request sent, awaiting response... 302 Found Location: https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz [following] --2017-06-20 18:14:43-- https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz Resolving cdn.mysql.com... 23.211.97.88 Connecting to cdn.mysql.com|23.211.97.88|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 314581668 (300M) [application/x-tar-gz] Saving to: `mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz' 100%[======================================>] 314,581,668 12.3M/s in 32s 2017-06-20 18:15:15 (9.33 MB/s) - `mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz' saved [314581668/314581668]
2.同上檢查服務器是否安裝mysql(已安裝須要刪除)
3.解壓mysql二進制文件,並將解壓的內容移動到/usr/local/目錄下,並修改文件夾名稱爲mysql
[root@iZ2zea08w4zyzvdifl3dxgZ soft]# tar -zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@iZ2zea08w4zyzvdifl3dxgZ soft]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/
[root@iZ2zea08w4zyzvdifl3dxgZ local]# mv mysql-5.6.35-linux-glibc2.5-x86_64/ mysql
/usr/local/mysql/下的目錄結構 :
目錄 |
目錄描述 |
bin |
客戶端程序和mysqld的服務器 |
data |
日誌文件,數據庫 |
docs |
文檔格式信息 |
man |
UNIX手冊頁 |
include |
|
lib |
|
scripts |
mysql_install_db的 |
share |
各類支持文件,包括錯誤信息,示例配置文件中,SQL數據庫的安裝 |
sql-bench |
基準 |
4.添加mysql組和mysql用戶,用於設置mysql安裝目錄文件全部者和所屬組。
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# groupadd mysql
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# useradd -r -g mysql mysql
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# chown -R mysql .
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# chgrp -R mysql .
*useradd -r參數表示mysql用戶是系統用戶,不可用於登陸系統。
5.安裝數據庫。
*將mysql/目錄下除了data/目錄的全部文件,改回root用戶全部,mysql用戶只需做爲mysql/data/目錄下全部文件的全部者。
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# chown -R root .
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# chown -R mysql data
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# bin/mysqld_safe --user=mysql &
6.配置文件
*首先須要將mysql.server服務腳本複製到/etc/init.d/,並重命名爲mysqld。
*經過chkconfig命令將mysqld服務加入到自啓動服務項中。
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# cd support-files/
[root@iZ2zea08w4zyzvdifl3dxgZ support-files] cp support-files/mysql.server /etc/init.d/mysql.server
[root@iZ2zea08w4zyzvdifl3dxgZ support-files] cp my-default.cnf /etc/my.cnf
[root@iZ2zea08w4zyzvdifl3dxgZ init.d]# mv mysql.server mysqld //將mysql.server更名爲mysqld
[root@iZ2zea08w4zyzvdifl3dxgZ mysql]# chkconfig --add mysqld
*查看是否添加成功
[root@iZ2zea08w4zyzvdifl3dxgZ support-files]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
配置mysql環境變量
[root@iZ2zea08w4zyzvdifl3dxgZ support-files]# vim /etc/profile
添加一下內容
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$MYSQL_HOME/bin
7.啓動mysql
[root@iZ2zea08w4zyzvdifl3dxgZ support-files]# service mysqld start
Starting MySQL [ OK ]
8.進入mysql
[root@iZ2zea08w4zyzvdifl3dxgZ support-files]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.35 MySQL Community Server (GPL)
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>
9.修改數據庫鏈接密碼及host鏈接權限(參考errorCode:1130文章)
echo "下載MySQL鏡像" docker pull mysql/mysql-server:5.7 #爲卷建立本地目錄。 mkdir /var/mysql5.7 echo "啓動MySQL容器" docker run --name mysql5.7 -p 3306:3306 -p 33060:33060 -v /var/mysql5.7/:/var/lib/mysql/ -d mysql/mysql-server:5.7
echo "下載MySQL鏡像" docker pull mysql #爲卷建立本地目錄。 mkdir /Users/lihongmei/workSystem/docker_config/mysql/ # 建立配置文件路徑 echo "啓動MySQL容器" docker run -it -d --name mysql8.0 -p 3306:3306 -p 33060:33060 -v /Users/lihongmei/workSystem/docker_config/mysql_cnf/:/etc/mysql/conf.d -v /Users/lihongmei/workSystem/docker_config/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=lihongmei mysql:latest