1. 解除安裝舊 MySQL
-
檢視 rpm 包
rpm-qa | grep mysql
如果存在,使用如下命令解除安裝
rpm -e
-
查詢是否存在
mysql
相關目錄
find / -name mysql
-
解除安裝系統自帶 mariadb
[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
mariadb-libs-5.5.64-1.el7.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 mariadb-libs-5.5.64-1.el7.x86_64
[root@localhost ~]# rm -rf /etc/my.cnf
2. 建立 mysql 使用者和組
-
檢查有無建立過 mysql 使用者組,如果沒有建立
# 檢查mysql 使用者組是否存在
[root@localhost ~]# cat /etc/group | grep mysql
[root@localhost ~]# cat /etc/passwd | grep mysql
# 建立mysql 使用者組和使用者
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r -g mysql mysql
3. 安裝 MySQL
-
下載
從官網下載 MySQL
https://dev.mysql.com/downloads/mysql/
使用如下命令下載 MySQL
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
-
安裝
下載下來是一個gz
的壓縮包,使用如下命令解壓
tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
將解壓的檔案重新命名,並移動到/usr/local
目錄
mv mysql-5.7.36-linux-glibc2.12-x86_64 /usr/local/mysql5.7
更改 mysql5.7 目錄下所有資料夾所屬的使用者、使用者組、以及許可權
chown -R mysql:mysql /usr/local/mysql5.7
chmod -R 755 /usr/local/mysql5.7
-
建立 MySQL 相關目錄
mkdir -p /usr/local/mysql5.7/{data,logs,tmp}
# 更改資料夾所屬
chown -R mysql.mysql /usr/local/mysql5.7/data
chown -R mysql.mysql /usr/local/mysql5.7/logs
chown -R mysql.mysql /usr/local/mysql5.7/tmp
-
建立 MySQL 配置檔案 my.cnf
簡單模板如下
[client]
port = 3306
socket = /usr/local/mysql5.7/tmp/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql5.7
datadir = /usr/local/mysql5.7/data
port = 3306
socket = /usr/local/mysql5.7/tmp/mysql.sock
pid-file = /usr/local/mysql5.7/tmp/mysqld.pid
tmpdir = /usr/local/mysql5.7/tmp
skip_name_resolve = 1
symbolic-links=0
max_connections = 2000
group_concat_max_len = 1024000
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names = 1
log_timestamps=SYSTEM
character-set-server = utf8
interactive_timeout = 1800
wait_timeout = 1800
max_allowed_packet = 32M
binlog_cache_size = 4M
sort_buffer_size = 2M
read_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 96M
max_heap_table_size = 96M
max_length_for_sort_data = 8096
#logs
server-id = 1003306
log-error = /usr/local/mysql5.7/logs/error.log
slow_query_log = 1
slow_query_log_file = /usr/local/mysql5.7/logs/slow.log
long_query_time = 3
log-bin = /usr/local/mysql5.7/logs/binlog
binlog_format = row
expire_logs_days = 15
log_bin_trust_function_creators = 1
relay-log = /usr/local/mysql5.7/logs/relay-bin
relay-log-recovery = 1
relay_log_purge = 1
#innodb
innodb_file_per_table = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_flush_method = O_DIRECT
innodb_autoinc_lock_mode = 2
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_buffer_pool_size = 2G
-
配置
mysql.server
cd /usr/local/mysql5.7/support-files
cp mysql.server /etc/init.d/mysql
vi /etc/init.d/mysql
# 修改目錄位置
basedir=/usr/local/mysql5.7
datadir=/usr/local/mysql5.7/data
# 註冊開機啟動服務
chkconfig --add mysql
chkconfig --list
-
新增 mysql 到環境變數
把/usr/local/mysql5.7/bin
新增到PATH環境變數中
新增的內容export PATH=$PATH:/usr/local/mysql5.7/bin
vi /etc/profile
source /etc/profile
-
初始化 mysql
[root@localhost ~]# /usr/local/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/usr/local/mysql5.7/data
# 臨時密碼儲存在 errlog 中;獲取臨時密碼
[root@localhost ~]# more /usr/local/mysql5.7/logs/error.log | grep password
2022-04-07T23:28:28.894994+08:00 1 [Note] A temporary password is generated for root@localhost: w(fYri(tk8Dj
-
啟動 mysql 服務,並修改
root
使用者密碼
# 啟動mysql服務
[root@localhost ~]# service mysql start
Starting MySQL... SUCCESS!
# 使用初始密碼登入mysql服務 並修改密碼
[root@localhost ~]# mysql -uroot -p
alter user 'root'@'localhost' identified by 'root';
flush privileges;
為了方便使用,可以建立軟連結
ln -s /usr/local/mysql5.7/bin/mysql /usr/bin/mysql
4. 附:常用命令與開啟遠端連線
-
啟動
service mysql start
-
關閉
service mysql stop
-
重啟
service mysql restart
-
開啟遠端連線
進入 MySQL 服務
mysql -u root -proot
操作 mysql 庫
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user, host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)
mysql>
host 欄位中,localhost 表示只允許本機訪問;要實現遠端連線,可以將 root 使用者的 hos t改為 %,% 表示允許任意 host 訪問;如果需要設定只允許特定 ip 訪問,則應改為對應的 ip。
修改 root 使用者的 host 欄位
update user set host='%' where user='root';
flush privileges;
讓 root 使用者可以遠端連線,存在安全風險;可以採用如下方案:建立獨立的使用者。
GRANT ALL PRIVILEGES ON *.* TO 'deployop'@'%' IDENTIFIED BY 'deployop' WITH GRANT OPTION;
第一個 deployop 表示使用者名稱,% 表示所有的電腦都可以連線;也可以設定某個 ip 地址執行連線。第二個 deployop 表示密碼。
再次select user, host from user;
可檢視到 deployop 使用者的 host 是 % ;代表任何 ip 都可以連線。然後使用flush privileges;
語句立即生效。
如果還是連線不上,就有可能是防火牆的問題。新增 3306 埠防火牆策略,重啟防火牆。