前言
我安装了很多次mysql,包括docker安装、kubernetes安装、或者使用ansible等自动化构建安装,但是直接用安装包安装还是比较少。
今天突然想从官网下载linux安装包来进行安装,故写此文进行分享
案例我也是从网上查的,感谢之前的网友的贡献,具体我也不清楚是谁。但是我写的,一定比网上的更加详细,为各位新手做指导。
步骤
1、下载安装包并解压
2、创建mysql账户
3、创建mysql配置文件my.cnf
4、执行mysql初始化并记录初始密码
5、启动项目并修改密码
6、开启远程连接
7、其他命令
1、下载安装包并解压
https://downloads.mysql.com/archives/community/
选择 Linux - Generic (glibc 2.12) (x86, 64-bit), Compressed TAR Archive
上传压缩包到/opt/softwarePackage
cd /opt/softwarePackage
#解压
tar -xvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
#移动到local路劲,并且重命名
mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
2、创建mysql账户
cd /usr/local
cd mysql/
groupadd mysql
useradd -r -g mysql mysql
# 创建数据目录并赋予权限,该目录用于防止mysql的data
mkdir -p /data/mysql
chown mysql:mysql -R /data/mysql/
3、创建mysql配置文件my.cnf
注意centos7的 /etc/my.cnf 是有数据的,用于内置数据库mariadb,此处我们直接覆盖即可
vi /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
4、执行mysql初始化并记录初始密码
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
记录初始密码,例如下面的密码为:%ft;0AtdegVi
cat /data/mysql/mysql.err
2024-07-28T07:30:20.611949Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-07-28T07:30:21.383427Z 0 [Warning] CA certificate ca.pem is self signed.
2024-07-28T07:30:21.691090Z 1 [Note] A temporary password is generated for root@localhost: %ft;0AtdegVi
5、启动项目并修改密码
设置开机自启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
启动mysql
cd /usr/local/mysql/bin
service mysql start
修改密码
cd /usr/local/mysql/bin
[root@localhost bin]# ./mysql -u root -p
Enter password: #输入刚刚记录的Miami
mysql> SET PASSWORD=PASSWORD('admin');
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
mysql> FLUSH PRIVILEGES;
6、开启远程连接
登录mysql进行配置
[root@localhost bin]# ./mysql -u root -p
Enter password:
mysql> use mysql
mysql> update user set host = '%' where user = 'root';
mysql> FLUSH PRIVILEGES;
mysql> quit
如果此处远程电脑还是连接不上mysql,大概率是防火墙没有放行
7、其他命令
以上mysql就已经安装好了,再分享几组命令
查看mysql运行状态
[root@localhost bin]# ps -ef|grep mysql
root 5820 1275 0 16:10 pts/0 00:00:00 grep --color=auto mysql
root 96497 1 0 15:36 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
mysql 96688 96497 0 15:36 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql.err --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306
登录mysql查看mysql版本
#登录mysql查看mysql版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.28 |
+-----------+
1 row in set (0.00 sec)
防火墙中怎么暴露mysql端口
再Linux中输入如下命令即可
/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT