博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql学习笔记---mysql的安装--二进制安装
阅读量:3960 次
发布时间:2019-05-24

本文共 11108 字,大约阅读时间需要 37 分钟。

安装mysql

二进制安装

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

[root@localhost ~]# lsanaconda-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 ~]# lsanaconda-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]# lsbin  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 stopRedirecting to /bin/systemctl stop firewalld.service[root@localhost sc_mysql]# systemctl disable firewalldRemoved 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=457902020-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 directoryStarting 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  startStarting 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/mysql2020-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=457902020-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  startStarting 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 2Server version: 5.7.30Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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.sockport = 3306open_files_limit = 8192innodb_buffer_pool_size = 512Mcharacter-set-server=utf8[mysql]auto-rehashprompt=\\u@\\d \\R:\\m  mysql>

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

[root@localhost mysql]# service mysqld  restartShutting 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 2Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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 #####################################新建用户mysqluseradd  -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_mysqlmv mysql-5.7.30-linux-glibc2.12-x86_64  /usr/local/sc_mysql#进入/usr/local/sc_mysqlcd  /usr/local/sc_mysql#关闭防火墙firewalldservice firewalld stopsystemctl  disable firewalld#关闭selinux#临时关闭selinux#永久关闭selinuxsetenforce 0 sed  -i  's/enforcing/disabled/' /etc/selinux/config #mysql的初始化操作mkdir /data/mysql -pchown 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/.bashrcPATH=$PATH:/usr/local/sc_mysql/bin#复制mysql提供的启动mysqld服务的脚本到/etc/init.d目录下-->复制mysqld的启动脚本cp ../support-files/mysql.server    /etc/init.d/mysqldsed  -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服务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"

转载地址:http://kqlzi.baihongyu.com/

你可能感兴趣的文章
linux文件属性及权限详解
查看>>
Find 命令使用详解
查看>>
Ext4,Ext3的特点和区别
查看>>
Linux文件系统目录结构的详细解说(二)
查看>>
Linux umount 报 device is busy 的处理方法
查看>>
一个大小为n的数组,里面的数都属于范围[0, n-1],有不确定的重复元素,找到至少一个重复元素,要求O(1)空间和O(n)时间。
查看>>
提供机制而不是策略
查看>>
内核中断机制
查看>>
内核抢占
查看>>
编译linux内核源码 ubuntu
查看>>
epoll使用详解
查看>>
epoll
查看>>
The AnimationClip 'Walk' used by the Animation component 'Pig' must be marked as Legacy.
查看>>
《Linux内核设计与实现》- Linux的进程
查看>>
《Linux内核设计与实现》- 进程的调度
查看>>
inet_ntoa()
查看>>
POSIX消息队列mq_open问题
查看>>
两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];
查看>>
用户态切换到内核态的3种方式
查看>>
笔试常见的智力题(附答案)
查看>>