欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux上安装mysql8.0.19

程序员文章站 2022-03-08 14:04:03
...

1.下载mysql

选择下载Linux - Generic (glibc 2.12) (x86, 64-bit), TAR

Linux上安装mysql8.0.19

2.上传到服务器后进行解压并安装

2.1 解压压缩包

$ tar -xvf mysql-8.0.19-linux-glibc2.12-x86_64.tar 
mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
mysql-test-8.0.19-linux-glibc2.12-x86_64.tar.xz
mysql-router-8.0.19-linux-glibc2.12-x86_64.tar.xz

2.2 解压后得到xz后缀压缩包,解压mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz

$ tar -xvf mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz 
mysql-8.0.19-linux-glibc2.12-x86_64/bin/
mysql-8.0.19-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-8.0.19-linux-glibc2.12-x86_64/bin/myisamchk
mysql-8.0.19-linux-glibc2.12-x86_64/bin/myisamlog
mysql-8.0.19-linux-glibc2.12-x86_64/bin/myisampack
mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysql
mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysql_config_editor
mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysql_secure_installation
mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysql_ssl_rsa_setup
mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysql_tzinfo_to_sql
mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysql_upgrade
mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysqladmin
mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysqlbinlog
......

#解压后,重命名为mysql-8.0.19,并移动到/usr/local目录下
$ mv mysql-8.0.19-linux-glibc2.12-x86_64/ /usr/local/mysql

2.3 添加用户名和用户组

$ cd /usr/local/mysql
$ groupadd mysql
$ useradd -r -g mysql mysql
$ chown -R mysql:mysql ./

在/usr/local/mysql下创建data文件夹

$ mkdir data

2.4 初始化数据库,会自动生成密码 记得记下来,后面登录修改密码要用到

$ cd /usr/local/mysql/bin
$ ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2020-03-26T08:06:37.751865Z 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.
2020-03-26T08:06:37.752022Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.19) initializing of server in progress as process 13594
2020-03-26T08:06:42.182558Z 5 [Note] [MY-010454] [Server] A temporary password is generated for aaa@qq.com: tD&LZNEsm3Et

在初始化的过程中,可能会出现*./mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory*这个错误,原因是缺少了libaio,这里执行安装yum -y install numactl后,重新初始化就可以了

2.5 创建配置文件及相关目录(如果在这个路径下已经存在的话就不用创建了)

创建配置文件:vim /etc/my.cnf

文件模板:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[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 = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# 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

修改配置文件内容:注意要在模板的 [mysqld] 下面去修改(basedir:mysql安装路径,datadir:数据存储目录)

[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 = /var/log/mysqld.log
character-set-server = utf8
pid-file = /tmp/mysqld/mysqld.pid
tmpdir = /tmp
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

保存退出:wq!

创建文件/tmp/mysql.sock:设置用户组及用户,授权

cd /tmp
touch mysql.sock
chown mysql:mysql mysql.sock
chmod 755 mysql.sock

创建文件/tmp/mysqld/mysqld.pid

mkdir mysqld
cd mysqld
touch mysqld.pid
cd ..
chown -R mysql:mysql mysqld
cd mysqld
chmod 755 mysqld.pid

创建文件/var/log/mysqld.log:

touch /var/log/mysqld.log
chown -R mysql:mysql /var/log
cd /var/log
chmod 755 mysqld.log

2.6 安装和初始化数据库

进入初始化目录:cd /usr/local/mysql/bin/

初始化数据库:./mysqld --initialize --user=mysql --basedir=/usr/local/mysql–datadir=/usr/local/mysql/data

如果报错:(./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory)

需要安装命令:yum -y install numactl

之后在执行初始化数据库:./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

2.7 启动数据库

./mysqld_safe --user=mysql &

查看是否成功:ps -ef | grep mysql

默认密码在mysqld.log日志里, cat /var/log/mysqld.log,其中aaa@qq.com: 后面的就是默认密码

$ /usr/local/mysql/bin/mysql -u root -p  #回车后输入密码

# 创建新用户
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'Hadoop3!';

# 修改用户密码
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '111';
# 给user用户添加权限
mysql> grant all privileges on *.* to 'root'@'%';

# 刷新登录权限
mysql> flush privileges;

2.8 配置mysql允许远程连接的方法

默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要修改/etc/my.cnf文件。还要改登录用户添加权限
找到bind-address = 127.0.0.1这一行
改为bind-address = 0.0.0.0即可