Linux 安裝和配置MySQL

1、準備好MySQL安裝包,官網下載地址:https://downloads.mysql.com/archives/community/

2、把MySQL安裝壓縮包包上傳到Linux服務器的/opt目錄

3、XShell連上Linux服務器

4、運行下面命令查看是否已安裝MySQL

rpm -qa|grep mysql

5、運行下面命令查看是否已安裝mariadb

rpm -qa|grep mariadb

本機輸出:mariadb-libs-5.5.65-1.el7.x86_64

可使用下面命令卸載

yum remove mariadb-libs-5.5.65-1.el7.x86_64

6、切換到/opt目錄,解壓MySQL壓縮包,重命名

cd /opt
tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.27-linux-glibc2.12-x86_64 mysql

7、創建mysql用戶組和用戶,創建存放日誌和數據目錄,修改目錄所屬用戶

groupadd mysql
useradd mysql -g mysql -s /sbin/nologin
mkdir -pv /opt/mysql/data /opt/mysql/logs
chown -R mysql:mysql /opt/mysql

8、創建MySQL配置文件

vim /etc/my.cnf

編輯爲下面內容後保存(basedir、datadir、log-error、pid-file路徑和上面保持一致):

[mysqld]
basedir=/opt/mysql
datadir=/opt/mysql/data
port = 3306
socket=/tmp/mysql.sock
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
symbolic-links=0
log-error=/opt/mysql/logs/mysqld.log
pid-file=/opt/mysql/mysqld.pid
lower_case_table_names=1
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
[client]
default-character-set = utf8mb4
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock

9、MySQL初始化、配置環境變量

/opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
echo "export PATH=/opt/mysql/bin:\$PATH" >> /etc/profile
source /etc/profile

備註:
如果運行出錯,檢查/etc/my.cnf裏面的配置是否有錯,修改完後刪除/opt/mysql/data和/opt/mysql/logs裏面的文件,再執行初始化命令。

10、MySQL啓動關閉配置

vim /usr/lib/systemd/system/mysqld.service

編輯爲下面內容後保存:

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# systemd service file for MySQL forking server
#

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

11、重新加載新的服務,設置MySQL開機自啓,啓動服務,查看啓動狀態

systemctl daemon-reload
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld

12、查看上面生成的臨時密碼,後面mysql_secure_installation初始化要用到

head /opt/mysql/logs/mysqld.log

裏面有一行:

A temporary password is generated for root@localhost: XajJlcb(C6KV

13、執行mysql_secure_installation初始化配置嚮導設置root用戶、密碼、權限。

mysql_secure_installation

部分內容如下:

Enter password for user root: ----輸入上面的臨時密碼
New password: ----設置新密碼
Re-enter new password: ----再次輸入新密碼
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y ----是否刪除匿名用戶
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n----是否禁止root遠程登錄,根據需求選擇
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n----是否刪除test數據庫,根據需求選擇
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y----是否重新加載權限表

14、登錄,輸入下面命令行後,輸入新密碼

mysql -uroot -p

查詢數據庫

show databases;

查詢用戶表

select user,host from mysql.user;

 如果需要允許在別的機器遠程連接,則需要更新字段值

use mysql;
update user set host = '%' where user = 'root' and host='localhost'; FLUSH PRIVILEGES;

 另外防火牆3306端口也需要開啓。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章