移除掉系统自带的mariadb数据库软件包 yum remove mariadb-libs -y 创建mysql用户 useradd -s /sbin/nologin -M mysql 创建mysql数据库存放目录和binlog日志目录 mkdir -pv /data/mysql/{data_3306,binlog_3306} 解压mysql二进制软件包,解压到/usr/local/下 tar xf mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz -C /usr/local/ 创建软连接mysql,方便后期升级不用改变目录 ln -s /usr/local/mysql-8.0.25-linux-glibc2.12-x86_64 /usr/local/mysql 添加mysql环境变量 echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh 让环境变量生效 source /etc/profile.d/mysql.sh 授权mysql的属主和数组给mysql的数据库目录 chown -R mysql.mysql /data/ 授权mysql软件安装目录给mysql属主属组 chown -R mysql.mysql /usr/local/mysql/ 安装libaio软件包(异步IO),不然会mysql初始化的时候会报错 yum install libaio libaio-devel -y 初始化mysql,指定mysql用户名,指定mysql安装目录,指定mysql数据库存放目录 mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/data_3306 返回结果: 2021-10-26T06:18:31.580889Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 2021-10-26T06:18:31.580990Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.25-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.25) initializing of server in progress as process 24590 2021-10-26T06:18:31.587352Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2021-10-26T06:18:32.218291Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2021-10-26T06:18:32.947514Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option 数据库是没有密码的可以直接使用mysql进入 准备数据库启动时的加载配置文件 vim /etc/my.cnf 写入以下内容 [mysqld] user=mysql basedir=/usr/local/mysql datadir=/data/mysql/data_3306 socket=/tmp/mysql.sock server_id=1 log_bin=/data/mysql/binlog_3306 port=3306 secure_file_priv = /data [mysql] socket=/tmp/mysql.sock 保存退出 解释: [mysqld] mysqld模块 user=mysql 指定mysql用户 basedir=/usr/local/mysql 指定mysql安装目录 datadir=/data/mysql/data_3306 指定mysql数据库目录存放目录 sockek=/tmp/mysql.sock mysql套接字,用于连接mysql使用 server_id=1 mysql的serverid的唯一标识符 log_bin=/data/mysql/binlog_3306 mysql的logbin日志启动后的存放目录 port=3306 mysql的端口 [mysql] 客户端mysql模块 sockek=/tmp/mysql.sock 客户端连接的套接字 复制启动程序到/etc/init.d/下 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 添加mysqld开机自启动 chkconfig --add mysqld 启动mysql service mysqld start 利用安全脚本初始化数据库 mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: 这里是问你是否有数据库的密码,我初始化的时候密码为空可以直接回车 Please set the password for root here. New password: 设置一个新的密码,输入密码时不显示 Re-enter new password: 再输入一次密码 By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y 是否移除匿名用户这里选择y回车 Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y 这里问你是否禁用root用户远程登录,根据自己的需求选择可以n或者y我选择y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) :y 是否移除test测试数据库这里选择 y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y 是否刷新权限这里选择y 登录mysql mysql -u root -p123456