一键安装MySQL5.7脚本,一劳永逸
MySQL5.0脚本
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
| #!/bin/sh # 提示:本脚本只支持centos 7 安装MySQL版本5.7 # 安装完成后脚本会打印出MySQL的root用户的初始登录密码 # mysql -u root -p # 输入初始密码即可登录
#wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.36-el7-x86_64.tar.gz REMOVE=`rpm -qa | grep -i mariadb-libs` #卸载系统预置的mariadb yum remove $REMOVE -y #安装依赖库 yum install libaio -y yum install libncurses* -y yum install wget -y #下载 wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.36-el7-x86_64.tar.gz tar -zxvf mysql-5.7.36-el7-x86_64.tar.gz mv mysql-5.7.36-el7-x86_64 mysql mv mysql /usr/local/ useradd -s/sbin/nlogin -M mysql id mysql mkdir /usr/local/mysql/{data,log} chown -R mysql.mysql /usr/local/mysql/ #编辑my.cnf cat << EOF > /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock [mysqld] server_id=10 port = 3306 user = mysql character-set-server = utf8mb4 default_storage_engine = innodb log_timestamps = SYSTEM socket = /tmp/mysql.sock basedir = /usr/local/mysql datadir = /usr/local/mysql/data/ pid-file = /usr/local/mysql/data/mysql.pid max_connections = 1000 max_connect_errors = 1000 table_open_cache = 1024 max_allowed_packet = 128M open_files_limit = 65535 log-bin=mysql-bin # innodb_buffer_pool_size = 1024M innodb_file_per_table = 1 innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_purge_threads = 2 innodb_flush_log_at_trx_commit = 1 innodb_log_file_size = 512M innodb_log_files_in_group = 2 innodb_log_buffer_size = 16M innodb_max_dirty_pages_pct = 80 innodb_lock_wait_timeout = 30 innodb_data_file_path=ibdata1:1024M:autoextend # log_error = /usr/local/mysql/log/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /usr/local/mysql/log/mysql-slow.log sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES EOF #编译 /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp #授权 chown -R mysql:mysql /usr/local/mysql cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql chkconfig --add mysqld chkconfig --list cp /usr/local/mysql/bin/* /usr/local/sbin/ cd /lib/systemd/system # /etc/init.d/mysql start netstat -lntup|grep mysql grep "password" /usr/local/mysql/log/mysql-error.log
|
MySQL8.0脚本
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 177 178 179 180 181
| #!/bin/sh
# 使用 # 去到https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads # 选中合适的mysql版本并修改脚本中的链接
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
Install_MySQL() { # CentOS 8 mysql: error while loading shared libraries: libncurses.so.5 yum install ncurses ncurses-compat-libs -y # Install dependencies setenforce 0 yum -y install epel-release yum -y install wget tar jemalloc jemalloc-devel gcc gcc-c++
# MySQL configuration # 此处应当进行适当修改 mysql_version="8.0.26" mysql_password="123456" mkdir -p /apps/server/mysql/data mysql_install_dir="/usr/local/mysql" mysql_data_dir="/usr/local/mysql/data" download_mysql="https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads"
# Download MySQL cd /mnt useradd -M -s /sbin/nologin mysql >/dev/null 2>&1 wget -c ${download_mysql}/MySQL-8.0/mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.xz --no-check-certificate echo "========== Start installing MySQL ============" tar xJf mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.xz mkdir ${mysql_install_dir} mv mysql-${mysql_version}-linux-glibc2.12-x86_64/* ${mysql_install_dir}/ if [[ -d "${mysql_install_dir}/support-files" ]]; then rm -rf mysql-${mysql_version}-linux-glibc2.12-x86_64 else rm -rf ${mysql_install_dir} echo "Error: MySQL install failed, Please contact the author" kill -9 $$ fi
# Initialize the database ${mysql_install_dir}/bin/mysqld --initialize-insecure --user=mysql --basedir=${mysql_install_dir} --datadir=${mysql_data_dir} chown -R root . ${mysql_install_dir} chown -R mysql.mysql ${mysql_data_dir} cp -f ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld sed -i "s@^basedir=.*@basedir=${mysql_install_dir}@" /etc/init.d/mysqld sed -i "s@^datadir=.*@datadir=${mysql_data_dir}@" /etc/init.d/mysqld sed -i "s@/usr/local/mysql@${mysql_install_dir}@g" ${mysql_install_dir}/bin/mysqld_safe sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/lib64/libjemalloc.so@' ${mysql_install_dir}/bin/mysqld_safe #删除配置文件中原有的环境变量 sed -ie '/MYSQL_HOME/d' /etc/profile #修改mysql的环境变量,直接写入配置文件 echo "export MYSQL_HOME=${mysql_install_dir}" >>/etc/profile echo "export PATH=\$PATH:\$MYSQL_HOME/bin" >>/etc/profile source /etc/profile # my.conf configuration cat > /etc/my.cnf << EOF [client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8mb4
[mysql] prompt="MySQL [\\d]> " no-auto-rehash
[mysqld] port = 3306 socket = /tmp/mysql.sock default_authentication_plugin = mysql_native_password
basedir = ${mysql_install_dir} datadir = ${mysql_data_dir} pid-file = ${mysql_data_dir}/mysql.pid user = mysql bind-address = 0.0.0.0 server-id = 1
init-connect = 'SET NAMES utf8mb4' character-set-server = utf8mb4 collation-server = utf8mb4_0900_ai_ci
skip-name-resolve #skip-networking back_log = 300
max_connections = 1000 max_connect_errors = 6000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 500M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M
read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 8M key_buffer_size = 4M
thread_cache_size = 8
ft_min_word_len = 4
log_bin = mysql-bin binlog_format = mixed binlog_expire_logs_seconds = 604800
log_error = ${mysql_data_dir}/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = ${mysql_data_dir}/mysql-slow.log
performance_schema = 0 explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB #default-storage-engine = MyISAM innodb_file_per_table = 1 innodb_open_files = 500 innodb_buffer_pool_size = 64M innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 0 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 10G myisam_repair_threads = 1
interactive_timeout = 28800 wait_timeout = 28800
[mysqldump] quick max_allowed_packet = 500M
[myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M EOF # Set up MySQL chmod 600 /etc/my.cnf chmod +x /etc/init.d/mysqld cp /etc/init.d/mysqld /usr/bin systemctl enable mysqld systemctl start mysqld ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "create user root@'127.0.0.1' identified by \"${mysql_password}\";" ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "grant all privileges on *.* to root@'127.0.0.1' with grant option;" ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "grant all privileges on *.* to root@'localhost' with grant option;" ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "alter user root@'localhost' identified by \"${mysql_password}\";" ${mysql_install_dir}/bin/mysql -uroot -p${mysql_password} -e "reset master;" rm -rf /etc/ld.so.conf.d/{mysql,mariadb,percona,alisql}*.conf echo "${mysql_install_dir}/lib" > /etc/ld.so.conf.d/mysql_renwole.com.conf ldconfig echo "========== MySQL installing Successfully =====" echo "MySQL:" echo "account: root" echo "password: ${mysql_password}" echo "database: ${mysql_data_dir}" echo "==============================================" }
Install_MySQL source /etc/profile
|
登录MySQL
脚本执行后最后一行输出一下内容
1
| [Note] A temporary password is generated for root@localhost: 2t<q#r;>2EBM
|
输入以下命令
将2t<q#r;>2EBM输入即可登陆,注意输入时密码不回显
登陆成功后修改密码
1
| ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
|
123456就是root用户的登陆密码
允许远程连接
1 2
| grant all on *.* to admin@'%' identified by '123456' with grant option; flush privileges;
|
1 2 3
| use mysql; update user set host='%' where user='root' and host='localhost'; flush privileges;
|
1 2
| use mysql; select host,user from user;
|