Mysql(1)Linux安装MySQL8
介绍
在我们日常工作中有可能遇到因为某些原因服务器与外网隔离,无法访问外部网络。这时候我们只能采用rpm包安装、源码安装或者二进制包来安装程序,而源码安装又需要提前处理好各种编译依赖(gcc、g++等),比较麻烦。这里介绍一下如何使用二进制包安装Mysql8
部署环境
系统:CentOS7
Mysql版本:8.0.30 直达地址:https://downloads.mysql.com/archives/community/
1 新建mysql用户
$ useradd mysql
2 下载解压mysql-8.0.30包
可以直接使用wget命令下载,或者去mysql官网下载再上传到服务器
$ cd /home
$ wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.30-el7-x86_64.tar.gz
$ tar -zxvf mysql-8.0.30-el7-x86_64.tar.gz
$ mkdir -p /usr/local/mysql
$ mv mysql-8.0.30-el7-x86_64 /usr/local/mysql/mysql-8.0.30
3 修改my.cnf文件
$ mv /etc/my.cnf /etc/my.cnf.bak
$ vim /etc/my.cnf
[client]
port=3306
socket = /usr/local/mysql/mysql-8.0.30/mysql.sock
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
basedir = /usr/local/mysql/mysql-8.0.30/
datadir = /usr/local/mysql/mysql-8.0.30/data
port = 3306
mysqlx_port = 33060
socket = /usr/local/mysql/mysql-8.0.30/mysql.sock
mysqlx_socket = /usr/local/mysql/mysql-8.0.30/mysql.sock
pid-file = /usr/local/mysql/mysql-8.0.30/mysqld.pid
innodb_buffer_pool_size = 1024M
sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
max_connections = 400
max_user_connections = 200
interactive_timeout = 900
wait_timeout = 900
connect_timeout = 86400
max_connect_errors = 10
character-set-server = utf8mb4
default-storage-engine = innodb
lower_case_table_names = 1
default-authentication-plugin = mysql_native_password
[mysqld_safe]
log-error=/usr/local/mysql/mysql-8.0.30/error.log
EOF
4 初始化mysql
$ /usr/local/mysql/mysql-8.0.30/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql/mysql-8.0.30 --datadir=/usr/local/mysql/mysql-8.0.30/data --initialize-insecure
注意:初始化会生成文件到/usr/local/mysql/mysql-8.0.30/data目录下,如果初始化到一半失败,修改之后重新初始化要先清除data文件夹下面的所有文件。
5 创建mysqld.pid和error.log文件
$ touch /usr/local/mysql/mysql-8.0.30/mysqld.pid
$ touch /usr/local/mysql/mysql-8.0.30/error.log
6 更改文件夹所属用户
$ chown -R mysql.mysql /usr/local/mysql/mysql-8.0.30
7 创建软连接
$ ln -s /usr/local/mysql/mysql-8.0.30/bin/mysql /usr/bin/mysql
8 创建mysql服务
$ vim/usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql
After=network-online.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/mysql-8.0.30/support-files/mysql.server start
ExecStop=/usr/local/mysql/mysql-8.0.30/support-files/mysql.server stop
ExecReload=/usr/local/mysql/mysql-8.0.30/support-files/mysql.server restart
User=mysql
Group=mysql
[Install]
WantedBy=multi-user.target
EOF
9 启动mysql并设置开机自启
$ systemctl start mysqld
$ systemctl enable mysqld
10 防火墙放行端口
$ firewall-cmd --permanent --add-port=3306/tcp
$ firewall-cmd --permanent --add-port=33060/tcp
$ firewall-cmd --reload
$ firewall-cmd --list-ports
11 创建远程连接用户
进入mysql(默认密码空),更改root密码,创建并授权新用户,设置远程访问权限
$ mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'admin@123456';
mysql> CREATE USER 'hzbb'@'%' IDENTIFIED BY 'admin@123456';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'hzbb'@'%';
mysql> FLUSH PRIVILEGES;
12 检查用户的密码认证方式
在mysql8版本之后新建用户默认为caching_sha2_password,我们在my.cnf文件里已经该成了mysql_native_password的认证方式
mysql> use mysql
mysql> select host, user, plugin from user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | hzbb | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | mysql_native_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
运维小记!
喜欢就支持一下吧
打赏
微信
支付宝