您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關MYSQL如何搭建,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
mysql安裝可以分為 1.rpm方式,2.tar壓縮包方式,兩者的差別rpm類似“一鍵安裝”模式,tar方式則是”高級定義“,企業中大多數為“高級定義”方式安裝。
rpm方式安裝
一:前提準備
1.檢測是否已經安裝
rpm -qa | grep mysql
2.檢測是否有mysql服務
ps -ef | grep mysql
3.檢測是否可連接外網
ping www.baidu.com
4. 配置yum源
自帶的yum源可以滿足需要,如不滿足需自行配置
二:安裝步驟
1.搜索到源,如果報錯可能是網絡不通。
yum search mysql
2.在線安裝
yum install -y mysql-server.x86_64 --服務端
yum install -y mysql.x86_64 --客戶端
三:啟動服務
1.service mysqld start (備注: mysqld ‘D’表示后臺運行的進程)
2.查看服務,端口號:
ps -ef | grep mysql
netstat -nls | grep 3306
備注:查看到3306默認端口號即mysql服務啟動
四: 進入用戶
安裝好后悔默認創建root用戶,密碼為空
[root@hadoop001 ~]# mysql -uroot -p
Enter password: 直接回車 -
備注:生成采用交互式輸密碼,防止明文history命令將密碼明文。
tar方式安裝mysql包
企業里專門的軟件會有專門的用戶去部署、運行
oa系統 oaadmin
erp系統 erpadmin
規范用戶可以規范權限,易于管理
mysql 一個關系型數據庫被oracle收購,有免費版有收費版。
oracle
postgresql
sqlserver
id name age
1 ruoze 18
2 20
二:tar方式安裝
1.在聯網情況下可以網絡直接下載安裝包
[root@sht-sgmhadoopnn-01 ~]# cd /usr/local
[root@sht-sgmhadoopnn-01 local]# wget https://downloads.mariadb.com/archives/mysql-5.6/mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
[root@sht-sgmhadoopnn-01 local]# wget https://downloads.mariadb.com/archives/mysql-5.6/mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz.md5
--這里我們用專用包下載
mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
2.本次安裝采用現成的安裝包進行安裝
secureCRT中輸入rz命令
1. root用戶進入目錄
[root@sht-sgmhadoopnn-01 ~]# cd /usr/local
2.用md5檢測包的完整性。
[root@sht-sgmhadoopnn-01 local]# cat mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz.md5 --原始包中自帶的md5值
61affe944eff55fcf51b31e67f25dc10 mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
[root@sht-sgmhadoopnn-01 local]# md5sum mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
61affe944eff55fcf51b31e67f25dc10 mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
2. 檢測是否已經安裝
[root@sht-sgmhadoopnn-01 local]# ps -ef|grep mysqld
root 2493 2423 0 19:48 pts/3 00:00:00 grep mysqld
[root@sht-sgmhadoopnn-01 local]# rpm -qa |grep -i mysql
3.解壓包到指定目錄,然后重命名
[root@sht-sgmhadoopnn-01 local] cd /usr/local
[root@sht-sgmhadoopnn-01 local] tar xzvf mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz --路徑是root用戶下/usr/local
[root@sht-sgmhadoopnn-01 local]# mv mysql-5.6.23-linux-glibc2.5-x86_64 mysql --將解壓后的包重命名為mysql(因為之后的mysqladmin用戶家目錄是 /usr/loca/mysql,這樣在su - mysqladmin切換到家目錄后,直接進入解壓后的包目錄,便于安裝.)
4.創建用戶組
[root@sht-sgmhadoopnn-01 local]# groupadd -g 101 dba
[root@sht-sgmhadoopnn-01 local]# useradd -u 514 -g dba -G root -d /usr/local/mysql mysqladmin
useradd: warning: the home directory already exists. --如果用戶存在要當心了
Not copying any file from skel directory into it. --由于用戶已經存在會導致環境變量不會在家目錄中存在,坑!
## if user mysqladmin is existing,please execute the following command of usermod.
##[root@sht-sgmhadoopnn-01 local]# usermod -u 514 -g dba -G root -d /usr/local/mysql
[root@sht-sgmhadoopnn-01 local]# userdel mysqladmin --如用戶已存在則刪除
5.為用戶創建密碼
[root@sht-sgmhadoopnn-01 local]# id mysqladmin
uid=514(mysqladmin) gid=101(dba) groups=101(dba),0(root)
[root@sht-sgmhadoopnn-01 local]# passwd mysqladmin
6.拷貝環境變量.bash_profile等,如果ls -lart有,此步驟可以省略
## copy 環境變量配置文件至mysqladmin用戶的home目錄中,為了以下步驟配置個人環境變量
[root@sht-sgmhadoopnn-01 local]# cp /etc/skel/.* /usr/local/mysql --將環境變量拷貝到mysqladmin家目錄下
cp: omitting directory `/etc/skel/.'
cp: omitting directory `/etc/skel/..'
cp: omitting directory `/etc/skel/.mozilla'
6.1配置環境變量
[mysqladmin@sht-sgmhadoopnn-01 ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export MYSQL_HOME=/usr/local/mysql
export PATH=${MYSQL_HOME}/bin:$PATH
export PATH
ps1=`uname -n`":"$USER":"$PWD":>"; --注意變量名要大寫
export PS1
#‘:’是分隔符,每次加環境變量要放到$PATH的前面(新變量放在前面是因為linux命令執行是按$PATH的先后順序查找的,如果$PATH中已經包含老版本的變量值,優先尋找的還是老版本的環境變量),$PATH的意思原先的環境變量+新的變量
使變量生效
. .bash_profile
su - mysqladmin
拷貝環境變量后exit用戶
重新進入su - mysqladmin 可以看到當前光標處用戶路徑已經帶出來
7.配置mysql主配置文件my.cnf (640權限)
尋找配置文件的順序
#defualt start: /etc/my.cnf->/etc/mysql/my.cnf->SYSCONFDIR/my.cnf->$MYSQL_HOME/my.cnf-> --defaults-extra-file->~/my.cnf
統一的我們在第一個位置配文件
[root@sht-sgmhadoopnn-01 mysql]# cd /etc/ --在etc目錄下建立my.cnf
[root@sht-sgmhadoopnn-01 etc]# touch my.cnf
配置文件內容:
[root@sht-sgmhadoopnn-01 etc]# vi my.cnf
[client] #客戶端配置位置
port = 3306
socket = /usr/local/mysql/data/mysql.sock
[mysqld] #服務端配置位置
port = 3306
socket = /usr/local/mysql/data/mysql.sock
skip-external-locking
key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
query_cache_size= 32M
max_allowed_packet = 16M
myisam_sort_buffer_size=128M
tmp_table_size=32M
table_open_cache = 512
thread_cache_size = 8
wait_timeout = 86400
interactive_timeout = 86400
max_connections = 600
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 32
#isolation level and default engine
default-storage-engine = INNODB
transaction-isolation = READ-COMMITTED
server-id = 1
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/hostname.pid
#open performance schema
log-warnings
sysdate-is-now
binlog_format = MIXED
log_bin_trust_function_creators=1
log-error = /usr/local/mysql/data/hostname.err
log-bin=/usr/local/mysql/arch/mysql-bin
#other logs
#general_log =1
#general_log_file = /usr/local/mysql/data/general_log.err
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/slow_log.err
#for replication slave
#log-slave-updates
#sync_binlog = 1
#for innodb options
innodb_data_home_dir = /usr/local/mysql/data/ #存放數據目錄
innodb_data_file_path = ibdata1:500M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/arch
innodb_log_files_in_group = 2
innodb_log_file_size = 200M
#生產上 機械硬盤
#sata盤 5000r 7200r(大眾生產) 10000r(大眾生產) 15000r 有條件用ssd
# innodb_buffer_pool_size 生產調優調大 8G
innodb_buffer_pool_size = 1024M #free命令查看服務器內存,再適度分配
innodb_additional_mem_pool_size = 50M
innodb_log_buffer_size = 16M
innodb_lock_wait_timeout = 100
#innodb_thread_concurrency = 0
innodb_flush_log_at_trx_commit = 1
innodb_locks_unsafe_for_binlog=1
#innodb io features: add for mysql5.5.8
performance_schema
innodb_read_io_threads=4
innodb-write-io-threads=4
innodb-io-capacity=200
#purge threads change default(0) to 1 for purge
innodb_purge_threads=1
innodb_use_native_aio=on
#case-sensitive file names and separate tablespace
innodb_file_per_table = 1
lower_case_table_names=1
[mysqldump]
quick
max_allowed_packet = 16M #最大數據包
[mysql]
no-auto-rehash
[mysqlhotcopy]
interactive-timeout
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
---------------------------
8.更改my.cnf權限
[root@sht-sgmhadoopnn-01 local]# chown mysqladmin:dba /etc/my.cnf
[root@sht-sgmhadoopnn-01 local]# chmod 640 /etc/my.cnf
[root@sht-sgmhadoopnn-01 etc]# ll my.cnf
-rw-r----- 1 mysqladmin dba 2201 Aug 25 23:09 my.cnf
9.更改家目錄下用戶、組的權限
[root@sht-sgmhadoopnn-01 local]# chown -R mysqladmin:dba /usr/local/mysql
[root@sht-sgmhadoopnn-01 local]# chmod -R 755 /usr/local/mysql
10. 切換到mysql家目錄,并創建arch目錄,arch目錄是在my.cnf指定的
root@sht-sgmhadoopnn-01 local]# su - mysqladmin
[mysqladmin@sht-sgmhadoopnn-01 ~]$ pwd
/usr/local/mysql
創建arch目錄用來存儲binlog二進制歸檔日志 mysql--maxwell--kafka--sparkstrm--hbase 實時
cd /usr/local/mysql
mkdir arch
11.開始安裝,用家目錄下的腳本
[mysqladmin@sht-sgmhadoopnn-01 ~]$ scripts/mysql_install_db
Installing MySQL system tables..../bin/mysqld:
error while loading shared libraries: libaio.so.1: cannot open shared object file:
No such file or directory #缺少libaio.so 包
====================
缺少包解決方法:
1.查看版本
[root@sht-sgmhadoopnn-01 local]# cat /proc/version
Linux version 2.6.18-164.11.1.el5 (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Wed Jan 20 07:32:21 EST 2010
2.查看是否有gcc包
[root@sht-sgmhadoopnn-01 local]# rpm -qa |grep gcc
libgcc-4.1.2-46.el5_4.2
libgcc-4.1.2-46.el5_4.2
3.安裝對應包
[root@sht-sgmhadoopnn-01 local]# yum -y install libaio
假設前面安裝錯誤再次安裝,需要清空兩個目錄遺留文件:
rm -rf data/*
rm -rf arch/*
=====================
12.開始安裝,為保險起見,指定了執行路徑,實在是不用指定的。
[mysqladmin@sht-sgmhadoopnn-01 ~]$ scripts/mysql_install_db \
--user=mysqladmin \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
查看data/hostname.err文件 仔細錯誤
或者:
scripts/mysql_install_db \
--user=mysqladmin \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--defaults-file=/etc/my.cnf --手工指定defaults-file方法
或者:
[root@bogon scripts]# /usr/local/mysql/scripts/mysql_install_db --user=mysqladmin --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &
備注:因為mysql家目錄為/usr/local/mysql,而我們上面步驟已經將tar包更名為mysql,在su - mysqladmin切換到家目錄時,已經進入tar解壓后的安裝包,此時可以看到script目錄
13.配置mysql開機自啟
[root@sht-sgmhadoopnn-01 ~]# cd /usr/local/mysql
#將服務文件拷貝到init.d下,并重命名為mysql
[root@sht-sgmhadoopnn-01 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysql
#賦予可執行權限
[root@sht-sgmhadoopnn-01 mysql]# chmod +x /etc/rc.d/init.d/mysql
#刪除服務
[root@sht-sgmhadoopnn-01 mysql]# chkconfig --del mysql
#重新添加服務
[root@sht-sgmhadoopnn-01 mysql]# chkconfig --add mysql
#添加開機啟動服務
[root@sht-sgmhadoopnn-01 mysql]# chkconfig --level 345 mysql on
[root@sht-sgmhadoopnn-01 mysql]# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
su - mysqladmin -c "/etc/init.d/mysql start --federated"
---------------
[root@hadoop ~]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
[root@hadoop ~]# chmod +x /etc/rc.d
rc.d/ rc.local rc.sysinit
[root@hadoop ~]# chmod +x /etc/rc.d/init.d/mysql
[root@hadoop ~]# ll /etc/rc.d/init.d/mysql
-rwxr-xr-x 1 root root 10880 Jan 29 12:47 /etc/rc.d/init.d/mysql
---------------
9. 啟動mysql
[root@sht-sgmhadoopnn-01 mysql]# su - mysqladmin
[mysqladmin@sht-sgmhadoopnn-01 ~]$ pwd
/usr/local/mysql
[mysqladmin@sht-sgmhadoopnn-01 ~]$ rm -rf my.cnf #保留也可以,因為目錄下已經有了
[mysqladmin@hadoop001 ~]$ service mysql start #開啟服務
Starting MySQL. [ OK ]
[mysqladmin@hadoop001 ~]$ service mysql status
MySQL running (3625) [ OK ]
或者:
#另一種安全模式啟動,同樣是可以的
[mysqladmin@sht-sgmhadoopnn-01 ~]$ mysqld_safe & #安全模式開啟,另一種開啟方式,執行后光標處回車
[1] 11802
[mysqladmin@sht-sgmhadoopnn-01 ~]$ 150825 22:53:38 mysqld_safe Logging to '/usr/local/mysql/data/hostname.err'.
150825 22:53:38 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data/
150825 22:53:39 mysqld_safe mysqld from pid file /usr/local/mysql/data/hostname.pid ended
[mysqladmin@sht-sgmhadoopnn-01 ~]$ which mysqld_safe
~/bin/mysqld_safe #可以看到mysqld_safe的目錄
10. 查看服務監聽
[mysqladmin@sht-sgmhadoopnn-01 ~]$ ps -ef|grep mysqld --是mysqld不是mysql
514 6247 6219 0 17:30 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe
514 6902 6247 2 17:30 pts/1 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/hostname.err --pid-file=/usr/local/mysql/data/hostname.pid --socket=/usr/local/mysql/data/mysql.sock --port=3306 #3306說明已經啟動
514 6927 6219 0 17:31 pts/1 00:00:00 grep mysqld
查看監聽
[mysqladmin@sht-sgmhadoopnn-01 ~]$ netstat -tulnp | grep mysql #查看端口
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 :::3306 :::* LISTEN 11541/mysqld
[root@sht-sgmhadoopnn-01 local]# service mysql status
MySQL running (21507) [ OK ]
至此部署完成
備注:
mysql會創建兩個用戶
1.root用戶 密碼為空 要更改密碼
2.空用戶 密碼為空 要刪除該用戶
登錄方式:
#注意事項1.不用明密碼登錄。2.-p等參數不能加空格
bin/mysql -uroot -p -h227.0.0.1 #回車輸入密碼 此方式安全不會被history記錄
bin/mysql -uroot -p123456 -h227.0.0.1 #此方式不安全的 會被history記錄
bin/mysql -u root -p 123456 -h 127.0.0.1 -P3306 #加端口號方式,此方式不安全,127.0.0.1是自己的電腦ip
11. 登錄mysql
[mysqladmin@sht-sgmhadoopnn-01 ~]$ bin/mysql -uroot -p
也可以進入/usr/local/mysql/bin目錄,./mysql -uroot -p表示執行當前路徑下的mysql ./是相對路徑 。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
12.更改root密碼,刪除空用戶
mysql> use mysql
Database changed
安全起見要做兩件事
1.更改root的密碼不為空
2.刪除空用戶
mysql> update user set password=password('123456') where user='root'; #將root用戶更改密碼,passwodr('123456')是加密
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select host,user,password from user;
+----------------+------+-------------------------------------------+
| host | user | password |
+----------------+------+-------------------------------------------+
| localhost | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| sht-sgmhadoopnn-01 | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| 127.0.0.1 | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| ::1 | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| localhost | | |
| sht-sgmhadoopnn-01 | | |
+----------------+------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> delete from user where user=''; #刪除空用戶
mysql> select host,user,password from user;
+----------------+------+-------------------------------------------+
| host | user | password |
+----------------+------+-------------------------------------------+
| localhost | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| sht-sgmhadoopnn-01 | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| 127.0.0.1 | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| ::1 | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
+----------------+------+-------------------------------------------+
4 rows in set (0.00 sec)
#針對用戶 權限的操作語句最后一步執行刷新權限
mysql> flush privileges;
13.重新登錄
bin/mysql -uroot -p -hlocalhost
14.Configure .bash_profile #配置環境變量
[mysqladmin@sht-sgmhadoopnn-01 ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export MYSQL_HOME=/usr/local/mysql
export PATH=${MYSQL_HOME}/bin:$PATH
export PATH
ps1=`uname -n`":"$USER":"$PWD":>"; --注意變量名要大寫
export PS1
#‘:’是分隔符,每次加環境變量要放到$PATH的前面(新變量放在前面是因為linux命令執行是按$PATH的先后順序查找的,如果$PATH中已經包含老版本的變量值,優先尋找的還是老版本的環境變量),$PATH的意思原先的環境變量+新的變量
使變量生效
. .bash_profile
su - mysqladmin
關于“MYSQL如何搭建”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。