当前位置:   article > 正文

centos上安装MySQL_centos 安装mysql

centos 安装mysql

方案一:

步骤 1 在 ecs-mysql 上安装 mysql。
通过下面的命令安装 mysql:

cd /opt
wget https://hciecloud.obs.cn-north-4.myhuaweicloud.com/MySQL-5.6.45-1.el6.x86_64.rpm-bundle.tar
mkdir mysql_install
tar -xvf MySQL-5.6.45-1.el6.x86_64.rpm-bundle.tar -C mysql_install
cd mysql_install
yum -y remove mariadb*
yum install -y MySQL-shared-compat-5.6.45-1.el6.x86_64.rpm
yum install -y MySQL-server-5.6.45-1.el6.x86_64.rpm
yum install -y MySQL-client-5.6.45-1.el6.x86_64.rpm
yum install -y MySQL-devel-5.6.45-1.el6.x86_64.rpm
yum install -y MySQL-shared-5.6.45-1.el6.x86_64.rpm

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

结果如下:
在这里插入图片描述
步骤 5 修改配置 Mysql 文件。
通过以下整段命令新增新的 my.cnf 配置文件

cat << EOF >> /etc/my.cnf
[mysqld]
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
lower_case_table_names = 1
innodb_strict_mode = 1
sql_mode =
symbolic-links=0
character_set_server = utf8
log-bin = mysql-bin
binlog_format=row
server-id = 2
expire_logs_days = 10
slave_skip_errors = 1062
innodb_strict_mode = 0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

通过以下命令确认 mysql 配置文件:

cat /etc/my.cnf
  • 1

步骤 6 在 ecs-mysql 上启动 mysql。
通过以下命令启动 mysql:

systemctl start mysql
  • 1

在这里插入图片描述
步骤 7 在 ecs-mysql 上修改 mysql 密码。

1、编写脚本 mysql_pass.sh,脚本内容如下,使用 vim mysql_pass.sh 然后输入内容后 :wq 保存
#!/bin/bash
# 安装 expect 插件
yum install -y expect
# 修改密码为 Huawei@123!
pass=`awk -F"[ :]+" 'NR==1{print $NF}' /root/.mysql_secret`
/bin/expect << EOF
spawn /usr/bin/mysql -h127.0.0.1 -uroot -p`echo -e $pass`
expect "mysql>"
send "SET PASSWORD FOR root@localhost=PASSWORD('Huawei@123!');"
send "\n"
expect "mysql>"
send 'flush privileges\n'
expect "mysql>"
send 'quit\n'
interact
EOF
2、增加可执行权限并执行脚本,输入如下两条命令
chmod +x mysql_pass.sh
./mysql_pass.sh
3、使用修改后的密码登录
mysql –u root –p
Huawei@123!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述

在这里插入图片描述
步骤 8 设置数据库访问权限。
在 mysql>界面输入以下命令为同步账户授予复制权限:

grant all privileges on *.* to root@"%" identified by "Huawei@123!";
flush privileges;
  • 1
  • 2

在这里插入图片描述
修改完毕通过以下命令退出 mysql:

quit
  • 1

在这里插入图片描述

方案二

TMOUT=0
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum localinstall mysql57-community-release-el7-8.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server
systemctl start mysqld
systemctl status mysqld
systemctl enable mysqld
systemctl daemon-reload
grep 'temporary password' /var/log/mysqld.log
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Huawei123!';
use mysql;
update user set host = '%' where user = 'root';
select host,user from user;
flush privileges;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

方案三


#!/bin/bash
#yum
wget -i -c https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
yum -y install mysql80-community-release-el7-3.noarch.rpm
#mysql8.0
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum -y install mysql-community-server
systemctl enable --now  mysqld.service
 
#mysql初始化
passwd=`grep  password /var/log/mysqld.log | cut -d " " -f 13`
passwd2=Lang@zuo123.
passwd3=123456
echo $passwd
sleep 2
mysql --connect-expired-password  -uroot -p${passwd} -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lang@zuo123.';"
mysql --connect-expired-password  -uroot -p${passwd2} -e "set global validate_password.policy=0;set global validate_password.length=1;"
mysql --connect-expired-password  -uroot -p${passwd2} -e "use mysql;ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';"
mysql --connect-expired-password  -uroot -p${passwd3} -e "use mysql;update user set host = '%'  where user = 'root';"
mysql --connect-expired-password  -uroot -p${passwd3} -e "ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';"
mysql -uroot -p${passwd3} -e "create database halodb character set utf8mb4 collate utf8mb4_bin;"
echo "mysql密码为passwd3"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

授权

use mysql;
update user set host = '%' where user = 'root';
select host,user from user;
flush privileges;
  • 1
  • 2
  • 3
  • 4

方案四

yum -y install mariadb mariadb-server
systemctl enable mariadb.service
systemctl start mariadb.service
mysql_secure_installation
use mysql;
update user set host = '%' where user = 'root';
select host,user from user;
flush privileges;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

方案五

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum localinstall mysql57-community-release-el7-8.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server
systemctl start mysqld
systemctl status mysqld
systemctl enable mysqld
systemctl daemon-reload
grep 'temporary password' /var/log/mysqld.log
mysql -u root -p
输入以下命令,修改 MySQL root 的密码为 Huawei123!
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Huawei123!';
use mysql;
输入以下命令,设置任意主机可以通过 root 登录。
update user set host = '%' where user = 'root';
输入以下命令,查看信息配置是否正确。
select host,user from user;
输入以下命令,刷新权限,保证配置立即生效。
flush privileges;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/217180
推荐阅读
相关标签
  

闽ICP备14008679号