Linux安装mysql5.7

Linux环境下安装mysql5.7(tar安装)

1、为数据库创建软件目录以及数据存放目录

#mysql软件目录
mkdir /opt/mysql5.7/mysql-5.7
#mysql数据文件目录
mkdir /data/mysql

2、上传mysql-XXXXXX.tar.gz文件到mysql软件安装目录下:

cd /opt/mysql5.7
tar -zvxf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

3、更改解压缩后的文件夹名称

mv mysql-5.7.30-linux-glibc2.12-x86_64 mysql-5.7

4、创建mysql用户组和mysql用户

groupadd mysql
useradd -r -g mysql mysql

5、关联myql用户到mysql用户组中

chown -R mysql:mysql /opt/mysql5.7/mysql-5.7
chown -R mysql:mysql /data/mysql

6、更改mysql安装文件夹mysql/的权限

chmod -R 755 /opt/mysql5.7/mysql-5.7

7、安装libaio依赖包

我的是没有的

# 查询是否暗转libaio依赖包
yum search libaio
# 如果没安装,可以用下面命令安装
yum install libaio

8、初始化mysql命令

cd /opt/mysql5.7/mysql-5.7/bin
./mysqld --user=mysql --basedir=/opt/mysql5.7/mysql-5.7--datadir=/data/mysql --initialize


# 在执行上面命令时特别要注意一行内容:(可能会有所不同,这不重要)
2021-03-05T11:55:12.964768Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-03-05T11:55:13.969963Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-03-05T11:55:14.096883Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-03-05T11:55:14.163723Z 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: a554381d-7da9-11eb-a407-00163e0a5a16.
2021-03-05T11:55:14.165198Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-03-05T11:55:14.489984Z 0 [Warning] CA certificate ca.pem is self signed.
2021-03-05T11:55:14.898237Z 1 [Note] A temporary password is generated for root@localhost: tmgL2hmTwK%O

# root@localhost: 后面跟的是mysql数据库登录的临时密码,各人安装生成的临时密码不一样

9、修改Mysql配置文件

vim support-files/mysql.server

修改高亮部分,根据自己的目录安装

if test -z “ b a s e d i r " t h e n b a s e d i r = / o p t / m y s q l 5.7 / m y s q l − 5.7 b i n d i r = / o p t / m y s q l 5.7 / m y s q l − 5.7 / b i n i f t e s t − z " basedir" then basedir=/opt/mysql5.7/mysql-5.7 bindir=/opt/mysql5.7/mysql-5.7/bin if test -z " basedir"thenbasedir=/opt/mysql5.7/mysql5.7bindir=/opt/mysql5.7/mysql5.7/biniftestz"datadir”
then
datadir=/data/mysql
fi
sbindir=/opt/mysql5.7/mysql-5.7/bin
libexecdir=/opt/mysql5.7/mysql-5.7/bin
else
bindir=“ b a s e d i r / b i n " i f t e s t − z " basedir/bin" if test -z " basedir/bin"iftestz"datadir”
then
datadir=“ b a s e d i r / d a t a " f i s b i n d i r = " basedir/data" fi sbindir=" basedir/data"fisbindir="basedir/sbin”
libexecdir="$basedir/libexec"
fi

保存并退出:

cp /opt/mysql5.7/mysql-5.7/support-files/mysql.server  /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld

10、修改my.cnf文件

vim /etc/my.cnf

修改成一下内容:

[client]
no-beep
socket =/opt/mysql5.7/mysql-5.7/mysql.sock
# pipe
# socket=0.0
port=3306
[mysql]
default-character-set=utf8
[mysqld]
basedir=/opt/mysql5.7/mysql-5.7
datadir=/data/mysql
port=3306
pid-file=/opt/mysql5.7/mysql-5.7/mysqld.pid
#skip-grant-tables
skip-name-resolve
socket = /opt/mysql5.7/mysql-5.7/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

11、启动mysql

/etc/init.d/mysqld start

# 如下结果
Starting MySQL.Logging to '/data/mysql/iZbp113w9axywnoojhmptaZ.err'.

因为新版本的mysql安全启动安装包只认/usr/local/mysql这个路径。解决办法:

方法1、建立软连接

cd /usr/local/mysql
ln -s /opt/mysql5.7/mysql-5.7/bin/myslqd mysqld

方法2、修改mysqld_safe文件(有强迫症的同学建议这种,我用的这种)

vim /opt/mysql5.7/mysql-5.7bin/mysqld_safe
#将所有的/usr/local/mysql改为/software/mysql

本人使用的方法1

再次启动mysql

/etc/init.d/mysqld start

# 结果犯了一个错误,应该是重启mysql,结果我又用启动命令启动mysql了,导致mysql实例多了,会报一下错误
mysqld_safe A mysqld process already exists

# 使用ps命令查看运行的mysql
ps -ef|grep mysql


# 使用kill命令杀死进程
kill -9 3754
kill -9 4463
# 然后在使用
/etc/init.d/mysqld start

12、解决linux mysql命令 bash: mysql: command not found 的方法

原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
首先得知道mysql命令或mysqladmin命令的完整路径,比如mysql的路径是:/usr/local/mysql/bin/mysql,我们则可以这样执行命令:

ln -s /opt/mysql5.7/mysql-5.7/bin/mysql /usr/bin

13、登录mysql

cd /opt/mysql5.7/mysql-5.7/bin
/mysql -u root –p
# 然后输入之前的临时密码

14、修改密码

set password=password('123456');

# 允许所有IP访问 密码是123456
grant all privileges on *.* to 'root'@'%' identified by '123456';

flush privileges;

15、设置开机自启

# 赋予可执行权限
chmod +x /etc/init.d/mysqld

#添加为服务
chkconfig --add mysqld

#查看服务列表
chkconfig --list

#结果如下
[root@iZbp113w9axywnoojhmptaZ ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

看到3、4、5状态为开或者为 on 则表示成功。如果是 关或者 off 则执行一下:
chkconfig --level 345 mysqld on

重启计算机:reboot

# 再次查看服务列表或者查看3306端口号
netstat -na|grep 3306
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值