基础环境centos7.5,mysql版本8.0.20,通过tar包安装,安装路径/usr/local。
mysql官网:https://dev.mysql.com/downloads/mysql/
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
卸载centos7中自带的mariadb
[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
上传并解压安装包mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz。注意压缩包是.tar.xz格式的,需要先解压、再解包。
[root@CentOS7-1 tools]# xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
[root@CentOS7-1 tools]# tar xf mysql-8.0.20-linux-glibc2.12-x86_64.tar -C /usr/local/
在安装包下创建数据文件路径data
[root@CentOS7-1 tools]# cd /usr/local/
[root@CentOS7-1 local]# mv mysql-8.0.20-linux-glibc2.12-x86_64/ mysql
[mysql@CentOS7-1 local]$ cd mysql
[mysql@CentOS7-1 mysql]$ mkdir data
创建mysql用户
[root@CentOS7-1 mysql]# useradd mysql
[root@CentOS7-1 mysql]# passwd mysql
重命名解压后的安装包目录,并修改目录的属主属组,
[root@localhost mysql]# cd ..
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x 10 mysql mysql 141 12月 8 14:12 mysql
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 12月 13 2020 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
编辑配置文件/etc/my.cnf
[root@localhost local]# cat /etc/my.cnf
[mysqld]
port=3306 #3306端口
basedir=/usr/local/mysql # mysql的安装目录
datadir=/usr/local/mysql/data # mysql数据库的数据的存放目录
max_connections=10000 # 允许最大连接数
max_connect_errors=10 # 允许连接失败的次数,防止有人从该主机试图攻击数据库系统
character-set-server=utf8 # 服务端使用的字符集
default-storage-engine=INNODB # 默认存储引擎
[mysql]
default-character-set=utf8 # 设置mysql客户端默认字符集
到bin目录下初始化数据库,记录初始密码
[mysql@CentOS7-1 local]$ cd mysql/bin
[mysql@CentOS7-1 bin]$ ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2021-12-07T16:29:25.936536Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
2021-12-07T16:29:25.936549Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
2021-12-07T16:29:25.937104Z 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.
2021-12-07T16:29:25.937260Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 111611
2021-12-07T16:29:25.954467Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-12-07T16:29:27.128120Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-12-07T16:29:28.520117Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: I<-VCdcL2kZb
将mysql添加到服务
[root@CentOS7-1 bin]# cd ../support-files/
[root@CentOS7-1 support-files]# cp -a mysql.server /etc/init.d/mysqld
[root@CentOS7-1 support-files]# chmod +x /etc/init.d/mysqld
[root@CentOS7-1 support-files]# chkconfig --add mysqld
配置环境变量/etc/profile
[root@localhost bin]# echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
[root@localhost bin]# source /etc/profile
启动mysql
[root@localhost bin]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
.... SUCCESS!
[root@localhost bin]# service mysqld status
SUCCESS! MySQL running (70760)
登录mysql
(如遇到下面问题请参考底部问题一方法解决)
[root@ecs-656621649-001 /usr/local/mysql/support-files]# mysql -uroot -p
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
[root@localhost bin]# mysql -uroot -p
Enter password: 输入初始化时生成的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
修改root用户密码即相关权限。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Cslc@pass123';
Query OK, 0 rows affected (0.00 sec)
mysql> update user set host='%' where user = 'root'; ---设置root用户可在任意主机登录,即可远程登录
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges; ---刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
根据报错内容可以看出,是因为在启动mysql的时候加载了libtinfo.so.5库文件。此时我们可以查看redhat9系统中是否存在改库文件。
[root@server bin]# find / -name 'libncurses*'
/usr/lib64/libncurses.so.6
/usr/lib64/libncursesw.so.6
/usr/lib64/libncurses.so.6.2
/usr/lib64/libncursesw.so.6.2
从上面的查询结果中可以看到,我现在使用的系统中并没有安装对应5版本的库文件,这个时候可以将高版本创建软连接到5版本。
[root@server bin]# ln -s /usr/lib64/libncurses.so.6 /usr/lib64/libncurses.so.5
再次启动时,发现还缺一个库文件。
./mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
我们继续查找库文件。
[root@server bin]# find / -name libtinfo.so*
/usr/lib64/libtinfo.so.6
/usr/lib64/libtinfo.so.6.2
发现也是缺少一个对应的5版本的文件,同样的道理,将对应的高版本创建一个低版本的软连接。
[root@server bin]# ln -s /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5