这篇文章上次修改于 454 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

mysql包下载地址:

https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

创建muysql用户和用户组


groupadd mysql useradd -r -g mysql mysql

安装


cd /usr/local wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz tar -zxvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.30-linux-glibc2.12-x86_64 mysql mkdir /data/mysql mkdir /data/mysql/slow mkdir /var/log/mysql chown -R mysql:mysql /data/mysql chown -R mysql:mysql /var/log/mysql cd /usr/local/mysql ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data # 若出现错误:error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory,,执行下面的命令 yum install -y libaio

如下是运行初始化安装命令后的截图,会出现一串mysql密码,表示已安装完成。
请输入图片描述

修改mysql的配置

  • 配置文件在 /etc/my.cnf
  • 文件内容修改如下

[mysql] #设置mysql客户端默认字符集 default-character-set=utf8mb4 [mysqld] port = 3306 datadir = /data/mysql/data thread_stack = 256K table_open_cache = 1024 sort_buffer_size = 8M read_buffer_size = 4M join_buffer_size = 4M #irecord_buffer = 2M read_rnd_buffer_size = 8M open_files_limit = 16384 myisam_sort_buffer_size = 64M thread_cache_size = 128 query_cache_type=0 #skip-grant-tables #最大连接数配置 max_connections = 1000 max_connect_errors = 2000 #等待超时时间,单位:秒 wait_timeout = 300 #慢日志查询配置 #是否开启慢日志查询 slow_query_log = 1 #查询时间超过多长时间记录,单位:秒 long_query_time = 2 #慢日志记录文件路径 slow_query_log_file=/data/mysql/slow/slow_query.log #存储引擎 default-storage-engine=INNODB #数据缓冲区buffer pool大小, 这里需要根据自己服务器的内存大小调整 innodb_buffer_pool_size=10G #binlog日志配置 #设置唯一id server-id = 1 #开启binlog,产生的bin-log命名 log-bin = mysql-bin #binlog日志缓存 max_binlog_cache_size = 12M #binlog单个日志文件大小 max_binlog_size = 300M #多少天以前的binlog日志会被清除 expire_logs_days = 25 #binlog单个日志类型 binlog_format=ROW #[safe_mysqld] #err-log = /data/mysql-log/mysqld.log #pid-file = /data/mysql/iZ23d0i8ts1Z.pid [isamchk] key_buffer = 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M [myisamchk] key_buffer = 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M [mysqldump] quick max_allowed_packet = 32M [mysqlhotcopy] interactive-timeout

将mysql加入的service服务

cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysqld
chkconfig --add mysqld

vi /etc/profile
#最后一行添加
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile

启动msyql

service mysql start

设置mysql用户

#进入mysql
mysql -uroot -p 

# 设置root的密码
alter user user() identified by "新密码";
flush privileges;

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED WITH mysql_native_password BY '新密码';
flush privileges;

# 添加主从同步的用户
CREATE USER 'backdb'@'%' IDENTIFIED BY '密码';
GRANT REPLICATION SLAVE ON *.* to 'backdb'@'%' identified by '密码';
flush privileges;