MySQL安装与卸载
Tags: dbdeployermariadbMySQLWindows安装yum安装二进制安装修改密码允许远程登陆卸载安装部署
MySQL客户端安装
1 2 3 4 5 6 | -- 安装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 | 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 mkdir -p /usr/local/mysql55 /usr/local/mysql56 /usr/local/mysql57 /usr/local/mysql80 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 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 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 |
其它情况可以使用以下脚本:
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 |
-- 使用下面的命令管理两个实例:
1 2 | service mysql57193307 {start|stop|status|restart} service mysql80193309 {start|stop|status|restart} |
------------------- CentOS 7
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 | cat << EOF > /usr/lib/systemd/system/mysqld57303341.service [Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql Type=forking Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0 Execute pre and post scripts as root PermissionsStartOnly=true Start main service ExecStart=/usr/local/mysql57/mysql5730/bin/mysqld --defaults-file=/usr/local/mysql57/mysql5730/data/my_57303341.cnf --pid-file=/usr/local/mysql57/mysql5730/data/mysqld57303341.pid --daemonize $MYSQLD_OPTS Use this to switch malloc implementation EnvironmentFile=-/etc/sysconfig/mysql Sets open_files_limit LimitNOFILE = 5000 Restart=on-failure RestartPreventExitStatus=1 PrivateTmp=false EOF cat <<EOF > /usr/local/mysql57/mysql5730/data/my_57303341.cnf [mysqld] basedir=/usr/local/mysql57/mysql5730 datadir=/usr/local/mysql57/mysql5730/data socket=/usr/local/mysql57/mysql5730/data/mysqls57303341.sock port=3341 server_id=57303341 log-bin EOF |
1 2 3 4 5 6 | chmod mysql.mysql /usr/local/mysql57/mysql5730/data/my_57303341.cnf systemctl daemon-reload systemctl {start|stop|status|restart} mysqld80193350 systemctl {start|stop|status|restart} mysqld80193351 |
编译安装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 | wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.30.tar.gz tar -zxf /soft/mysql-boost-5.7.30.tar.gz yum install -y bzip2 wget glibc-headers bison* ncurses* gcc gcc-c++ libaio cmake openssl* cd /soft/mysql-5.7.30 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/mysql5730 \ -DMYSQL_DATADIR=/usr/local/mysql/mysql5730/data \ -DSYSCONFDIR=/etc \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DENABLE_DTRACE=0 \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_general_ci \ -DWITH_EMBEDDED_SERVER=1 \ -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=/soft/mysql-5.7.30/boost/boost_1_59_0 #make -j`cat /proc/cpuinfo|grep "processor"| wc -l` make -j8 && make install groupadd mysql useradd -g mysql mysql chown -R mysql:mysql /usr/local/mysql/mysql5730 ----------/etc/my.cnf [mysqld] basedir=/usr/local/mysql/mysql5730 datadir=/usr/local/mysql/mysql5730/data socket=/usr/local/mysql/mysql5730/mysqls.sock /usr/local/mysql/mysql5730/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql/mysql5730 --datadir=/usr/local/mysql/mysql5730/data --user=mysql cp /usr/local/mysql/mysql5730/support-files/mysql.server /etc/init.d/mysql service mysql start echo 'export PATH=/usr/local/mysql/mysql5730/bin:$PATH' > ~/.bash_profile source ~/.bash_profile mysql -uroot -p -S/usr/local/mysql/mysql5730/mysqls.sock grant all privileges on *.* to root@'localhost' identified by 'lhr' with grant option; grant all privileges on *.* to root@'%' identified by 'lhr' with grant option; flush privileges; select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user; |
最后可以删除源码部分:rm -rf /soft/mysql-5.7.30
注:重新运行配置,需要删除CMakeCache.txt文件
- -DCMAKE_INSTALL_PREFIX=dir_name 设置mysql安装目录
- -DMYSQL_UNIX_ADDR=file_name 设置监听套接字路径,这必须是一个绝对路径名。默认为/tmp/mysql.sock
- -DDEFAULT_CHARSET=charset_name 设置服务器的字符集。缺省情况下,MySQL使用latin1的(CP1252西欧)字符集。cmake/character_sets.cmake文件包含允许的字符集名称列表。
- -DDEFAULT_COLLATION=collation_name 设置服务器的排序规则。
- -DWITH_INNOBASE_STORAGE_ENGINE=1
- -DWITH_ARCHIVE_STORAGE_ENGINE=1
- -DWITH_BLACKHOLE_STORAGE_ENGINE=1
- -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 存储引擎选项:MyISAM,MERGE,MEMORY和CSV引擎是默认编译到服务器中,并不需要明确地安装。静态编译一个存储引擎到服务器,使用-DWITH_engine_STORAGE_ENGINE= 1,可用的存储引擎值有:ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE (InnoDB), PARTITION (partitioning support), 和PERFSCHEMA (Performance Schema)
- -DMYSQL_DATADIR=dir_name 设置mysql数据库文件目录
- -DMYSQL_TCP_PORT=port_num 设置mysql服务器监听端口,默认为3306
- -DENABLE_DOWNLOADS=bool 是否要下载可选的文件。例如,启用此选项(设置为1),cmake将下载谷歌所使用的测试套件运行单元测试。
Windows下安装MySQL
- 使用管理员权限,已在windows 10、Windows7、XP上测试通过,XP不支持5.7及其以上版本
- 创建服务时一定需要全路径
- 5.5、5.6、5.7、8.0 默认密码都为空,8.0数据库需要8.0的客户端才能无密码登录
- 若报错“由于找不到MSVCP120.dll,无法继续执行代码.重新安装程序可能会解决此问题。” ---解决:下载安装“微软常用运行库64位”即可。
- 多版本多实例安装:重复以下步骤,修改datadir和参数文件即可
1 2 3 4 5 6 7 8 9 10 11 12 13 | D:\mysql-5.7.30-winx64\bin\mysqld --initialize-insecure --user=mysql --console --basedir=D:\mysql-5.7.30-winx64 --datadir=D:\mysql-5.7.30-winx64\data57303307 D:\mysql-5.7.30-winx64\bin\mysqld install mysql57303307 --defaults-file="D:\mysql-5.7.30-winx64\data57303307\mysql57303307.ini" [mysqld] basedir = D:\mysql-5.7.30-winx64 datadir = D:\mysql-5.7.30-winx64\data57303307 server_id=57303307 port = 3307 net start mysql57303307 setx PATH "D:\mysql-5.7.30-winx64\bin" mysql -uroot -p -P3307 |
可以在服务(services.msc)和任务管理器中进行管理MySQL实例。
1 | tasklist | findstr mysql |
sc命令操作MySQL
1 2 3 4 5 6 7 | sc create mysql57303307 binpath= "D:\mysql-5.7.30-winx64\bin\mysqld --defaults-file=D:\mysql-5.7.30-winx64\data57303307\mysql57303307.ini mysql57303307" start= auto displayname= "mysql57303307" sc delete mysql57303307 sc start mysql57303307 sc stop mysql57303307 sc query mysql57303307 sc qc mysql57303307 |
sandbox(已过期)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | yum install cpan perl-Test-Simple -y cpan MySQL::Sandbox echo 'export SANDBOX_AS_ROOT=1' >> /root/.bash_profile source /root/.bash_profile --单实例 make_sandbox mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz mysql -S/tmp/mysql_sandbox5562.sock -uroot -pmsandbox --多实例 make_multiple_sandbox --how_many_nodes=2 mysql-5.6.48-linux-glibc2.12-x86_64.tar.gz $SANDBOX_HOME/multi_msb_mysql-5_6_48/n1 --主从复制 make_replication_sandbox mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz make_replication_sandbox --gtid --how_many_slaves=1 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz $SANDBOX_HOME/rsandbox_mysql-5_7_30/test_replication ---不同版本 export SANDBOX_BINARY=/soft ls $SANDBOX_BINARY make_sandbox 5.5.62 make_multiple_custom_sandbox 5.5.62 5.7.30 5.6.48 |
dbdeployer
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 | VERSION=1.50.0 OS=linux origin=https://github.com/datacharmer/dbdeployer/releases/download/v$VERSION wget $origin/dbdeployer-$VERSION.$OS.tar.gz tar -xzf dbdeployer-$VERSION.$OS.tar.gz chmod +x dbdeployer-$VERSION.$OS mv dbdeployer-$VERSION.$OS /usr/local/bin/dbdeployer dbdeployer unpack mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz dbdeployer unpack mysql-5.6.48-linux-glibc2.12-x86_64.tar.gz dbdeployer unpack mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz dbdeployer unpack mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz mkdir -p /root/opt/mysql ln -s /usr/local/mysqlsoft/mysql-5.5.62-linux-glibc2.12-x86_64 /root/opt/mysql/5.5.62 ln -s /usr/local/mysqlsoft/mysql-5.6.48-linux-glibc2.12-x86_64 /root/opt/mysql/5.6.48 ln -s /usr/local/mysqlsoft/mysql-5.7.30-linux-glibc2.12-x86_64 /root/opt/mysql/5.7.30 ln -s /usr/local/mysqlsoft/mysql-8.0.19-linux-glibc2.12-x86_64 /root/opt/mysql/8.0.19 ll /root/opt/mysql/ dbdeployer remote list dbdeployer downloads list dbdeployer sandboxes --full-info dbdeployer global status dbdeployer global stop msb_8_0_19 dbdeployer delete msb_8_0_19 默认密码:msandbox --- 单机 dbdeployer deploy single 8.0.19 --bind-address=0.0.0.0 --port=3351 --remote-access='%' --native-auth-plugin --gtid --my-cnf-options="skip_name_resolve" --pre-grants-sql="create user root@'%' identified with mysql_native_password by 'lhr';grant all on *.* to root@'%' with grant option;flush privileges;" dbdeployer deploy single 5.7.30 --bind-address=0.0.0.0 --port=3343 --remote-access='%' --gtid --my-cnf-options="skip_name_resolve" --my-cnf-options="character_set_server=utf8mb4" --pre-grants-sql="grant all on *.* to root@'%' identified by 'lhr' with grant option;flush privileges;" dbdeployer deploy single 5.7.30 --bind-address=0.0.0.0 --port=3343 --remote-access='%' --sandbox-binary /usr/local/mysqlsoft --gtid --my-cnf-options="skip_name_resolve" --my-cnf-options="character_set_server=utf8mb4" --pre-grants-sql="grant all on *.* to root@'%' identified by 'lhr' with grant option;flush privileges;" mysql -uroot -pmsandbox -S/tmp/mysql_sandbox3343.sock firewall-cmd --add-port=3300-3500/tcp --permanent firewall-cmd --reload firewall-cmd --list-port --部署2个mysql(using ports 3335,3336) dbdeployer deploy multiple 5.6.48 --bind-address=0.0.0.0 --base-port=3334 --nodes=2 --remote-access='%' --my-cnf-options="skip_name_resolve" --my-cnf-options="character_set_server=utf8mb4" --pre-grants-sql="grant all on *.* to root@'%' identified by 'lhr' with grant option;flush privileges;" -- 主从复制 --部署1主2从: (using ports 3344,3345,3346) dbdeployer deploy replication 5.7.30 --bind-address=0.0.0.0 --base-port=3343 --nodes=3 --gtid --remote-access='%' --my-cnf-options="skip_name_resolve" --my-cnf-options="character_set_server=utf8mb4" --pre-grants-sql="grant all on *.* to root@'%' identified by 'lhr' with grant option;flush privileges;" --安装3主2从(多主多从):(using ports 3347,3348,3349,3350,3351) dbdeployer deploy replication 5.7.30 --topology=fan-in --nodes=5 --master-list="1,2,3" --slave-list="4,5" --base-port=3346 --bind-address=0.0.0.0 --gtid --remote-access='%' --my-cnf-options="skip_name_resolve" --my-cnf-options="character_set_server=utf8mb4" --pre-grants-sql="grant all on *.* to root@'%' identified by 'lhr' with grant option;flush privileges;" --安装组复制:(using ports 3352,3353,3354) -- 多主 dbdeployer deploy replication 8.0.19 --topology=group --bind-address=0.0.0.0 --base-port=3351 --native-auth-plugin --remote-access='%' --gtid --my-cnf-options="skip_name_resolve" --pre-grants-sql="create user root@'%' identified with mysql_native_password by 'lhr';grant all on *.* to root@'%' with grant option;flush privileges;" -- 单主 dbdeployer deploy replication 8.0.19 --topology=group --single-primary --bind-address=0.0.0.0 --base-port=3354 --native-auth-plugin --remote-access='%' --gtid --my-cnf-options="skip_name_resolve" --pre-grants-sql="create user root@'%' identified with mysql_native_password by 'lhr';grant all on *.* to root@'%' with grant option;flush privileges;" --函数切换:从MySQL 8.0.13开始,可以使用函数进行在线修改MGR模式。 select group_replication_switch_to_multi_primary_mode(); #单主切多主 select group_replication_switch_to_single_primary_mode('00015802-3333-3333-3333-333333333333') ; #多主切单主 |
Ubuntu 安装MySQL
1 | apt-get install mysql-server |
卸载MySQL
linux下卸载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | yum remove -y mysql --nodeps yum remove -y mysql-community-* --nodeps rpm -qa | grep mysql rpm -e mysql-community-embedded-devel-5.7.30-1.el7.x86_64 --nodeps rpm -e mysql-community-libs-5.7.30-1.el7.x86_64 --nodeps rpm -e mysql-community-embedded-5.7.30-1.el7.x86_64 --nodeps rpm -e mysql-community-embedded-compat-5.7.30-1.el7.x86_64 --nodeps rpm -e mysql-community-common-5.7.30-1.el7.x86_64 --nodeps rpm -e mysql-community-devel-5.7.30-1.el7.x86_64 --nodeps rpm -e mysql-community-libs-compat-5.7.30-1.el7.x86_64 --nodeps rm -rf /var/lib/mysql rm -rf /var/log/mysqld.log rm -rf /usr/lib/mysql rm -rf /usr/include/mysql rm -rf /etc/my.cnf rm -rf /run/lock/subsys/mysql find / -iname mysql |
Windows下卸载
1 2 3 4 5 6 7 8 9 10 11 12 13 | ① 停止MySQL服务:可以用services.msc,也可以用任务管理器,也可以用“net stop mysql57303307” ② 删除服务:管理员命令行执行“sc delete mysql57303307”或“mysqld remove mysql57303307” ③ 删除所有MySQL文件 ④ regedit打开注册表,删除以下位置的3个文件夹: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application\MySQL HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Services\Eventlog\Application\MySQL HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\MySQL -- Windows下重新注册 1、删除服务:管理员命令行执行“sc delete mysql57303307”或“mysqld remove mysql57303307” 2、重新注册:D:\mysql-5.7.30-winx64\bin\mysqld install mysql57303307 --defaults-file="D:\mysql-5.7.30-winx64\data57303307\mysql57303307.ini" 或: sc create mysql57303307 binpath= "D:\mysql-5.7.30-winx64\bin\mysqld --defaults-file=D:\mysql-5.7.30-winx64\data57303307\mysql57303307.ini mysql57303307" start= auto displayname= "mysql57303307" |
MySQL修改密码和允许远程登陆
MySQL 5.5、5.6、5.7
1 2 3 4 5 6 7 8 9 10 11 12 | -- update mysql.user set grant_priv='Y',super_priv='Y' where user='root'; -- update mysql.user set password=password('lhr') where user='root'; -- 5.5~5.6 -- update mysql.user set authentication_string=password('lhr') where user='root'; -- >= 5.7 -- set password=password('lhr'); -- set password for root@'%'=password('lhr'); -- <= 5.7 grant all on *.* to root@'localhost' identified by 'lhr' with grant option; grant all on *.* to root@'%' identified by 'lhr' with grant option; flush privileges; select user,host,grant_priv,super_priv,password from mysql.user; -- 5.5~5.6 select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user; -- >= 5.7 |
MySQL 8.0
1 2 3 4 5 6 7 8 9 10 11 12 | alter user root@'localhost' identified with mysql_native_password by 'lhr'; grant all on *.* to root@'localhost' with grant option; create user root@'%' identified with mysql_native_password by 'lhr'; grant all on *.* to root@'%' with grant option; flush privileges; select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user; -- mysql 8.0远程连接,在参数文件的[mysqld]下添加: default_authentication_plugin=mysql_native_password -- 不要使用caching_sha2_password |
其它
1 2 3 4 | show privileges; rename user 'lhr'@'localhost' TO 'lhr'@'127.0.0.1'; |