张小白带你在华为云ECS服务器上安装MySQL 5.7
华为云ECS服务器在购买后,会内置一个类似MySQL的数据库,叫做mariadb。
一般情况下,如果要使用MySQL,最好把这个数据库替换掉,下面是张小白参照网上的介绍进行的替换实践。
使用root登录,查看mariadb的版本:
删除mariadb:
查看MySQL是否安装:
下载MySQL5.7.37的代码包:
cd /usr/local
wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.37-el7-x86_64.tar.gz
解压:
tar -zxvf mysql-*.gz
mv mysql-5.7.37-el7-x86_64 mysql
设置权限:
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
mkdir -p /data/mysql/data
mkdir -p /data/mysql/logs
mkdir -p /data/mysql/tmp
chown -R mysql:mysql /data/mysql
编辑 /etc/my.cnf 文件:
[client]
port = 3306
socket = /data/mysql/tmp/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/data
port = 3306
socket = /data/mysql/tmp/mysql.sock
pid-file = /data/mysql/tmp/mysqld.pid
tmpdir = /data/mysql/tmp
skip_name_resolve = 1
symbolic-links=0
max_connections = 2000
group_concat_max_len = 1024000
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names = 1
log_timestamps=SYSTEM
character-set-server = utf8
interactive_timeout = 1800
wait_timeout = 1800
max_allowed_packet = 32M
binlog_cache_size = 4M
sort_buffer_size = 2M
read_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 96M
max_heap_table_size = 96M
max_length_for_sort_data = 8096
#logs
server-id = 1003306
log-error = /data/mysql/logs/error.log
slow_query_log = 1
slow_query_log_file = /data/mysql/logs/slow.log
long_query_time = 3
log-bin = /data/mysql/logs/binlog
binlog_format = row
expire_logs_days = 15
log_bin_trust_function_creators = 1
relay-log = /data/mysql/logs/relay-bin
relay-log-recovery = 1
relay_log_purge = 1
#innodb
innodb_file_per_table = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_flush_method = O_DIRECT
innodb_autoinc_lock_mode = 2
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_buffer_pool_size = 2G
将mysql设置为linux服务:
cd /usr/local/mysql/support-files
cp mysql.server /etc/init.d/mysql
编辑 /etc/init.d/mysql 文件:
设置以下两行:
basedir=/usr/local/mysql
datadir=/data/mysql/data
chkconfig --add mysql
chkconfig --list
编辑环境变量:
echo "PATH=$PATH:/usr/local/mysql/bin " >> /etc/profile
source /etc/profile
手工启动MySQL:
报错!
需要安装libaio软件包:
rpm -qa|grep libaio
yum install libaio-devel.x86_64
再次启动MySQL尝试:
上面箭头所指的一串字符就是临时的root密码。
重启MySQL:
service stop mysql
service start mysql
登录MySQL命令行:
mysql -uroot -p
输入前面的临时密码进入命令行。
修改root密码:
alter user 'root'@'localhost' identified by 'xxxxxx';
flush privileges;
quit退出命令行。
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
可以再次重启MySQL:
service restart mysql
此后就可以使用新的密码在命令行登陆了。
如果想使用远程登录,需要修改以下内容:
grant all privileges on *.* to'root'@'%' identify by 'xxxxxx' with grant option;
flush privileges;
再重启MySQL即可。
(全文完,谢谢阅读)
- 点赞
- 收藏
- 关注作者
评论(0)