在centos上二進制安裝mysql

1 下載安裝文件

查看下系統環境

# uname -a
Linux xxx 4.14.0_1-0-0-45 #2 SMP Tue Oct 19 18:27:28 CST 2021 x86_64 x86_64 x86_64 GNU/Linux

# cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core)

# ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

官網 下載 mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz (因為上面查到的系統glibc是2.17,所以選擇了相對應的版本)

image.jpeg

2 準備工作

檢查下是否安裝過的殘留文件

$ sudo su -
# rpm -qa | grep mysql

如果存在則刪除

# rpm -e --nodeps mysql-xxxxxxxxx

檢查是否存在殘留的mysql相關配置文件,如果有非預期的,可以刪除

# whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql

# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/share/mysql
/usr/lib64/mysql

檢查是否存在計劃運行mysql的用戶和用戶組,不存在則創建

# cat /etc/group | grep mysql
# cat /etc/passwd |grep mysql

# groupadd mysql
# useradd -r -g mysql mysql

# cat /etc/group | grep mysql
mysql:x:1002:
# cat /etc/passwd |grep mysql
mysql:x:996:1002::/home/mysql:/bin/bash

因為規劃將mysql的數據存放在/home/disk1,在該目錄下創建mysql目錄,并將目錄權限設為777

# cd /home/disk1
# mkdir mysql
# chmod 777 mysql

3 安裝并啟動

將上面下載的安裝文件解壓,移動到/usr/local/mysql目錄下

# tar -xvf  mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz
# ls
mysql-8.0.28-linux-glibc2.17-x86_64-minimal  mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz
# mv mysql-8.0.28-linux-glibc2.17-x86_64-minimal /usr/local/mysql

編輯配置文件,內容如下:

# vim /etc/my.cnf
[mysqld]
# datadir=/var/lib/mysql
# socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
# symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/home/disk1/mysql
socket=/tmp/mysql.sock

log-error=/home/disk1/mysql/mysql.err
pid-file=/home/disk1/mysql/mysql.pid
###character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

[mysqld_safe]
log-error=/home/disk1/mysql/mariadb.log
pid-file=/home/disk1/mysql/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

初始化mysql

# cd /usr/local/mysql/
# ./bin/mysqld --initialize --user=mysql --datadir=/home/disk1/mysql --basedir=/usr/local/mysql

初始化需要一些時間,耐心等待。初始化完后,在mysql數據目錄下會看到生成了一系列的文件。其中的mysql.err文件中記錄了root用戶的初始密碼。記住這個密碼,初次登錄mysql可以使用這個密碼。

# cd /home/disk1/mysql/
# ls
auto.cnf  ca-key.pem  ca.pem  client-cert.pem  client-key.pem  #ib_16384_0.dblwr  #ib_16384_1.dblwr  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  #innodb_temp  mysql  mysql.err  mysql.ibd  performance_schema  private_key.pem  public_key.pem  server-cert.pem  server-key.pem  sys  undo_001  undo_002

# cat mysql.err
2022-09-24T09:14:21.867222Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2022-09-24T09:14:21.867339Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 31621
2022-09-24T09:14:21.927464Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-09-24T09:14:28.444781Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-09-24T09:14:48.505031Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 0HVtqbUo1z#w

mysql.server文件拷貝到/etc/init.d/mysql目錄下

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

這樣就可以以服務的方式啟動mysql了

# service mysql start

# ps -ef | grep mysql
root     44425     1  0 17:21 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/home/disk1/mysql --pid-file=/home/disk1/mysql/mysql.pid
mysql    44688 44425  6 17:21 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/disk1/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/home/disk1/mysql/mariadb.log --pid-file=/home/disk1/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306
root     44893 21139  0 17:21 pts/0    00:00:00 grep --color=auto mysql

# lsof -i :3306
COMMAND   PID  USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
mysqld  44688 mysql   24u  IPv4 2942913256      0t0  TCP localhost:mysql (LISTEN)

登錄mysql,修改密碼

# ./bin/mysql -uroot -h 127.0.0.1 -p0HVtqbUo1z#w

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'mysql123';
Query OK, 0 rows affected (0.12 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)

mysql> use mysql;
mysql> update user set user.Host='%' where user.User='root';
mysql> flush privileges;

上面在修改root用戶密碼時,使用了WITH mysql_native_password參數,如果不加這個參數,遠程登錄時可能會報下面的錯誤:

$ mysql -hx.x.x.x -uroot -pmysql123 -P3306
Warning: Using a password on the command line interface can be insecure.
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: .../lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory

現在本地和遠程都可以登錄并使用mysql了

$ mysql -hx.x.x.x -uroot -pmysql123 -P3306

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容