centos 安装mysql 两种方式: 离线,在线

  • 注: 若使用rpm安装,在离线环境可能导致依赖问题,终极解决办法是二进制安装

1, centos6 安装mysql 5.5

A, 离线安装: rpm包

centos6 mysql5.5 离线包下载: https://dev.mysql.com/downloads/mysql/5.5.html#downloads
【mysql5.5安装后默认没有密码, 比其他的版本方便】

国内镜像:
https://mirrors.huaweicloud.com/mysql/Downloads/
https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/

#1, 删除已安装的mysql服务, 顺序安装新的rpm包
rpm -qa |grep mysql |xargs -n 1 rpm -e --nodeps
whereis mysql  |xargs -n 1 rm -rf
whereis mysqld |xargs -n 1 rm -rf
rm -rf /var/lib/mysql
 
#2,安装mysql5.5
mkdir ~/mysqls
tar -xvf  ~/MySQL-5.5.62-1.el6.x86_64.rpm-bundle.tar -C ~/mysqls ; cd ~/mysqls
rpm -ivh MySQL-devel-5.5.62-1.el6.x86_64.rpm  --nodeps --force
rpm -ivh MySQL-shared-5.5.62-1.el6.x86_64.rpm  --nodeps --force
rpm -ivh MySQL-shared-compat-5.5.62-1.el6.x86_64.rpm  --nodeps --force
rpm -ivh MySQL-embedded-5.5.62-1.el6.x86_64.rpm  --nodeps --force
rpm -ivh MySQL-client-5.5.62-1.el6.x86_64.rpm  --nodeps --force
rpm -ivh MySQL-server-5.5.62-1.el6.x86_64.rpm  --nodeps --force

#启动mysql,设置root密码
service mysql start
mysqladmin -u root password '123456'

B, 在线安装

#1,  删除已安装的mysql服务, 顺序安装新的rpm包
rpm -qa |grep mysql |xargs -n 1 rpm -e --nodeps
whereis mysql  |xargs -n 1 rm -rf
whereis mysqld |xargs -n 1 rm -rf
rm -rf /var/lib/mysql
 
yum -y install wget
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
yum list |grep mysql 
yum -y install mysql-server mysql mysql-devel
 
#2, 启动服务,修改root密码
service mysqld start
service mysqld status
mysqladmin -u root password 123456

C, 离线安装: 二进制包

下载依赖包

yum -y install libaio  numactl #mysql8:  numactl ncurses-compat-libs

mysql5.5/5.6

安装包下载 (搜索glibc2):https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6
mysql5.5/6 二进制安装文档: https://dev.mysql.com/doc/refman/5.6/en/binary-installation.html

shell>
	basedir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64
	bindir=$basedir/bin
	datadir=$basedir/data

	groupadd mysql
	useradd -r -g mysql -s /bin/false mysql
	cd /usr/local
	tar zxvf /path/to/mysql-VERSION-OS.tar.gz
	ln -s full-path-to-mysql-VERSION-OS mysql

#1,初始化mysql	
	cd mysql
	mkdir tmp
	touch mysq56.err #指定日志文件
	
	# 查看变量  ./bin/mysqld --verbose --help |grep pid
	# 创建配置文件
	[root@lvs-c6 mysql-5.6.50-linux-glibc2.12-x86_64]# grep -Ev '^$|^#' my.cnf
		[mysql]
		port=3356
		socket=/tmp/mysql56.sock
		[mysqld]
		basedir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64
		datadir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64/data
		port=3356
		log-error=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64/mysq56.err
		pid-file=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64/mysql56.pid
		tmpdir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64/tmp
		socket=/tmp/mysql56.sock
		sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

	 chown mysql. . -R
	 scripts/mysql_install_db --user=mysql --defaults-file=$basedir/my.cnf  

#2,启动mysql
	 $bindir/mysqld_safe  --defaults-file="$basedir/my.cnf"    &
	 
	 #测试连接
	 #./bin/mysql --protocol=tcp  -uroot -P3356 -p''
	 #./bin/mysql --defaults-file=./my.cnf 

	 mysql> select user,host from mysql.user;
	+------+-----------+
	| user | host      |
	+------+-----------+
	| root | 127.0.0.1 |
	| root | ::1       |
	|      | localhost |
	| root | localhost |
	|      | lvs-c6    |
	| root | lvs-c6    |
	+------+-----------+
	6 rows in set (0.00 sec)

	mysql> update mysql.user set password=password('123@abc') where user='root';
	Query OK, 4 rows affected (0.00 sec)
	Rows matched: 4  Changed: 4  Warnings: 0

	mysql> flush privileges;
	Query OK, 0 rows affected (0.00 sec)


#3,使用service管理服务
	 export PATH=$PATH:/usr/local/mysql/bin
	 #cp support-files/mysql.server /etc/init.d/mysql56

