liunx-centos-mysql8.0(自用收藏)

原文章地址:

CentOS安装mysql8.0,并远程连接_centos安装mysql8.0远程-CSDN博客

一、环境
腾讯云轻量级服务器,操作系统:CentOS 8.2 64bit

二、安装mysql
1.进入/usr/local目录,创建mysql文件夹
[root@VM-4-2-centos ~]# cd /usr/local
[root@VM-4-2-centos local]# ls
bin  etc  games  include  lib  lib64  libexec  qcloud  sbin  share  src
[root@VM-4-2-centos local]# mkdir mysql

2.进入创建的mysql目录,下载mysql压缩包
[root@VM-4-2-centos local]# cd mysql
[root@VM-4-2-centos mysql]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz


3.解压
[root@VM-4-2-centos mysql]# tar xvJf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

4.将解压后的文件夹重命名为mysql-8.0
[root@VM-4-2-centos mysql]# ls
mysql-8.0.20-linux-glibc2.12-x86_64  mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
[root@VM-4-2-centos mysql]# mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0
[root@VM-4-2-centos mysql]# ls
mysql-8.0  mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

5.进入mysql-8.0文件夹,创建data文件夹
[root@VM-4-2-centos mysql]# cd mysql-8.0
[root@VM-4-2-centos mysql-8.0]# mkdir data

6.创建用户mysql,并修改文件权限
[root@VM-4-2-centos mysql-8.0]# groupadd mysql
[root@VM-4-2-centos mysql-8.0]# useradd -g mysql mysql
[root@VM-4-2-centos mysql-8.0]# chown -R mysql.mysql /usr/local/mysql/mysql-8.0
[root@VM-4-2-centos mysql-8.0]# chmod 750 /usr/local/mysql/mysql-8.0/data -R

7.配置mysql环境变量
7.1 编辑
[root@VM-4-2-centos mysql-8.0]# vim /etc/profile
新增添加export PATH=$PATH:/usr/local/mysql/mysql-8.0/bin:/usr/local/mysql/mysql-8.0/lib

7.2 保存
编辑完后,按esc键,输入:wq保存并退出
注意:一定要执行source /etc/profile,使其配置生效

[root@VM-4-2-centos ~]# source /etc/profile

8.在根目录的etc目录下,创建my.cnf文件,并编辑
[root@VM-4-2-centos mysql-8.0]# vi /etc/my.cnf

8.1将下面的内容复制到my.cnf文件中,删除原先内容

[mysql]
default-character-set=utf8mb4
[client]
#port=3306
socket=/var/lib/mysql/mysql.sock

[mysqld]
#port=3306
#server-id=3306
user=mysql
general_log = 1
general_log_file= /var/log/mysql/mysql.log
socket=/var/lib/mysql/mysql.sock
basedir=/usr/local/mysql/mysql-8.0
datadir=/usr/local/mysql/mysql-8.0/data
log-bin=/usr/local/mysql/mysql-8.0/data/mysql-bin
innodb_data_home_dir=/usr/local/mysql/mysql-8.0/data
innodb_log_group_home_dir=/usr/local/mysql/mysql-8.0/data/
character-set-server=utf8mb4
lower_case_table_names=1
autocommit=1
default_authentication_plugin=mysql_native_password
symbolic-links=0
# Disabling symbolic-links is recommended to prevent assorted security risks
# 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

[mysqld_safe]
log-error=/usr/local/mysql/mysql-8.0/data/mysql.log
pid-file=/usr/local/mysql/mysql-8.0/data/mysql.pid

#
# include all files from the config directory

编辑完后,按esc键,输入:wq保存并退出,输入:q!是强制退出不保存文件

9.切换到/usr/local/mysql/mysql-8.0/bin目录下
[root@VM-4-2-centos mysql-8.0]# cd bin

初始化基础信息,得到数据库的初始密码
[root@VM-4-2-centos bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql/mysql-8.0 --datadir=/usr/local/mysql//mysql-8.0/data/ --initialize
2022-10-10T02:49:34.128408Z 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-10-10T02:49:34.128535Z 0 [System] [MY-013169] [Server] /usr/local/mysql/mysql-8.0/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 2941366
2022-10-10T02:49:34.147819Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-10T02:49:35.735065Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysqld: File '/var/log/mysql/mysql.log' not found (OS errno 2 - No such file or directory)
2022-10-10T02:49:37.157076Z 0 [ERROR] [MY-011263] [Server] Could not use /var/log/mysql/mysql.log for logging (error 2 - No such file or directory). Turning logging off for the server process. To turn it on again: fix the cause, then either restart the query logging by using "SET GLOBAL GENERAL_LOG=ON" or restart the MySQL server.
2022-10-10T02:49:37.851606Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: *nD(+Ac#y2Ae

最后一段*nD(+Ac#y2Ae是密码,保存下来

11. 返回mysql-8.0目录,复制mysql.server文件,并赋予权限

[root@VM-4-2-centos bin]# cd ..
[root@VM-4-2-centos mysql-8.0]# cp -a ./support-files/mysql.server /etc/init.d/mysql
[root@VM-4-2-centos mysql-8.0]# cp -a ./support-files/mysql.server /etc/init.d/mysqld
[root@VM-4-2-centos mysql-8.0]# chown 777 /etc/my.cnf
[root@VM-4-2-centos mysql-8.0]# chmod +x /etc/init.d/mysql
[root@VM-4-2-centos mysql-8.0]# chmod +x /etc/init.d/mysqld

12检查一下/var/lib/mysql是否存在,否则进行创建
[root@VM-4-2-centos mysql-8.0]# mkdir /var/lib/mysql
[root@VM-4-2-centos mysql-8.0]# chown -R mysql:mysql /var/lib/mysql/

13启动mysql服务,SUCCESS表示成功
[root@VM-4-2-centos mysql-8.0]# service mysql start
Starting MySQL.Logging to '/usr/local/mysql/mysql-8.0/data/mysql.log'.
 SUCCESS! 

三、修改mysql密码,并设置远程连接
1.这一步我遇到了问题,出现了错误:mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory

[root@VM-4-2-centos mysql-8.0]# mysql -uroot -p
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory

2.解决方法:进入mysql-8.0下的lib目录,执行sudo ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5

[root@VM-4-2-centos mysql-8.0]# cd lib
[root@VM-4-2-centos lib]# sudo ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5

3.问题解决!接下来正常进行

输入mysql -uroot -p,然后输入刚刚上面的初始化密码,进入mysql。注意:这里输入密码是不会显示的,以为没有输入,实际输入了,也可以直接复制粘贴。
[root@VM-4-2-centos lib]# mysql -uroot -p
Enter password:

4.修改密码
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '自己设置新密码;

1.执行flush privileges; 使密码生效
mysql> flush privileges;

2.选择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> create user 'root'@'%' identified by  '设置的密码';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.02 sec)

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

四、完成
至此,远程服务器上mysql安装完毕!本地连接测试成功!

  • 25
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值