Mysql 安装

1.1 创建目录

[root@db01 ~]# mkdir -p /data/app/ /data/3306/data /data/3306/binlog

1.2 创建MySQL相关用户、组

[root@db01 ~]# useradd mysql
[root@db01 ~]# chown -R mysql.mysql /data

1.3 系统残留清空

[root@db01 ~]# yum remove mariadb-libs -y

社区版下载地址

https://downloads.mysql.com/archives/community/

2 软件上传和解压

cd /data/app
[root@db01 app]# tar xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
[root@db01 app]# ln -s mysql-5.7.28-linux-glibc2.12-x86_64 mysql
[root@db01 app]# chown -R mysql. /data

3 环境变量设置

vim /etc/profile
#添加一行:
export PATH=/data/app/mysql/bin:$PATH

#生效配置

source /etc/profile

检查设置

[root@db01 bin]# mysql -V

4 准备基础配置文件

cat >/etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/data/app/mysql
datadir=/data/3306/data
server_id=6
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock
EOF

5 初始化数据

5.7 + 版本 :

1
[root@db01 bin]# mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql --datadir=/data/3306/data

#########报错###############
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@db01 bin]#
########################

报错处理:

[root@db01 bin]# yum install -y libaio-devel

再次运行初始化命令

1
[root@db01 bin]# mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql --datadir=/data/3306/data

2020-03-13T03:33:34.530498Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2020-03-13T03:33:34.923532Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-03-13T03:33:35.000129Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-03-13T03:33:35.061644Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6b64f882-64db-11ea-88a1-000c29248f69.
2020-03-13T03:33:35.065248Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2020-03-13T03:33:36.340554Z 0 [Warning] CA certificate ca.pem is self signed.
2020-03-13T03:33:36.603604Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the –initialize-insecure option.

+++++++++++++++++报错++++++++++++++++++++++

1
[root@db01 bin]# mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql --datadir=/data/3306/data

2020-03-13T03:35:50.039977Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2020-03-13T03:35:50.043386Z 0 [ERROR] –initialize specified but the data directory has files in it. Aborting.
2020-03-13T03:35:50.043441Z 0 [ERROR] Aborting

[root@db01 bin]#

处理方法: 清空datadir目录下所有数据,重新初始化。
!!!!!!生产需要详细确认。!!!!!!!!!!
+++++++++++++++++++++++++++++++++++++++++

6 准备启动脚本

[root@db01 ~]# cp /data/app/mysql/support-files/mysql.server /etc/init.d/mysqld

方法一: 自带脚本(适合于单机单MySQL)

启动方式 1: SYS-V (C6服务管理方式)
[root@db01 ~]# service mysqld start /stop /restart
[root@db01 ~]# service mysqld start
Starting MySQL.Logging to ‘/data/3306/data/db01.err’.
SUCCESS!
[root@db01 ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@db01 ~]# service mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS!
[root@db01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!

启动方式2: C7 管理方式 systemd
1
2
3
4
5
6
7
[root@db01 ~]# chkconfig --add mysqld
[root@db01 ~]# systemctl stop mysqld
[root@db01 ~]# systemctl start mysqld
[root@db01 ~]# systemctl restart mysqld

# 自启动
systemctl enable mysqld
1
[root@db01 ~]# mysqladmin -uroot -p password qA6C9RHn

#直接回车

[root@db01 ~]# mysql -u root -p
qA6C9RHn

mysql> show master status;
+——————-+———-+————–+——————+——————-+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+——————-+———-+————–+——————+——————-+
| master-bin.000001 | 2556 | | | |
+——————-+———-+————–+——————+——————-+

如搭建主从:

CHANGE MASTER TO
MASTER_HOST=’192.168.100.171’,
MASTER_USER=’repl’,
MASTER_PASSWORD=’passwd123’,
MASTER_PORT=3306,
MASTER_LOG_FILE=’master-bin.000001’,
MASTER_LOG_POS=2556,
MASTER_CONNECT_RETRY=10;

开启远程登陆

方法一:
选择 mysql 数据库

1
use mysql;

在 mysql 数据库的 user 表中查看当前 root 用户的相关信息

1
2
select host, user, authentication_string, plugin from user; 
show variables like '%skip_networking%';

授权 root 用户的所有权限并设置远程访问

1
2
3
CREATE USER 'root'@'%' IDENTIFIED BY 'root';

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

设置为远程登录

1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

刷新权限

1
flush privileges;

如若有root存在localhost权限则需删掉

1
drop user root@localhost; 

方法二:

1
2
3
4
5
mysql8.0 不能使用方法一,需使用方法二

use mysql;

update user set host='%' where user='root';