一、离线安装
1.1. 上传到服务器指定目录
从官网下载对应tar包,以centos为例:mysql-8.0.35-linux-glibc2.12-x86_64.tar.xz
1.2. 解压安装包
先解压为tar包,在解压成文件夹
xz -d mysql-8.0.35-linux-glibc2.12-x86_64.tar.xz
tar xvf mysql-8.0.35-linux-glibc2.12-x86_64.tar
1.3. 创建工作目录
mv mysql-8.0.35-linux-glibc2.12-x86_64 mysql
cd /usr/local/mysql/
mkdir data
1.4. 创建用户并赋予权限
groupadd mysql
useradd -g mysql mysql
chown -R mysql.mysql /usr/local/mysql/
chown -R mysql:mysql /usr/local/mysql/data
1.5. 初始化数据库
命令中添加了忽略大小写的配置lower-case-table-names,1.6中的配置文件同样需要添加该配置,否则启动会报错。
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lower-case-table-names=1
初始化成功后会打印一个初始密码,root@localhost:后面的字符需要记录,后面登录要用
[root@localhost local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lower-case-table-names=1
2024-04-25T02:55:45.571780Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.35) initializing of server in progress as process 64838
2024-04-25T02:55:45.585064Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-04-25T02:55:46.600925Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-04-25T02:55:48.391974Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: f&s!tojVX4la
1.6. 配置
vim /etc/my.cng
文件内容为:
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
user=mysql
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
lower_case_table_names=1
1.7. 启动服务
cp -a ./support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
service mysql start
1.8. 修改密码及远程访问
在/usr/local/bin目录下添加软连接,方便使用mysql命令
cd /usr/local/bin
ln -fs /usr/local/mysql/bin/mysql mysql
使用命令连接并修改密码,然后修改root登录的权限,开启远程访问
mysql -u root -p
alter user 'root'@'loaclhost' identified by 'tzkj123456';
use mysql;
select host from user where user='root';
update user set host = '%' where user ='root';
flush privileges;
1.9. 可能遇到的问题
登录mysql时依赖找不到
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
查找相同名称开头的文件,创建软连接让引用指向存在的依赖
[root@localhost /]# find / -name 'libtinfo.*'
/home/docker/run/overlay2/36d14f77c819e713064a3c0bb231cef9c86322598b1b7c9a083051fd317feb8c/diff/lib64/libtinfo.so.5
/home/docker/run/overlay2/36d14f77c819e713064a3c0bb231cef9c86322598b1b7c9a083051fd317feb8c/diff/lib64/libtinfo.so.5.7
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/lib64/libtinfo.so.6
/usr/lib64/libtinfo.so.6.1
[root@localhost /]# ln -s /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5