二进制方式安装mysql

(1)获取MySQL二进制安装包

官网下载地址:

https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

(2)系统设置

第一步:关闭防火墙

 
  

systemctl stop firewalld

第二步:关闭selinux

 
  

sed -i 's/enforcing/disabled/' /etc/selinux/config

修改配置文件,重启服务器生效,无法通过命令修改为

第三步:检查是否有libaio没有则安装

 
  

rpm -qa |grep libaio

(3)创建用户

第一步:创建MySQL用户组和用户

创建用户的目的是启动和停止MySQL服务

 
  

groupadd mysql2 useradd -m -g mysql2 mysql2

-m 参数会自动在home目录下为mysql2 创建一个mysql2的目录

第二步:设置MySQL用户资源限制

 
  

cat << EOF >> /etc/security/limits.conf

mysql2 soft nproc 65535

mysql2 hard nproc 65535

mysql2 soft nofile 65535

mysql2 hard nofile 65535

EOF

(4)解压并设置MySQL

第一步:解压安装包

将安装包下载到服务器任意位置,解压二进制安装包

 
  

tar -zxf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

第二步:将解压后的目录考到指定目录

 
  

mv mysql-5.7.30-linux-glibc2.12-x86_64 /home/mysql2/mysql

第三步:创建MySQL数据目录

数据目录和软件目录分离

 
  

mkdir -p /Lcdmp3_mysqldata/mysqldata #数据目录

给目录归属改为mysql2用户

 
  

chown -R mysql2.mysql2 /Lcdmp3_mysqldata/ /home/mysql2

第四步:创建/etc/my.cnf

[mysqld]

#basic settings

secure_file_priv =

server-id = 3306

port = 3306

user = mysql2

autocommit = 1

character_set_server = utf8mb4

socket = /Lcdmp3_mysqldata/mysqldata/mysql57.socket

skip_name_resolve = 1

max_connections = 1000 #最大连接数

basedir = /home/mysql2/mysql

datadir = /Lcdmp3_mysqldata/mysqldata

pid-file = /Lcdmp3_mysqldata/mysqldata/mysql57.pid

transaction_isolation = READ-COMMITTED

join_buffer_size = 134217728 #查询缓冲区128M

tmp_table_size = 67108864 #临时表缓冲区大小

tmpdir = /tmp

max_allowed_packet = 16777216 # mysql服务器端和客户端在一次传送数据包的过程当中最大允许的数据包大小。16M

sql_mode = "STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION "

interactive_timeout = 1800

wait_timeout = 1800

sort_buffer_size = 33554432

#log settings

log_error = error.log

slow_query_log = on

slow_query_log_file = slow.log

expire_logs_days = 7

long_query_time = 3

log_bin = bin.log

sync_binlog = 1

binlog_format = row

#innodb settings

innodb_buffer_pool_size = 2048M #inodb表数据和索引数据最大缓冲池大小,不能超过最大内存的80%,根据实际情况修改

innodb_data_file_path = ibdata1:1G:autoextend #表空间目录,在数据目录下创建ibdata1

innodb_buffer_pool_instances = 8 #将缓存池划分的区域数,缓冲池较大时提高并发性

innodb_buffer_pool_load_at_startup = 1 #当mysql启动时,就会把上次的buffer pool热数据进行加载

innodb_buffer_pool_dump_at_shutdown = 1 # mysql关闭时,会自动将buffer pool中的热数据进行保存

innodb_lock_wait_timeout = 5 #事务锁超时时间

innodb_log_file_size = 500M #日志轮转大小

innodb_log_buffer_size = 16777216 #写日志的缓冲区大小,默认值16M,单位时字节。

innodb_purge_threads = 4 #用户清理的purge进程数量

innodb_large_prefix = 1 #使用前缀索引

innodb_print_all_deadlocks = 1 #打印死锁日志

innodb_strict_mode = 1 #启用严格模式,该模式语法错误会直接抛出错误。

