1.下載
可以官網(wǎng)下載也可以通過(guò)wget下載
官網(wǎng)地址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads
cd /usr/local/
wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz
下載可能失敗献起,請(qǐng)登錄官網(wǎng)查看最新版本號(hào)的烁,然后將以上的版本號(hào)5.6.41修改為最新的版本號(hào)就可以下載了
2.解壓并重命名文件夾
tar -xvf mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.6.41-linux-glibc2.12-x86_64 mysql
3.添加系統(tǒng)mysql組和mysql用戶(hù)
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
4.進(jìn)入mysql目錄更改權(quán)限
cd mysql
chown -R mysql:mysql ./
5.安裝數(shù)據(jù)庫(kù)断盛,此處可能出現(xiàn)錯(cuò)誤
./scripts/mysql_install_db --user=mysql
- 可能錯(cuò)誤1
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper
解決方法:
yum install -y perl-Data-Dumper
- 可能錯(cuò)誤2
linux libaio.so.1: cannot open shared object file: No such file or directory
解決方法:
yum install libaio*
6.修改當(dāng)前目錄擁有者為root用戶(hù),修改data目錄擁有者為mysql
chown -R root:root ./
chown -R mysql:mysql data
MySql安裝結(jié)束
7.添加mysql服務(wù)開(kāi)機(jī)自啟動(dòng)
cp support-files/mysql.server /etc/init.d/mysql
# 賦予可執(zhí)行權(quán)限
chmod +x /etc/init.d/mysql
# 添加服務(wù)
chkconfig --add mysql
# 顯示服務(wù)列表
chkconfig --list
如果看到mysql的服務(wù)药磺,并且3,4,5都是on的話則成功屋厘,如果是off芋哭,則執(zhí)行
chkconfig --level 345 mysql on
8.啟動(dòng)紧帕、關(guān)閉、重啟MySql
service mysql start
service mysql stop
service mysql restart
執(zhí)行開(kāi)啟mysql
service mysql start
- 可能錯(cuò)誤1
Starting MySQL.180822 21:33:57 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 (/var/lib/mysql/i[FAILED]c2bbcfdqcpcrcz.pid).
解決方法:
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb/
以上命令不行可以使用以下命令啟動(dòng)
sudo -u mysql service mysql start
9.本地連接數(shù)據(jù)庫(kù)
由于系統(tǒng)默認(rèn)會(huì)查找/usr/bin下的命令愉烙,如果這個(gè)命令不在這個(gè)目錄下讨盒,當(dāng)然會(huì)找不到命令,我們需要做的就是映射一個(gè)鏈接到/usr/bin目錄下步责,相當(dāng)于建立一個(gè)鏈接文件
ln -s /usr/local/mysql/bin/mysql /usr/bin
ln -s /usr/local/mysql/bin/mysqladmin /usr/bin
設(shè)置密碼
mysqladmin -u root password "newpwd"
連接數(shù)據(jù)庫(kù)
mysql -u root -p
輸入密碼報(bào)錯(cuò)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解決辦法
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
10.遠(yuǎn)程連接數(shù)據(jù)庫(kù)
開(kāi)啟遠(yuǎn)程訪問(wèn)權(quán)限返顺,需要在防火墻中開(kāi)端口,具體操作查看【13.】
#先登錄mysql
mysql -u root -p
#設(shè)置所有ip可以登錄mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
#grant all privileges on 庫(kù)名.表名 to '用戶(hù)名'@'IP地址' identified by '密碼' with grant option;
#刷新權(quán)限
FLUSH PRIVILEGES;
修改默認(rèn)端口號(hào)
11.先登錄mysql 蔓肯,查看當(dāng)前端口
mysql> show global variables like 'port';
12.編輯mysql配置文件
/etc/my.cnf
在第二行新增端口配置
[mysqld]
datadir=/var/lib/mysql
新增后如下
[mysqld]
port=3506
datadir=/var/lib/mysql
修改后重啟mysql生效
service mysql restart
13.開(kāi)放端口
- 防火墻命令
# 關(guān)閉防火墻
systemctl stop firewalld.service
# 開(kāi)啟防火墻
systemctl start firewalld
# 查看防火墻狀態(tài)
systemctl status firewalld
- 開(kāi)啟3506端口
firewall-cmd --zone=public --add-port=3506/tcp --permanent # 開(kāi)放3506端口
firewall-cmd --zone=public --remove-port=3506/tcp --permanent #關(guān)閉3506端口
firewall-cmd --reload # 配置立即生效
查看防火墻所有開(kāi)放的端口
firewall-cmd --zone=public --list-ports