步骤:
1、在根目录下创建文件夹software和数据库数据文件/data/mysql
#mkdir /software/
#mkdir /data/mysql
2、上传mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz文件到/software下
(这一步可在finalShell里连接到目标服务器后,直接拖拽文件到/software文件夹下完成)
#cd /software/
#tar -zxvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
3、更改解压缩后的文件夹名称
#mv /software/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz/ /software/mysql
4、创建mysql用户组和mysql用户
#groupadd mysql
#useradd -r -g mysql mysql
5、关联myql用户到mysql用户组中
#chown -R mysql:mysql /software/mysql/
#chown -R mysql:mysql /data/mysql/
#chown -R mysql /software/mysql/
#chown -R mysql /data/mysql
6、更改mysql安装文件夹mysql/的权限
#chmod -R 755 /software/mysql/
7、安装libaio依赖包(这一步可以跳过,可以等出错的时候再来安装依赖,也可以先安装依赖)
查询是否暗转libaio依赖包
#yum search libaio
如果没安装,可以用下面命令安装
#yum install libaio
8、初始化mysql命令
(请复制一下本步骤中的密码,在安装成功后需要用密码进行登录)
#cd /software/mysql/bin
#./mysqld --user=mysql --basedir=/software/mysql --datadir=/data/mysql --initialize
在执行上面命令时特别要注意一行内容
[Note] A temporary password is generated for root@localhost: o*s#gqh)F4Ck
root@localhost:
后面跟的是mysql数据库登录的临时密码,各人安装生成的临时密码不一样,面膜是后面的字母加数字
如果初始化时报错如下:
error while loading shared libraries: libnuma.so.1: cannot open shared objec
是因为libnuma安装的是32位,我们这里需要64位的,执行下面语句就可以解决
#yum install numactl.x86_64
执行完后重新初始化mysql命令
(
请注意,这里的numaclt.x86_64也未必是符合当下环境安装的,请看清楚报错提示,按照报错提示所缺失的依赖文件去安装,在安装前最好先卸载一次各个版本的 libao、linuma ,然后继续安装
rpm -e 软件名 卸载软件
rpm -ivh 软件名 查询某软件的安装进度
)
9、启动mysql服务(基本上都会报错)
#sh /software/mysql/support-files/mysql.server start
上面启动mysql服务命令是会报错的,因为没有修改mysql的配置文件,报错内容大致如下:
./support-files/mysql.server: line 239: my_print_defaults: command not found
./support-files/mysql.server: 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)
10、修改Mysql配置文件
(将默认的mysql启动目录、数据库存放目录改为当前创建的路径 )
打开mysql启动文件
#vim /software/mysql/support-files/mysql.server
修改前
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/usr/local/mysql/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
修改后
if test -z "$basedir"
then
basedir=/software/mysql
bindir=/software/mysql/bin
if test -z "$datadir"
then
datadir=/data/mysql
fi
sbindir=/software/mysql/bin
libexecdir=/software/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
保存退出
(以下这两步很重要,把mysql启动文件复制到开机自启下面,赋予权限)
#cp /software/mysql/support-files/mysql.server /etc/init.d/mysqld
#chmod 755 /etc/init.d/mysqld
11、修改my.cnf文件
#vim /etc/my.cnf
将下面内容复制替换当前的my.cnf文件中的内容
(复制粘贴后需要留意,由于我用的是vim编辑,导致从第6行之后,每行行首都自动加上了#注释,需要手动删除)
[client]
no-beep
socket =/software/mysql/mysql.sock
#pipe
#socket=0.0
port=3306
[mysql]
default-character-set=utf8
[mysqld]
basedir=/software/mysql
datadir=/data/mysql
port=3306
pid-file=/software/mysql/mysqld.pid
#skip-grant-tables
skip-name-resolve
socket = /software/mysql/mysql.sock
character-set-server=utf8
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true
#Server Id.
server-id=1
max_connections=2000
query_cache_size=0
table_open_cache=2000
tmp_table_size=246M
thread_cache_size=300
#限定用于每个数据库线程的栈大小。默认设置足以满足大多数应用
thread_stack = 192k
key_buffer_size=512M
read_buffer_size=4M
read_rnd_buffer_size=32M
innodb_data_home_dir = /data/mysql
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=16M
innodb_buffer_pool_size=256M
innodb_log_file_size=128M
innodb_thread_concurrency=128
innodb_autoextend_increment=1000
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
back_log=80
flush_time=0
join_buffer_size=128M
max_allowed_packet=1024M
max_connect_errors=2000
open_files_limit=4161
query_cache_type=0
sort_buffer_size=32M
table_definition_cache=1400
binlog_row_event_max_size=8K
sync_master_info=10000
sync_relay_log=10000
sync_relay_log_info=10000
#批量插入数据缓存大小,可以有效提高插入效率,默认为8M
bulk_insert_buffer_size = 64M
interactive_timeout = 120
wait_timeout = 120
log-bin-trust-function-creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#
#include all files from the config directory
#
!includedir /etc/my.cnf.d
保存退出
12、启动mysql
(我试过很多次卡在这里,唯独最后一次下载对了文件才成功,而原文中提到的两种方法都试过,不好使,如果各位遇到错误,还请检查一下下载的文件是否对口)
#/etc/init.d/mysqld start
新版本的安装包会报错,错误内容如下:
Starting MySQL.Logging to '/data/mysql/SZY.err'.
2018-07-02T10:09:03.779928Z mysqld_safe The file /usr/local/mysql/bin/mysqld
does not exist or is not executable. Please cd to the mysql installation
directory and restart this script from there as follows:
./bin/mysqld_safe&
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information
ERROR! The server quit without updating PID file (/software/mysql/mysqld.pid).
因为新版本的mysql安全启动安装包只认/usr/local/mysql这个路径。
解决办法:
方法1、建立软连接
例
#cd /usr/local/mysql
#ln -s /sofware/mysql/bin/myslqd mysqld
方法2、修改mysqld_safe文件(有强迫症的同学建议这种,我用的这种)
#vim /software/mysql/bin/mysqld_safe
将所有的/usr/local/mysql改为/software/mysql
保存退出。(可以将这个文件拷出来再修改然后替换)
13、登录mysql
#/software/mysql/bin/mysql -u root –p
然后输入密码,上文中第8条的密码。
14、临时密码失效
小编在这里也是临时密码不能用,可能是失效了,只能无密码登录了。
#vim /etc/my.cnf
中间小编也是为大家写好了
[mysqld]
basedir=/software/mysql
datadir=/data/mysql
port=3306
pid-file=/software/mysql/mysqld.pid
#skip-grant-tables
把skip-grant-tables前面的#删除掉
#/software/mysql/bin/mysql -u root –p
密码不用输入,直接回车
update user set authentication_string=password(‘填入新密码”’) where user=‘root’;
4.4 刷新mysql权限
mysql> flush privileges;
4.5 退出
mysql> exit;
再次vi /etc/my.cnf。把skip-grant-tables注释掉。保存退出。完成MySql Root密码修改
15、配置环境变量
打开profile文件,系统的环境变量都在里面
#vim /etc/profile
#系统生成的
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
#mysql环境变量
export PATH=$PATH:/software/mysql/bin
#jdk的环境变量
export JAVA_HOME=/usr/local/java/jdk1.8.0_231
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
再次登陆mysql就简单多了
#mysql -u root -p