CentOS安装Mysql

每次需要在 Linux 上安装 Mysql 时都需要在网上找教程,今天整理一篇文章记录一下 Mysql 安装的过程。

下载 Mysql 安装包

可以到Mysql官方下载,我这里选择 mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz 8.0的版本。

或者命令行下载

1
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz
添加用户用户组
1
2
groupadd mysql
useradd -r -g mysql mysql
解压
1
tar xvf mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz
设置环境变量
1
2
3
4
5
6
# 移动到/usr/local目录下
mv mysql-8.0.24-linux-glibc2.12 /usr/local/mysql
cd /usr/local/mysql
# 配置环境变量
vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin

如果需要:关闭SELinux,编辑/etc/selinux/config,将selinux=enforce改为disable即可

修改 data 目录为 mysql 用户
1
2
3
cd /usr/local/mysql
mkdir data
chown -R mysql.mysql data
修改 my.cnf 文件

默认读取配置文件的顺序: 1. /etc/my.cnf2. /etc/mysql/my.cnf3. /usr/local/mysql/etc/my.cnf 4. ~/.my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vim /etc/my.cnf
# 添加下面内容
[Client]
port = 3306
character-set-server=utf8

[mysqld]
lower_case_table_names=1
port = 3306
basedir="/usr/local/mysql"
datadir="/usr/local/mysql/data"
max_connections=200
max_connect_errors=20
character-set-server=utf8
default-storage-engine=INNODB
sql_mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
# skip-grant-tables
default_authentication_plugin=mysql_native_password
log-error=/usr/local/mysql/data/error.err
pid-file=/usr/local/mysql/data/mysql.pid
初始化数据库
1
./bin/mysqld --initialize --user=mysql --lower-case-table-names=1

需要注意的是mysql8版本之后,如果要忽略大小写, lower-case-table-names 这个参数必须在初始数据库时指定。

启动 mysql
1
2
3
support-files/mysql.server start
# 登录mysql
mysql -u root -p
查看随机密码
1
cat /usr/local/mysql/data/error.err

Server] A temporary password is generated for root@localhost: rn%gAk;yq5iy

如果报错 mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory

执行:ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5

修改 root 密码
1
2
3
4
5
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxx';
# 为远程登录创建用户,并赋权
CREATE USER 'root'@'%' IDENTIFIED BY 'xxxxx';
GRANT ALL ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;
开机自启
1
2
3
4
5
# 配置mysql开机自启并建立MySQL服务 
cp ./support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld # 增加执行权限
chkconfig --add mysqld # 注册启动服务
chkconfig --list mysqld # 检查服务是否生效

整个过程其实很简单,时间久了就会忘记怎么安装,记录于此以后查阅。

原文作者: dgb8901,yinxing

原文链接: https://www.itwork.club/2021/08/31/install-mysql/

版权声明: 转载请注明出处

为您推荐

体验小程序「简易记账」

关注公众号「特想学英语」

导出Docker镜像或者容器