shell> cat /etc/init.d/mysql56
...
basedir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64
bindir=$basedir/bin
datadir=$basedir/data
mysqld_pid_file_path=$basedir/mysql56.pid

case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      #$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
      $bindir/mysqld_safe  --defaults-file="$basedir/my.cnf"   $other_args >/dev/null &
      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
	...

mysql5.7/8.0

mysql5.7 二进制安装文档:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
mysql8.0 二进制安装文档:https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html

shell> 
 groupadd mysql
 useradd -r -g mysql -s /bin/false mysql
 cd /usr/local
 tar zxvf /path/to/mysql-VERSION-OS.tar.gz
 ln -s full-path-to-mysql-VERSION-OS mysql

 cd mysql
 # 查看变量  ./bin/mysqld --verbose --help |grep pid
 #创建配置文件 vi ./my.cnf
	[mysql]
	port=3357
	socket=/tmp/mysql57.sock
	[mysqld]
	basedir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64
	datadir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/data
	port=3357
	log-error=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/mysql57.err
	pid-file=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/mysql57.pid
	tmpdir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/tmp
	socket=/tmp/mysql57.sock
	sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
	explicit_defaults_for_timestamp = OFF

 mkdir mysql-files
 chown mysql:mysql mysql-files
 chmod 750 mysql-files
 mkdir tmp
 touch mysql57.err #指定日志文件
 chown mysql. . -R
 
 
 #1, 初始化数据
 basedir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64
 bindir=$basedir/bin
 datadir=$basedir/data
 bin/mysqld --initialize --user=mysql  --basedir=$basedir --datadir=$datadir
 bin/mysql_ssl_rsa_setup --user=mysql  --basedir=$basedir --datadir=$datadir

 #2, 启动mysql
 [root@lvs-c6 mysql-5.7.31-linux-glibc2.12-x86_64]# cat /tmp/mysqinit.txt
 	#mysql 5.5/6 
 	#update mysql.user set password=password('123@abc') where user='root';
 	#mysql 5.7
	#update mysql.user set authentication_string=password('123@abc') where user='root';
	##mysql 5.5/5.6/5.7/8.0
	alter user 'root'@'localhost' identified by '123@abc_456';
	flush privileges;

 [root@lvs-c6 mysql-5.7.31-linux-glibc2.12-x86_64]# bin/mysqld_safe \
   --user=mysql --basedir=$basedir  --datadir=$datadir \
   --pid-file=$basedir/mysql57.pid --log-error=$basedir/mysql57.err  \
   --init-file=/tmp/mysqinit.txt 


shell> ./bin/mysql --protocol=tcp  -uroot -P3357 -p'123@abc_456'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
 

#3,使用service管理服务
cp support-files/mysql.server /etc/init.d/mysql.server
shell> cat /etc/init.d/mysql57
...
basedir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64
bindir=$basedir/bin
datadir=$basedir/data
mysqld_pid_file_path=$basedir/mysql57.pid

case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      #$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
      $bindir/mysqld_safe  --user=mysql --basedir=$basedir  --datadir=$datadir --pid-file=$basedir/mysql57.pid --log-error=$basedir/mysql57.err $other_args >/dev/null &
   ... 

2, mysql配置文件:my.cnf

参考:https://blog.csdn.net/eyeofeagle/article/details/109363102

3, openSuse12 安装mysql: 二进制包

zypper in libaio1  libnuma1 
#ln -s libncurses.so.5.6 /lib64/libtinfo.so.5

$> groupadd mysql
$> useradd -r -g mysql -s /bin/false mysql
$> cd /usr/local
$> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
$> ln -s full-path-to-mysql-VERSION-OS mysql
$> cd mysql
$> mkdir mysql-files
$> chown mysql:mysql mysql-files
$> chmod 750 mysql-files
$> bin/mysqld --initialize --user=mysql

$> bin/mysql_ssl_rsa_setup
$> bin/mysqld_safe --user=mysql &
# Next command is optional
$> cp support-files/mysql.server /etc/init.d/mysql.server


初始化mysql输出信息:
su12:/usr/local/mysql # bin/mysqld --initialize --user=mysql
2021-10-19T06:52:04.454725Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-19T06:52:04.716943Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-19T06:52:05.008412Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-19T06:52:05.105998Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 11fd447b-30a9-11ec-8607-080027801586.
2021-10-19T06:52:05.109051Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-19T06:52:05.492032Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-19T06:52:05.628489Z 1 [Note] A temporary password is generated for root@localhost: gaE5Roerln.r
su12:/usr/local/mysql # ls
LICENSE  README  bin  data  docs  include  lib  man  mysql-files  share  support-files
su12:/usr/local/mysql # ls data/
auto.cnf    ca.pem           client-key.pem  ib_logfile0  ibdata1  performance_schema  public_key.pem   server-key.pem
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile1  mysql    private_key.pem     server-cert.pem  sys

启动服务:
/etc/init.d/mysql.server status
chkconfig mysql.server on
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

根哥的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值