innodb_sort_buffer_size = 67108864 #创建InnoDB索引时用于指定对数据排序的排序缓冲区的大小,64M

innodb_flush_log_at_trx_commit = 2 #入库速率参数

log_bin_trust_function_creators=1 #主从服务参数

#如果想通过mysql -uroot -p 命令直接本地登陆需要配置socket,否则的话需要加-h指定host才能登陆,前者方便,后者更安全

[client]

port=3306

socket=/Lcdmp3_mysqldata/mysqldata/mysql57.socket

无注释版本

[mysqld]
#basic settings
secure_file_priv =
server-id = 3306
port = 3306
user = mysql2
autocommit = 1
character_set_server = utf8mb4
socket = /Lcdmp3_mysqldata/mysqldata/mysql57.socket
skip_name_resolve = 1
max_connections = 1000
basedir = /home/mysql2/mysql
datadir = /Lcdmp3_mysqldata/mysqldata
pid-file = /Lcdmp3_mysqldata/mysqldata/mysql57.pid
transaction_isolation = READ-COMMITTED
join_buffer_size = 134217728
tmp_table_size = 67108864
tmpdir = /tmp
max_allowed_packet = 16777216
sql_mode = "STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
interactive_timeout = 1800
wait_timeout = 1800
sort_buffer_size = 33554432
#log settings
log_error = error.log
slow_query_log = on
slow_query_log_file = slow.log
expire_logs_days = 7
long_query_time = 3
log_bin = bin.log
sync_binlog = 1
binlog_format = row 
#innodb settings
innodb_buffer_pool_size = 2048M
innodb_data_file_path = ibdata1:1G:autoextend
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lock_wait_timeout = 5
innodb_log_file_size = 500M
innodb_log_buffer_size = 16777216
innodb_purge_threads = 4
innodb_large_prefix = 1
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 67108864
innodb_flush_log_at_trx_commit = 2
log_bin_trust_function_creators=1
#set client
 
[client]
port=3306
socket=/Lcdmp3_mysqldata/mysqldata/mysql57.socket

第五步:初始化数据库

/home/mysql2/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql2

成功了会在数据目录创建一些文件

在日志里面查询初始化密码。

grep -i "password" /Lcdmp3_mysqldata/mysqldata/error.log

如果出错也可以在/Lcdmp3_mysqldata/mysqldata/ error.log 查看错误原因。

第六步:创建启动脚本

1在mysql程序目录创建启动脚本文件。

cat << EOF >> start_mysql.sh

/home/mysql2/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf >/dev/null 2>&1 &

EOF

创建关闭脚本,需要输入数据库密码才能停止

cat << EOF >> stop_mysql.sh

/home/mysql2/mysql/bin/mysqladmin shutdown –uroot -S /Lcdmp3_mysqldata/mysqldata/mysql57.socket -p

EOF

2或者使用官方的启动脚本

cp /home/mysql2/mysql/support-files/mysql.server /etc/init.d/mysqld

/etc/init.d/mysqld start/stop/restart

service mysqld start/stop/restart

第七步:启动MySQL

sh start_mysql.sh

配置环境变量,主要用于登录mysql的。

cat << EOF >> /etc/profile.d/mysql.sh

export PATH=/home/mysql2/mysql/bin:$PATH

EOF

source /etc/profile.d/mysql.sh

登录mysql

mysql -uroot -p

然后根据提示输入初始密码

然后修改成我们想要设置的密码:

mysql> set password='1234%qwer';

报错处理:

如果忘记初始化密码;

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

解决方法:

(1)/etc/my.cnf 的[mysqld]域下添加 skip-grant-tables,并重启mysql,即可免密登陆

(2)然后修改密码

update user set authentication_string=PASSWORD("1234%qwer") where user='root';

(3)删除/etc/my.cnf 的[mysqld]域下添加 skip-grant-tables,并重启mysql

(4)重新用密码登陆 mysql -uroot -p1234%qwer

你可能感兴趣的:(数据库,mysql,数据库,服务器)