mysql学习笔记---mysql的安装--二进制安装

安装mysql

二进制安装

1,去官方网站下载通用的二进制文件,并将其传入linux

[root@localhost ~]# ls
anaconda-ks.cfg  mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

2,解压安装包

[root@localhost ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  mysql-5.7.30-linux-glibc2.12-x86_64  mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

3,将解压后的文件夹移动到/usr/local/下面,重命名为sc_mysql

[root@localhost ~]# mv mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/sc_mysql
[root@localhost ~]# cd /usr/local/sc_mysql/
[root@localhost sc_mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files

4,关闭selinux和防火墙

[root@localhost sc_mysql]# setenforce 0
[root@localhost sc_mysql]# vim /etc/selinux/config
[root@localhost sc_mysql]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
[root@localhost sc_mysql]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

5,创建一个/data/mysql文件夹,用来存放mysql的数据文件,需要赋权限

[root@localhost sc_mysql]# mkdir /data/mysql -p
[root@localhost sc_mysql]# chown mysql:mysql /data/mysql/
chown: 无效的用户: "mysql:mysql"
[root@localhost sc_mysql]# useradd -s /sbin/noloign mysql 
[root@localhost sc_mysql]# chown mysql:mysql /data/mysql/
[root@localhost sc_mysql]# chmod 750 /data/mysql

。。。。。。。。。。。。。。。。。。。。。。。
相关命令解释:

chown mysql:mysql /data/mysql/ 将文件的属主和属组都改为mysql

chmod 750 /data/mysql 赋予文件属主读写执行权限,文件属组读执行权限,其他人没有权限

。。。。。。。。。。。。。。。。。。。。。。。
执行mysqld的初始化操作

[root@localhost sc_mysql]# cd /usr/local/sc_mysql/bin
[root@localhost bin]# ./mysqld  --initialize --user=mysql --basedir=/usr/local/sc_mysql/  --datadir=/data/mysql 
2020-10-06T07:29:00.399233Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-06T07:29:00.805989Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-10-06T07:29:00.888983Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-10-06T07:29:00.965368Z 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: 9a99103b-07a5-11eb-ab37-000c29ab471a.
2020-10-06T07:29:00.966517Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-10-06T07:29:02.968254Z 0 [Warning] CA certificate ca.pem is self signed.
2020-10-06T07:29:03.235405Z 1 [Note] A temporary password is generated for root@localhost: q4etr.fPGn1u

修改PATH变量,并将mysql.server复制到init.d下面

[root@localhost bin]# PATH=$PATH:/usr/local/sc_mysql/bin
[root@localhost bin]# cp ../support-files/mysql.server  /etc/init.d/mysqld

先将my.cnf文件清空,再 将新的配置内容写入

[root@localhost bin]#  >/etc/my.cnf
[root@localhost bin]# cat /etc/my.cnf
[root@localhost bin]# vim /etc/my.cnf
[root@localhost bin]# service mysqld  start
/etc/init.d/mysqld: line 239: my_print_defaults: command not found
/etc/init.d/mysqld: line 259: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

#报错了,错误原因:/usr/local/mysql文件找不到,因为我们一开始把解压后的后面放到了/usr/local下,命名为了/sc_mysql

#解决方法1:更改/etc/init.d/mysqld里的相关内容,具体如何更改,下面脚本安装里会有说明

#解决方法2:

[root@localhost bin]# mv /usr/local/sc_mysql /usr/local/mysql
[root@localhost bin]# service mysqld  start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
 ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).

又报错了,因为将sc_mysql改为mysql后,我们的basedir也需要更改

[root@localhost bin]# ./mysqld  --initialize --user=mysql --basedir=/usr/local/mysql/  --datadir=/data/mysql
2020-10-06T07:39:24.321218Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-06T07:39:24.323445Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2020-10-06T07:39:24.323482Z 0 [ERROR] Aborting

又报错了,错误原因是我们之前执行过上条命令,所以/data/mysql里面已经有内容了
所以解决方法有两种:

1删除/data/mysql下的内容,再执行上述命令
2将上述命令的–datadir去掉即可

[root@localhost bin]# ./mysqld  --initialize --user=mysql --basedir=/usr/local/mysql/
2020-10-06T07:40:39.456747Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-06T07:40:39.882315Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-10-06T07:40:39.943979Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-10-06T07:40:40.000568Z 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: 3b416199-07a7-11eb-83b5-000c29ab471a.
2020-10-06T07:40:40.001627Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-10-06T07:40:40.358552Z 0 [Warning] CA certificate ca.pem is self signed.
2020-10-06T07:40:40.939087Z 1 [Note] A temporary password is generated for root@localhost: WwWgn7sR7%?T
[root@localhost bin]# service mysqld  start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
. SUCCESS! 

nice !!!启动成功

设置mysqld开机启动

[root@localhost bin]#chkconfig --add mysqld
#可以用chkconfig --list 查看mysqld是否开机启动
[root@localhost ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
#可以看到3和5是开启的
#linux的运行级别
#0:关机1:单用户2:多用户3:字符界面4:没使用5:图形界面6:重启
[root@localhost bin]# mysql -uroot -p'WwWgn7sR7%?T'
-bash: mysql: 未找到命令

又报错,很显然,PATH变量没改

[root@localhost bin]# PATH=$PATH:/usr/local/mysql/bin
[root@localhost bin]# mysql -uroot -p'WwWgn7sR7%?T'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with; or \g.
Your MySQL connection id is 2
Server version: 5.7.30

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.

root@(none) 15:42  mysql>alter user 'root'@'localhost' identified by 'Sanchuang123#';
Query OK, 0 rows affected (0.01 sec)

在使用时,你会突然发现,好像忘记了一件很重要的事情,字符集没有指定
root@(none) 15:44 mysql>show variables like “%char%”;
通过上述命令查看,果然没有指定。。。

root@(none) 17:30  mysql>show variables like '%char%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | latin1                            |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.01 sec)

没办法,再次进入配置文件里,在 [mysqld]下添加一行配置信息

[root@localhost bin]# cat /etc/my.cnf
[mysqld_safe]

[client]
socket=/data/mysql/mysql.sock

[mysqld]
socket=/data/mysql/mysql.sock
port = 3306
open_files_limit = 8192
innodb_buffer_pool_size = 512M
character-set-server=utf8

[mysql]
auto-rehash
prompt=\\u@\\d \\R:\\m  mysql>

更改配置文件,需要重启服务使其生效

[root@localhost mysql]# service mysqld  restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

成功!

[root@localhost mysql]# mysql -uroot -p'Sanchuang123#'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)

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.

root@(none) 15:56  mysql>

再查看字符集

root@(none) 17:30  mysql>show variables like '%char%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.01 sec)

接下来,将命令写入脚本,方便下次使用

#二进制安装的脚本
[root@mysql-binary ~]# cat onekey_binary_install_mysql.sh 
#!/bin/bash

#author:zwx
#time: 2020-10-6
#QQ:2624551985
####################################
#编译安装mysql 5.7.30
#os: centos7.8.2003 
####################################
#新建用户mysql
useradd  -s /sbin/nologin  mysql

#解决软件的依赖关系
yum  install cmake ncurses-devel gcc  gcc-c++  vim  lsof bzip2 openssl-devel -y

#解压二进制安装包
tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz 
#将解压后的文件夹移动剪切到/usr/local下改名为sc_mysql
mv mysql-5.7.30-linux-glibc2.12-x86_64  /usr/local/sc_mysql
#进入/usr/local/sc_mysql
cd  /usr/local/sc_mysql

#关闭防火墙firewalld
service firewalld stop
systemctl  disable firewalld

#关闭selinux
#临时关闭selinux
#永久关闭selinux
setenforce 0 
sed  -i  's/enforcing/disabled/' /etc/selinux/config 

#mysql的初始化操作
mkdir /data/mysql -p
chown mysql:mysql /data/mysql/
chmod 750 /data/mysql/

cd /usr/local/sc_mysql/bin
./mysqld  --initialize --user=mysql --basedir=/usr/local/sc_mysql/  --datadir=/data/mysql  &>passwd.txt

 #获得临时密码
tem_passwd=$(cat passwd.txt |grep "temporary"|awk '{print $NF}')
  #$NF表示最后一个字段
  # abc=$(命令)  优先执行命令,然后将结果赋值给abc 


#修改环境变量,添加我们编译安装的MySQL的可执行命令的路径
echo  "PATH=$PATH:/usr/local/sc_mysql/bin" >>/root/.bashrc
PATH=$PATH:/usr/local/sc_mysql/bin

#复制mysql提供的启动mysqld服务的脚本到/etc/init.d目录下-->复制mysqld的启动脚本
cp ../support-files/mysql.server    /etc/init.d/mysqld
sed  -i '66,73 s/mysql/sc_mysql/'    /etc/init.d/mysqld 
sed  -i '70c  datadir=/data/mysql'  /etc/init.d/mysqld
#生成/etc/my.cnf配置文件
cat  >/etc/my.cnf  <<EOF
[mysqld_safe]

[client]
socket=/data/mysql/mysql.sock

[mysqld]
socket=/data/mysql/mysql.sock
port = 3306
open_files_limit = 8192
innodb_buffer_pool_size = 512M
#指定mysql的字符集为utf8
character-set-server=utf8

[mysql]
auto-rehash
prompt=\\u@\\d \\R:\\m  mysql>
EOF

#启动mysqld服务
service mysqld  start

#设置mysqld开机启动
chkconfig --add mysqld

#登录重新设置初始密码为Sanchuang123#
#初次修改密码需要使用--connect-expired-password 选项
mysql -uroot -p$tem_passwd --connect-expired-password   -e  "set password='Sanchuang123#';"
#验证密码是否设置成功
mysql -uroot -pSanchuang123#  -e  'show databases;' && echo "database is installed success"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值