第一篇 华为云安装MySql(CentOS7-mysql-8.0.20)

下载Mysql到你的目录文件中(我的是/data/src):

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

下载完成后解压文件:

xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar

将加压后的mysql文件移动到你心怡的目录里:

mv mysql-8.0.20-linux-glibc2.12-x86_64 /data/servers/mysql

创建mysql用户组和用户:

groupadd mysql

useradd -r -g mysql mysql

进入mysql目录,创建日志文件等目录,并授权读写给上面创建的mysql用户:

cd /data/servers/mysql

mkdir data logs tmp

chown -R mysql:mysql ./

chmod -R 775 ./

编辑/etc/my.cnf文件:

vim /etc/my.cnf

[mysql]
default-character-set=utf8mb4
socket=/data/servers/mysql/tmp/mysql.sock
[mysqld]
default-authentication-plugin=mysql_native_password
port=3306
socket=/data/servers/mysql/tmp/mysql.sock

basedir=/data/servers/mysql
datadir=/data/servers/mysql/data
character-set-server=utf8mb4
default-storage-engine=INNODB
innodb_buffer_pool_size=200M
max_allowed_packet=16M
explicit_defaults_for_timestamp=1
log-output=FILE
general_log=0
general_log_file=/data/servers/mysql/logs/general.log
slow_query_log=ON
slow_query_log_file=/data/servers/mysql/logs/slow-query.log
long_query_time=10

#数据库初始密码会在这个文件中: A temporary password is generated for root@localhost:KhqDvMU_3=y
log-error=/data/servers/mysql/logs/log-error.err

初始化数据库:(在你的mysql目录里执行也可以去bin目录里执行mysqld)

bin/mysqld --initialize --user=mysql --basedir=/data/servers/mysql --datadir=/data/servers/mysql/data

如果出现下面的错误:

bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

执行:yum install libaio*

配置mysql的环境变量:

vim /etc/profile 文件末尾追加

export PATH=$PATH:/data/servers/mysql/bin:/data/servers/mysql/lib

保存并关闭

source /etc/profile

复制mysql.server脚本到资源目录,并赋予执行权限(在mysql目录里):

cp support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig --add mysqld

启动mysql并修改初始密码和允许远程登陆:

启动:service mysqld start

登陆:mysql -uroot -p

输入日志文件中初始密码:KhqDvMU_3=y

修改密码:ALTER user 'root'@'localhost' IDENTIFIED BY 'yourpassword';

使用mysql库:use mysql

查询信息:select user,host from user
host字段中,localhost表示只允许本机访问,要实现远程连接,可以将root用户的host改为%,%表示允许任意host访问,如果需要设置只允许特定ip访问,则应改为对应的ip。
update user set host="%" where user="root";

使本次修改立即生效,命令:flush privileges;

安装结束

你可能感兴趣的:(MySql)