使用的是阿里云服务器
创建存放的文件夹
mkdir mysql
进入mysql文件夹下并下载指定版本压缩包
此处我下载的是5.7.29这是官网最新的GC版本
解压
tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
修改刚刚解压后的文件夹为mysql5729(文件夹名自己选定,本人只是不想文件夹名称太长,强迫症)
在当前文件夹下创建data文件夹,我的数据库解压后的内容所在目录是/develop/mysql/mysql5729
mkdir data
执行命令后创建了目录/develop/mysql/data
添加组
groupadd mysql
添加用户
useradd -r -g mysql mysql
权限
chown -R mysql:mysql /develop/mysql/mysql5729
初始化
/develop/mysql/mysql5729/bin/mysqld --initialize --user=mysql --basedir=/develop/mysql/mysql5729/ --datadir=/develop/mysql/data/
执行后,此处会产生临时密码需要记录下来,在更改密码时需要使用
开启SSL
/develop/mysql/mysql5729/bin/mysql_ssl_rsa_setup --datadir=/develop/mysql/data/
授权
chown -R mysql:mysql /develop/mysql/data/
配置my.cnf
进入配置文件所在目录:cd /etc
执行命令:vim my.cnf
后的文件原样如下:
我的配置如下:
[mysqld]
#设置mysql的安装目录
basedir=/develop/mysql/mysql5729
#设置mysql数据库的数据存放目录
datadir=/develop/mysql/data
socket=/develop/mysql/data/mysql.sock
pid-file=/develop/mysql/data/mysql.pid
port = 3306
user = mysql
bind-address = 0.0.0.0
server-id = 1
#character config
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1277
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 1024
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 256M
thread_cache_size = 64
query_cache_type = 1
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
log_error = /data/mysql/mysql-error.log
#slow_query_log = 1
#long_query_time = 1
#slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 1024M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
max_allowed_packet = 500M
[mysqld]
secure_file_priv=''
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 8M
read_buffer = 4M
[client]
default-character-set = utf8mb4
[mysql]
#设置默认字符集
default-character-set = utf8mb4
# Disabling symbolic-links is recommended to prevent assorted security risks
# #禁用符号连接
symbolic-links=0
skip_symbolic_links=yes
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/develop/mysql/log/mariadb.log
pid-file=/develop/mysql/data/mysql.pid
启动mysql服务
service mysqld start
出现错误:Failed to start mysqld.service: Unit not found.
然后了解到MariaDB代替了mysql数据库,安装MariaDB
# yum install mariadb -y //如果已安装可以省略
# systemctl start mariadb.service //启动服务
执行systemctl start mariadb.service时又出现另外一个问题:
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.
解决办法:执行命令cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
#systemctl start mariadb.service
发现mariadb服务已经启动起来了
# systemctl enable mariadb.service //开机启动服务
然后启动MySQL,发现还是不行
接着,查询/etc/init.d/下是否存在mysql:
ll /etc/init.d/ | grep mysql
发现该目录下并没有mysql的文件
查询mysql.server所在处
find / -name mysql.server
mysql.server一般在你安装的mysql目录下,自己是编译安装,所以知道目录在哪里,不知道的这样看一下
执行复制操作,/develop/mysql/mysql5729/是我自己的mysql安装目录,
cp /develop/mysql/mysql5729/support-files/mysql.server /etc/init.d/mysql
然后执行命令:service mysql start
出现了另外一个错误:Starting MySQL...The server quit without updating PID file [FAILED]p/mysql/mysql5729/data/mysql.pid).
这时候,很明显是配置文件配错了。
最后修改配置文件如下:
[mysqld]
#设置mysql的安装目录
basedir=/develop/mysql/mysql5729
#设置mysql数据库的数据存放目录
[mysqld]
datadir=/develop/mysql/data
port = 3306
user = mysql
bind-address = 0.0.0.0
server-id = 1
#character config
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
# Disabling symbolic-links is recommended to prevent assorted security risks
# #禁用符号连接
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
pid-file=/develop/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
再次执行命令:service mysql start,mysql启动成功!
最后记录一下如果启动数据库出现Starting MySQL...The server quit without updating PID file [FAILED]p/mysql/mysql5729/data/mysql.pid).时不要慌,仔细检查配置文件my.cnf,大多数时候都是因为配置文件有问题了。本人曾经就因为这个问题死活无法启动服务器的mysql,差点重装。两次都是因为配置文件出错了。