CentOS7下安装MySQL5.7(生产模式)

it2024-11-22  2

前言

之前有篇总结了用rpm包简单安装MySQL5.7,这篇文章总结一下生产模式下MySQL的安装配置。

安装

1.解压及创建目录

# mysql安装到/usr/local目录下 tar -xzvf mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz -C /usr/local mv mysql-5.7.11-linux-glibc2.5-x86_64 mysql # 解压完成后 ll看一下 mysql的用户和用户组 drwxr-xr-x 9 7161 wheel 129 2月 2 2016 mysql

2.创建必要目录

# arch 归档 tmp 临时目录 mkdir mysql/arch mysql/data mysql/tmp

配置

1.修改配置文件

vi /etc/my.cnf

gg定位到内容头部,dG清除内容,按i进入insert模式下 添加以下全部内容

[client] port = 3306 socket = /usr/local/mysql/data/mysql.sock default-character-set=utf8mb4 [mysqld] port = 3306 socket = /usr/local/mysql/data/mysql.sock skip-slave-start skip-external-locking key_buffer_size = 256M sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 4M query_cache_size= 32M max_allowed_packet = 16M myisam_sort_buffer_size=128M tmp_table_size=32M table_open_cache = 512 thread_cache_size = 8 wait_timeout = 86400 interactive_timeout = 86400 max_connections = 600 # Try number of CPU's*2 for thread_concurrency #thread_concurrency = 32 #isolation level and default engine default-storage-engine = INNODB transaction-isolation = READ-COMMITTED server-id = 1739 basedir = /usr/local/mysql datadir = /usr/local/mysql/data pid-file = /usr/local/mysql/data/hostname.pid #open performance schema log-warnings sysdate-is-now binlog_format = ROW log_bin_trust_function_creators=1 log-error = /usr/local/mysql/data/hostname.err log-bin = /usr/local/mysql/arch/mysql-bin expire_logs_days = 7 innodb_write_io_threads=16 relay-log = /usr/local/mysql/relay_log/relay-log relay-log-index = /usr/local/mysql/relay_log/relay-log.index relay_log_info_file= /usr/local/mysql/relay_log/relay-log.info log_slave_updates=1 gtid_mode=OFF enforce_gtid_consistency=OFF # slave slave-parallel-type=LOGICAL_CLOCK slave-parallel-workers=4 master_info_repository=TABLE relay_log_info_repository=TABLE relay_log_recovery=ON #other logs #general_log =1 #general_log_file = /usr/local/mysql/data/general_log.err #slow_query_log=1 #slow_query_log_file=/usr/local/mysql/data/slow_log.err #for replication slave sync_binlog = 500 #for innodb options innodb_data_home_dir = /usr/local/mysql/data/ innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend innodb_log_group_home_dir = /usr/local/mysql/arch innodb_log_files_in_group = 4 innodb_log_file_size = 1G innodb_log_buffer_size = 200M #根据生产需要,调整pool size innodb_buffer_pool_size = 2G #innodb_additional_mem_pool_size = 50M #deprecated in 5.6 tmpdir = /usr/local/mysql/tmp innodb_lock_wait_timeout = 1000 #innodb_thread_concurrency = 0 innodb_flush_log_at_trx_commit = 2 innodb_locks_unsafe_for_binlog=1 #innodb io features: add for mysql5.5.8 performance_schema innodb_read_io_threads=4 innodb-write-io-threads=4 innodb-io-capacity=200 #purge threads change default(0) to 1 for purge innodb_purge_threads=1 innodb_use_native_aio=on #case-sensitive file names and separate tablespace innodb_file_per_table = 1 lower_case_table_names=1 [mysqldump] quick max_allowed_packet = 128M [mysql] no-auto-rehash default-character-set=utf8mb4 [mysqlhotcopy] interactive-timeout [myisamchk] key_buffer_size = 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M

2.创建用户组及用户

具体什么服务,就创建一个具体用户来管理

groupadd -g 101 dba useradd -u 514 -g dba -G root -d /usr/local/mysql mysqladmin id mysqladmin uid=514(mysqladmin) gid=101(dba) 组=101(dba),0(root)

3.copy 环境变量配置文件至mysqladmin用户的home目录中,为了以下步骤配置个人环境变量

cp /etc/skel/.* /usr/local/mysql

4…配置环境变量

vi mysql/.bashrc

添加

export MYSQL_BASE=/usr/local/mysql export PATH=${MYSQL_BASE}/bin:$PATH

5.赋权限和用户组,切换用户mysqladmin,安装

chown mysqladmin:dba /etc/my.cnf chmod 640 /etc/my.cnf chown -R mysqladmin:dba /usr/local/mysql chmod -R 755 /usr/local/mysql

6.配置服务及开机自启动

[root@hadoop local] cd /usr/local/mysql #将服务文件拷贝到init.d下,并重命名为mysql [root@hadoop mysql] cp support-files/mysql.server /etc/rc.d/init.d/mysql #赋予可执行权限 [root@hadoop mysql] chmod +x /etc/rc.d/init.d/mysql #删除服务 [root@hadoop mysql] chkconfig --del mysql #添加服务 [root@hadoop mysql] chkconfig --add mysql [root@hadoop mysql] chkconfig --level 345 mysql on vi /etc/rc.local

添加下面一句

su - mysqladmin -c "/etc/init.d/mysql start --federated"

7.安装libaio及安装mysql的初始db

[root@hadoop mysql] yum -y install libaio [root@hadoop mysql] su - mysqladmin hadoop:mysqladmin:/usr/local/mysql:> bin/mysqld \ --defaults-file=/etc/my.cnf \ --user=mysqladmin \ --basedir=/usr/local/mysql/ \ --datadir=/usr/local/mysql/data/ \ --initialize

初始化完成后,查看临时密码

cd data/ cat hostname.err |grep password

8.启动,登录及修改用户密码

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & 回车后 光标停在 如下 2021-01-11T06:20:40.665779Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data 再回车一下 就ok [mysqladmin@node1 ~]$ mysql -uroot -p’临时密码‘ Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.11-log Copyright (c) 2000, 2016, 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> _

设置用户权限

mysql> alter user root@localhost identified by '新密码'; Query OK, 0 rows affected (0.05 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '新密码' ; Query OK, 0 rows affected, 1 warning (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit; Bye

9.重启服务

hadoop:mysqladmin:/usr/local/mysql:> service mysql restart

登录账户密码,或者查看服务状态

10.无需重装

mysql出了问题,无需uninstall install,清空data 和 arch 两个目录的内容,重新初始化即可

rm -rf arch/* data/* su - mysqladmin bin/mysqld \ --defaults-file=/etc/my.cnf \ --user=mysqladmin \ --basedir=/usr/local/mysql/ \ --datadir=/usr/local/mysql/data/ \ --initialize

使用

查看MySQL服务是否启动 若没启动 切mysqladmin用户 su - mysqladmin service mysql start Starting MySQL. SUCCESS! 退回到root登录 或者直接登录 exit cd /usr/local/mysql/bin ./mysql -uroot -p 输入密码: ... mysql>
最新回复(0)