原 MySQL安装与卸载(持续更新)
Tags: 原创MySQL安装部署卸载yum安装修改密码Windows安装二进制安装mariadbdbdeployer允许远程登陆
MySQL客户端安装
1 2 3 4 5 | -- 安装MySQL客户端 rpm -Uvh https://repo.mysql.com//mysql80-community-release-el7.rpm yum-config-manager --disable mysql80-community yum-config-manager --enable mysql57-community yum install -y mysql |
yum安装MySQL
1 2 3 4 5 6 | docker run -itd --name lhrc76mysql -h lhrc76mysql \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:9.0 \ /usr/sbin/init |
yum离线安装MySQL
https://dev.mysql.com/downloads/mysql/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar md5sum mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar tar -xvf mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar rpm -ivh mysql-community-*.rpm yum localinstall -y mysql-community-*.rpm systemctl status mysqld systemctl start mysqld 日志文件:/var/log/mysqld.log 数据文件:/var/lib/mysql/ grep 'temporary password' /var/log/mysqld.log mysql -uroot -p ALTER USER root@'localhost' IDENTIFIED BY 'Lhr@xxt123'; |
在线yum安装MySQL
1 2 3 4 5 | -- linux 6.5安装 MySQL 5.1 yum list | grep mysql yum install -y mysql-server mysql mysql-devel rpm -qi mysql-server service mysqld start |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #repo下载 https://dev.mysql.com/downloads/ https://dev.mysql.com/downloads/repo/yum/ http://repo.mysql.com/ http://repo.mysql.com/yum #安装文档 https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/#repo-qg-yum-platform-specifics rpm -Uvh https://repo.mysql.com//mysql80-community-release-el8.rpm ## linux8 rpm -Uvh https://repo.mysql.com//mysql80-community-release-el7.rpm ## linux7 rpm -Uvh https://repo.mysql.com//mysql80-community-release-el6.rpm ## linux6 yum clean all yum makecache rpm --rebuilddb /etc/yum.repos.d/mysql-community.repo yum repolist all | grep mysql yum-config-manager --disable mysql80-community yum-config-manager --enable mysql57-community -- linux 8 #dnf config-manager --disable mysql80-community #dnf config-manager --enable mysql57-community yum repolist enabled | grep mysql linux 8执行 yum module disable mysql yum install -y mysql-community-server #仅仅下载安装包而不进行安装: yum install mysql-community-server --downloadonly --downloaddir=/soft/ systemctl start mysqld #service mysqld start systemctl status mysqld #service mysqld status mysql_secure_installation -- linux 6 chkconfig --list | grep mysqld chkconfig mysqld on rpm -qi mysql-community-server 日志文件:/var/log/mysqld.log 数据文件:/var/lib/mysql/ grep 'temporary password' /var/log/mysqld.log mysql -uroot -p ALTER USER root@'localhost' IDENTIFIED BY 'lhr'; -- 去掉密码验证策略 SHOW VARIABLES LIKE 'validate_password%'; SHOW STATUS LIKE 'validate_password%'; set global validate_password_policy=0; set global validate_password_policy=LOW; set global validate_password_length=1; show plugins; uninstall plugin validate_password; show variables like 'plugin_dir'; mv /usr/lib64/mysql/plugin/validate_password.so /usr/lib64/mysql/plugin/validate_password.so_bk mv /usr/lib64/mysql/plugin/component_validate_password.so /usr/lib64/mysql/plugin/component_validate_password.so_bk -- 若在线yum下载慢,则可以直接使用迅雷下载离线的rpm包比较快,下载后使用如下命令安装: yum localinstall -y mysql-community-*.rpm |
mariadb安装
在centos7上,直接yum install mysql-server
将默认安装mariadb,如果配置了mysql的yum源,需要指定"mysql-community-server"才表示安装mysql。
mariadb的镜像站点:http://mirror.mariadb.org/yum/
1 2 3 4 5 6 7 8 9 10 | cat > /etc/yum.repos.d/mariadb.repo <<"EOF" [mariadb] name=mariadb baseurl=http://mirror.mariadb.org/yum/10.9/rhel/7/x86_64/ enabled=1 gpgcheck=0 EOF yum install -y mariadb-server systemctl start mariadb |
其它:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | vgextend vg_mysqlsoft /dev/sdc7 lvcreate -n lv_MariaDB_104 -L 4G vg_mysqlsoft mkfs.ext4 /dev/vg_mysqlsoft/lv_MariaDB_104 mkdir /usr/local/MariaDB_104 mount /dev/vg_mysqlsoft/lv_MariaDB_104 /usr/local/MariaDB_104 /dev/vg_mysqlsoft/lv_MariaDB_104 /usr/local/MariaDB_104 ext4 defaults 0 0 mariadb-10.4.10-linux-x86_64.tar.gz tar -zxvf mariadb-10.4.10-linux-x86_64.tarsupport-files cd /usr/local ln -s mariadb-10.4.10-linux-x86_64 mysql groupadd mysql useradd -g mysql mysql mkdir /usr/local/MariaDB_104/mariadb-10.4.10-linux-x86_64/data cd mariadb-10.4.10-linux-x86_64 --mysql_secure_installation ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/MariaDB_104/mariadb-10.4.10-linux-x86_64/data chown -R root:mysql . chown -R mysql:mysql data ./bin/mysqld_safe --user=mysql --datadir=/usr/local/MariaDB_104/mariadb-10.4.10-linux-x86_64/data & [mysqld104103320] mysqld=/usr/local/MariaDB_104/mariadb-10.4.10-linux-x86_64/bin/mysqld_safe mysqladmin=/usr/local/MariaDB_104/mariadb-10.4.10-linux-x86_64/bin/mysqladmin basedir=/usr/local/MariaDB_104/mariadb-10.4.10-linux-x86_64 datadir=/usr/local/MariaDB_104/mariadb-10.4.10-linux-x86_64/data socket=/usr/local/MariaDB_104/mariadb-10.4.10-linux-x86_64/data/mysql104103320.sock port=3320 log-bin = server-id = 104103320 mysqld_multi start 104103320 mysqld_multi report ./bin/mysqladmin -u root password 'lhr' ./bin/mysqladmin -u root -h localhost.localdomain password 'lhr' set password=password("lhr"); grant all privileges on *.* to root@'%' identified by 'lhr'; flush privileges; |
二进制方式安装MySQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | mkdir -p /usr/local/mysqlsoft tar -zxvf mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysqlsoft tar -zxvf mysql-5.6.48-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysqlsoft tar -zxvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysqlsoft tar -Jxf mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz -C /usr/local/mysqlsoft tar -Jxf mysql-8.1.0-linux-glibc2.17-x86_64.tar.xz -C /usr/local/mysqlsoft mkdir -p /usr/local/mysql55 /usr/local/mysql56 /usr/local/mysql57 /usr/local/mysql80 /usr/local/mysql81 ln -s /usr/local/mysqlsoft/mysql-5.5.62-linux-glibc2.12-x86_64 /usr/local/mysql55/mysql5562 ln -s /usr/local/mysqlsoft/mysql-5.6.48-linux-glibc2.12-x86_64 /usr/local/mysql56/mysql5648 ln -s /usr/local/mysqlsoft/mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysql57/mysql5730 ln -s /usr/local/mysqlsoft/mysql-8.0.19-linux-glibc2.12-x86_64 /usr/local/mysql80/mysql8019 ln -s /usr/local/mysqlsoft/mysql-8.0.19-linux-glibc2.12-x86_64 /usr/local/mysql80/mysql8019 ln -s /usr/local/mysqlsoft/mysql-8.1.0-linux-glibc2.17-x86_64 /usr/local/mysql81/mysql81 groupadd mysql useradd -r -g mysql mysql chown -R mysql.mysql /usr/local/mysqlsoft/ --- 5.5、5.6 --MySQL 5.5、5.6的二进制安装默认密码为空 /usr/local/mysql55/mysql5562/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql55/mysql5562 --datadir=/usr/local/mysql55/mysql5562/data /usr/local/mysql56/mysql5648/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql56/mysql5648 --datadir=/usr/local/mysql56/mysql5648/data -- MySQL 5.7.6之后的版本初始化数据库不再使用mysql_install_db /usr/local/mysql57/mysql5730/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql57/mysql5730 --datadir=/usr/local/mysql57/mysql5730/data /usr/local/mysql80/mysql8019/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql80/mysql8019 --datadir=/usr/local/mysql80/mysql8019/data mkdir -p /usr/local/mysqlsoft/log/ -- /etc/my.cnf 管理多实例 [mysqld_multi] mysqld=/usr/local/mysql80/mysql8019/bin/mysqld_safe mysqladmin=/usr/local/mysql80/mysql8019/bin/mysqladmin log=/usr/local/mysqlsoft/log/mysqld_multi.log user=root password=lhr #[client] #user=root #host=localhost #password=lhr [mysql] default-character-set=utf8mb4 [mysqld55623321] mysqld=/usr/local/mysql55/mysql5562/bin/mysqld_safe mysqladmin=/usr/local/mysql55/mysql5562/bin/mysqladmin port=3321 basedir=/usr/local/mysql55/mysql5562 datadir=/usr/local/mysql55/mysql5562/data socket=/usr/local/mysql55/mysql5562/data/mysqls55623321.sock server_id=55623321 log-bin [mysqld56483331] mysqld=/usr/local/mysql56/mysql5648/bin/mysqld_safe mysqladmin=/usr/local/mysql56/mysql5648/bin/mysqladmin port=3331 basedir=/usr/local/mysql56/mysql5648 datadir=/usr/local/mysql56/mysql5648/data socket=/usr/local/mysql56/mysql5648/data/mysqls56483331.sock server_id=56483331 log-bin [mysqld57303341] mysqld=/usr/local/mysql57/mysql5730/bin/mysqld_safe mysqladmin=/usr/local/mysql57/mysql5730/bin/mysqladmin port=3341 basedir=/usr/local/mysql57/mysql5730 datadir=/usr/local/mysql57/mysql5730/data socket=/usr/local/mysql57/mysql5730/data/mysqls57303341.sock server_id=57303341 log-bin [mysqld80193350] mysqld=/usr/local/mysql80/mysql8019/bin/mysqld_safe mysqladmin=/usr/local/mysql80/mysql8019/bin/mysqladmin port=3350 basedir=/usr/local/mysql80/mysql8019 datadir=/usr/local/mysql80/mysql8019/data socket=/usr/local/mysql80/mysql8019/data/mysqls80193350.sock default_authentication_plugin=mysql_native_password server_id=80193350 log-bin [mysqld810] mysqld=/usr/local/mysqlsoft/8.1.0/bin/mysqld_safe mysqladmin=/usr/local/mysqlsoft/8.1.0/bin/mysqladmin port=13381 basedir=/usr/local/mysqlsoft/8.1.0 datadir=/usr/local/mysqldata/data810 socket=/tmp/mysqls810.sock default_authentication_plugin=mysql_native_password server_id=801913381 log-bin default-time-zone = '+8:00' log_timestamps = SYSTEM skip-name-resolve character_set_server=utf8mb4 echo "export PATH=$PATH:/usr/local/mysql80/mysql8019/bin" >> /root/.bashrc source /root/.bashrc mysqld_multi report mysqld_multi start -- mysqld_multi不能关闭实例,需要修改221行(vi /usr/bin/mysqld_multi): my $com= join ' ', 'my_print_defaults ', @defaults_options, $group; 替换为: my $com= join ' ', 'my_print_defaults -s', @defaults_options, $group; -- Linux启动MySQL /usr/local/mysql55/mysql5557/bin/mysqld_safe --defaults-file=/usr/local/mysql55/mysql5557/mysql55573310.cnf & /usr/local/mysql55/mysql5557/bin/mysqld_safe --defaults-file=/usr/local/mysql55/mysql5557/mysql55573311.cnf & /usr/local/mysql56/mysql5637/bin/mysqld_safe --defaults-file=/usr/local/mysql56/mysql5637/mysql56373312.cnf & /usr/local/mysql56/mysql5637/bin/mysqld_safe --defaults-file=/usr/local/mysql56/mysql5637/mysql56373313.cnf & /usr/local/mysql57/mysql5719/bin/mysqld_safe --defaults-file=/usr/local/mysql57/mysql5719/mysql57193308.cnf & /usr/local/mysql57/mysql5719/bin/mysqld_safe --defaults-file=/usr/local/mysql57/mysql5719/mysql57193309.cnf & /usr/local/mysql80/mysql8019/bin/mysqld_safe --defaults-file=/usr/local/mysql80/mysql8019/mysql80193314.cnf & /usr/local/mysql80/mysql8019/bin/mysqld_safe --defaults-file=/usr/local/mysql80/mysql8019/mysql80193315.cnf & -- Linux关闭MySQL /usr/local/mysql55/mysql5557/bin/mysqladmin -u root -plhr -S/usr/local/mysql55/mysql5557/data/mysql55573310.sock shutdown /usr/local/mysql55/mysql5557/bin/mysqladmin -u root -plhr -S/usr/local/mysql55/mysql5557/data55573311/mysql55573311.sock shutdown /usr/local/mysql56/mysql5637/bin/mysqladmin -u root -plhr -S/usr/local/mysql56/mysql5637/data/mysql56373312.sock shutdown /usr/local/mysql56/mysql5637/bin/mysqladmin -u root -plhr -S/usr/local/mysql56/mysql5637/data56373313/mysql56373313.sock shutdown /usr/local/mysql57/mysql5719/bin/mysqladmin -u root -plhr -S/usr/local/mysql57/mysql5719/data/mysql57193308.sock shutdown /usr/local/mysql57/mysql5719/bin/mysqladmin -u root -plhr -S/usr/local/mysql57/mysql5719/data57193309/mysql57193309.sock shutdown /usr/local/mysql80/mysql8019/bin/mysqladmin -u root -plhr -S/usr/local/mysql80/mysql8019/data/mysql80193314.sock shutdown /usr/local/mysql80/mysql8019/bin/mysqladmin -u root -plhr -S/usr/local/mysql80/mysql8019/data80193315/mysql80193315.sock shutdown |
多实例配置
1 2 3 4 5 | 启动全部实例:mysqld_multi start 查看全部实例状态:mysqld_multi report 启动单个实例:mysqld_multi start 3306 停止单个实例:mysqld_multi stop 3306 查看单个实例状态:mysqld_multi report 3306 |
配置服务service开机启动
如果MySQL安装在/usr/local/mysql下,datadir为/usr/local/mysql/data,参数文件为/etc/my.cnf,/usr/bin/my_print_defaults存在,则可以直接使用$MYSQL_HOME/support-files/mysql.server文件;如果安装在其它路线下,那么需要做其他的额外设置,比较麻烦:
1 2 3 4 5 6 7 | cp ./support-files/mysql.server /etc/init.d/mysql57193307 chmod 755 /etc/init.d/mysql57193307 chkconfig --add mysql57193307 chkconfig mysql57193307 on chkconfig --level 345 mysql57193307 on service mysql57193307 start systemctl status mysql57193307 |
service
其它情况可以使用以下脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | cat > /etc/init.d/mysql57193307 <<"EOF" #!/bin/sh mysqld This shell script takes care of starting and stopping the MySQL subsystem (mysqld). # chkconfig: 345 64 36 Source function library. . /etc/rc.d/init.d/functions Source networking configuration. . /etc/sysconfig/network basedir=/usr/local/mysql57/mysql5719 mysql_sid=57193307 datadir="$basedir/data" exec="$basedir/bin/mysqld_safe" prog="mysqld_$mysql_sid" socketfile="$datadir/mysqld_$mysql_sid.sock" errlogfile="$datadir/mysqld_$mysql_sid.log" mypidfile="$datadir/mysqld_$mysql_sid.pid" cnf="$datadir/my_$mysql_sid.cnf" Set timeouts here so they can be overridden from /etc/sysconfig/mysqld STARTTIMEOUT=120 STOPTIMEOUT=60 Set in /etc/sysconfig/mysqld, will be passed to mysqld_safe MYSQLD_OPTS= [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog lockfile=/var/lock/subsys/$prog case $socketfile in /*) adminsocket="$socketfile" ;; *) adminsocket="$datadir/$socketfile" ;; esac start(){ [ -x $exec ] || exit 5 ## check to see if it's already running RESPONSE=$($basedir/bin/mysqladmin --no-defaults --socket="$adminsocket" --user=mysql ping 2>&1) if [ $? = 0 ]; then ## already running, do nothing action $"Starting $prog: " /bin/true ret=0 elif echo "$RESPONSE" | grep -q "Access denied for user" then ## already running, do nothing action $"Starting $prog: " /bin/true ret=0 else ## Now start service $exec $MYSQLD_OPTS --defaults-file="$cnf" --datadir="$datadir" --socket="$socketfile" \ --pid-file="$mypidfile" \ --basedir="$basedir" --user=mysql >/dev/null & safe_pid=$! ## Spin for a maximum of N seconds waiting for the server to come up; ## exit the loop immediately if mysqld_safe process disappears. ## Rather than assuming we know a valid username, accept an "access ## denied" response as meaning the server is functioning. ret=0 TIMEOUT="$STARTTIMEOUT" while [ $TIMEOUT -gt 0 ]; do RESPONSE=$($basedir/bin/mysqladmin --no-defaults --socket="$adminsocket" --user=mysql ping 2>&1) && break echo "$RESPONSE" | grep -q "Access denied for user" && break if ! /bin/kill -0 $safe_pid 2>/dev/null; then echo "MySQL Daemon failed to start." ret=1 break fi sleep 1 let TIMEOUT=${TIMEOUT}-1 done if [ $TIMEOUT -eq 0 ]; then echo "Timeout error occurred trying to start MySQL Daemon." ret=1 fi if [ $ret -eq 0 ]; then action $"Starting $prog: " /bin/true touch $lockfile else action $"Starting $prog: " /bin/false fi fi return $ret } stop(){ if [ ! -f "$mypidfile" ]; then ## not running; per LSB standards this is "ok" action $"Stopping $prog: " /bin/true return 0 fi MYSQLPID=`cat "$mypidfile"` if [ -n "$MYSQLPID" ]; then /bin/kill "$MYSQLPID" >/dev/null 2>&1 ret=$? if [ $ret -eq 0 ]; then TIMEOUT="$STOPTIMEOUT" while [ $TIMEOUT -gt 0 ]; do /bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break sleep 1 let TIMEOUT=${TIMEOUT}-1 done if [ $TIMEOUT -eq 0 ]; then echo "Timeout error occurred trying to stop MySQL Daemon." ret=1 action $"Stopping $prog: " /bin/false else rm -f $lockfile rm -f "$socketfile" action $"Stopping $prog: " /bin/true fi else action $"Stopping $prog: " /bin/false fi else ## failed to read pidfile, probably insufficient permissions action $"Stopping $prog: " /bin/false ret=4 fi return $ret } restart(){ stop start } condrestart(){ [ -e $lockfile ] && restart || : } See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p "$mypidfile" $prog ;; restart) restart ;; condrestart|try-restart) condrestart ;; reload) exit 3 ;; force-reload) restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $? EOF |
修改basedir和port,然后编辑参数文件my_57193307.cnf
1 2 3 4 5 6 7 8 9 10 | basedir=/usr/local/mysql57/mysql5719 mysql_sid=57193307 cat <<EOF > /usr/local/mysql57/mysql5719/data/my_57193307.cnf [mysqld] port=57193307 EOF chown mysql.mysql /usr/local/mysql57/mysql5719/data/my_57193307.cnf chmod +x /etc/init.d/mysql57193307 |