Centos7安装mysql


本文所有操作基于root用户进行

1 软件准备

  1. mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz 百度网盘下载
    将mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz上传至centos的/tmp目录下

2 安装步骤

  1. 解压mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
    使用以下命令进行解压:
cd /tmp
tar -xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
  1. 将解压出来的mysql-5.7.23-linux-glibc2.12-x86_64移动到/opt目录下
cd /tmp
mv mysql-5.7.23-linux-glibc2.12-x86_64 /opt/
  1. 修改mysql-5.7.23-linux-glibc2.12-x86_64名称为mysql
cd /opt
mv mysql-5.7.23-linux-glibc2.12-x86_64 mysql
  1. 创建数据库仓库
cd /opt/mysql
mkdir data
  1. 修改/etc/my.cnf文件,如果在/etc目录下没有这个文件,则直接创建即可
    my.cnf文件的内容修改成
[client]

[mysqld]
port=3306
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/tmp/mysql.sock

auto_increment_increment=1
auto_increment_offset=1
back_log=50
binlog_checksum=CRC32
binlog_format=MIXED
character_set_filesystem=binary
character_set_server=utf8
connect_timeout=10
default_storage_engine=InnoDB
div_precision_increment=4
explicit_defaults_for_timestamp=OFF
ft_query_expansion_limit=20
general_log=OFF
group_concat_max_len=1024
innodb_adaptive_hash_index=ON
innodb_adaptive_hash_index_parts=8
innodb_autoinc_lock_mode=1
innodb_concurrency_tickets=5000
innodb_flush_log_at_trx_commit=1
innodb_io_capacity=200
innodb_io_capacity_max=2000
innodb_large_prefix=ON
innodb_lock_wait_timeout=50
innodb_max_dirty_pages_pct=90.000000
innodb_old_blocks_pct=37
innodb_old_blocks_time=1000
innodb_open_files=2048
innodb_purge_batch_size=300
innodb_purge_threads=4
innodb_read_ahead_threshold=56
innodb_read_io_threads=4
innodb_rollback_on_timeout=OFF
innodb_stats_method=nulls_equal
innodb_stats_on_metadata=OFF
innodb_stats_sample_pages=8
innodb_strict_mode=ON
innodb_table_locks=ON
innodb_thread_concurrency=0
innodb_thread_sleep_delay=10000
innodb_write_io_threads=4
interactive_timeout=3600
key_cache_age_threshold=300
key_cache_block_size=1024
key_cache_division_limit=100
lock_wait_timeout=31536000
log_bin_trust_function_creators=OFF
log_queries_not_using_indexes=OFF
log_slow_admin_statements=OFF
log_timestamps=UTC
long_query_time=1.000000
low_priority_updates=OFF
lower_case_table_names=0
max_allowed_packet=67108864
max_heap_table_size=67108864
max_prepared_stmt_count=16382
net_read_timeout=30
net_retry_count=10
net_write_timeout=60
old_passwords=2
performance_schema=OFF
query_cache_limit=1048576
query_cache_type=OFF
query_prealloc_size=8192
#rpl_semi_sync_master_timeout=10000
slave_rows_search_algorithms=TABLE_SCAN,INDEX_SCAN
slow_launch_time=2
sql_mode=NO_ENGINE_SUBSTITUTION
sync_binlog=1000
table_definition_cache=1424
table_open_cache=2048
table_open_cache_instances=16
thread_cache_size=20
thread_stack=196608
#time_zone=SYSTEM
tmp_table_size=1048576
#tx_isolation=REPEATABLE-READ
wait_timeout=3600
  1. 新建mysql用户、组及目录
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql -d /opt/mysql
  1. 修改目录所有者
cd /opt/mysql
chown -R mysql .
chgrp -R mysql .
  1. 配置参数
cd /opt/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql

使用以上命令后,会出现如下提示:

[root@bogon bin]# ./mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql
2019-10-22T08:26:52.059797Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-22T08:26:52.060535Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2019-10-22T08:26:52.064513Z 0 [Warning] You need to use --log-bin to make --binlog-format work.
2019-10-22T08:26:52.365513Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-10-22T08:26:52.435111Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-10-22T08:26:52.588466Z 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: b34433ce-f4a5-11e9-9324-000c29e8705e.
2019-10-22T08:26:52.594688Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-10-22T08:26:52.596680Z 1 [Note] A temporary password is generated for root@localhost: ZrY&Xd646ccF
[root@bogon bin]#

以上提示中ZrY&Xd646ccF为mysql的初始密码

  1. 启动ssl
cd /opt/mysql/bin
./mysql_ssl_rsa_setup --datadir=/opt/mysql/data
  1. 启动mysql
cd /opt/mysql/bin
./mysqld_safe --user=mysql &
  1. 修改root的临时密码
    使用root用户登录
cd /opt/mysql/bin
./mysql -u root -p

使用上述命令后,会提示Enter password:,此时输入上面的临时密码,然后按回车。(注意:这里输入的密码不可见,只管正确输入然后按回车即可)
密码输入正确后会进入mysql命令模式,如下:

[root@bogon bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23

Copyright (c) 2000, 2018, 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.

mysql>

接着输入以下命令进行修改root的密码:

set password=password('root');

以上password(‘root’)中的root为新密码,读者根据实际修改成自己要的新密码即可
密码修改完成后再输入以下命令:

grant all privileges on *.* to root@'%' identified by 'root';
flush privileges;

以上命令中identified by 'root'中的rootroot用户的密码,读者需要将root修改成实际各自的密码
使用以下命令退出mysql命令行:

\q;

3 将mysql添加到环境变量中

使用以下命令打开/etc/profile文件:

vim /etc/profile

profile文件末尾添加以下内容:

PATH=/opt/mysql/bin:$PATH
export PATH

编辑完成后按Esc键,并输入:wq进行保存并退出操作
使用以下命令使新添加的环境变量生效:

source /etc/profile

4 将mysql添加至系统服务自启

使用以下命令可将mysql添加至系统服务并随系统启动:

cd /opt/mysql/support-files
cp mysql.server /etc/init.d/
cd /etc/init.d/
mv mysql.server mysql
chmod 755 /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on

5 使用系统服务命令对mysql操作

  1. 启动systemctl start mysql
  2. 停止systemctl stop mysql
  3. 重启systemctl restart mysql
  4. 查看状态systemctl status mysql
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值