Linux安装mysql8.0.27
1. 准备
(1).mysql8.0.27 安装包
下载链接: mysql最新版本下载.
(2).检查服务器是否安装过linux
# 检测是否安装过mysql
rpm -qa | grep mysql
# 删除命令
rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64
# 检查mysql文件夹
whereis mysql
rm -rf /usr/lib64/mysql /usr/share/mysql
find / -name mysql
rm -rf /etc/selinux/targeted/active/modules/100/mysql
执行结果如图:
(3).创建用户(mysql)
若数据库被攻破,可能会影响整个服务器的安全。划分mysql用户可以割离数据库
useradd -s /sbin/nologin -M mysql
2安装过程
1.解压缩包
# 解压
tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
# 移动位置并重命名
mv /mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql
2. 初始化数据库
cd /usr/local/mysql/
# 创建data文件夹
mkdir data
# 给文件夹授权
chown -R root:root /usr/local/mysql
# 给文件夹授权
chown -R mysql:mysql /usr/local/mysql/data/
# 初始化数据库,mysql的bin目录下的mysqld命令
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lower-case-table-names=1 --initialize
红框内是root用户的临时密码,此处可能报错。1
3. 创建配置文件my.cnf
mysql配置文件默认路径有 : /etc/my.cnf,/etc/mysql/my.cnf,等等
#创建并打开配置文件
vim /etc/my.cnf
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
port = 3306
#lower_case_table_names = 1
# server_id = .....
# socket = .....
lower_case_table_names = 1
max_allowed_packet=32M
default-authentication-plugin = mysql_native_password
#lower_case_file_system = on
#lower_case_table_names = 1
log_bin_trust_function_creators = ON
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
4. 设置mysql开机自启
- 进入mysql的安装目录下找到,support-files文件夹,将其复制进/etc/init.d/目录下
cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysql
#赋权
chmod +x /etc/init.d/mysql
5. 注册服务
#注册服务
chkconfig --add mysql
#检测服务
chkconfig --list mysql
vim /etc/ld.so.conf
最尾行添加: /usr/local/mysql/lib
6.配置环境变量
# 配置环境变量
vim /etc/profile
最尾行添加如下内容:
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
# 系统重新加载文件,记得一定要
source /etc/profile
7. 修改mysql密码
#启动mysql服务
[root@VM_0_3_centos support-files]# service mysql start
Starting MySQL.. SUCCESS!
#登录root用户,首次登录需要使用上文生成的临时密码
[root@VM_0_3_centos support-files]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 82
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密码
mysql> alter user 'root'@'localhost' identified by '修改后的密码';
Query OK, 0 rows affected (0.01 sec)
8.放开异地登录
# 登录
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> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root | $A$005$=!..iRO=k:x_>qiFtXTYJmwgur/2pJH1mfk26FBL.1YZOC.YcBw8auuFFM0 | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)
# 更新root信息
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# 授权root用户可以远程登陆,失败
mysql> GRANT ALL ON *.* TO 'root'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
# 立即生效,重新授权
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
# 授权root用户可以远程登陆
mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.02 sec)
# 立即生效
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
#
mysql> alter user 'root'@'%' identified with mysql_native_password by 'kid+1412';
Query OK, 0 rows affected (0.01 sec)
# 立即生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 退出
mysql> exit
Bye
错误:error while loading shared libraries: libtinfo.so.5
解决方案:
↩︎#执行命令 创建一个软连接 ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5