ubuntu上用源码进行一键安装mysql

 

 

首先卸载原有的mysql:

首先查看自己的mysql有哪些依赖

#dpkg --list|grep mysql

先卸载

#sudo apt-get remove mysql-common

#sudo apt-get autoremove --purge mysql-server-5.0 

再用dpkg --list|grep mysql查看,还剩什么就卸载什么

#dpkg --list|grep mysql

最后清楚残留数据:

#dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

#---------------------------------------------------------------------------------------------------------->>

接下来以root身份安装mysql,同时,需要下载带有boost的mysql源码,

 

 

 选择带有boost的源码包

 

 

 将源码包下载到/usr/local/src/之后,进入/usr/local/src/,然后将下面的脚本mysql.sh也放在/usr/local/src/目录下面,然后执行即可:

# sh mysql.sh

 

 

 ########################################################

########################################################

脚本内容如下:mysql.sh

 

  1 #!/bin/bash
  2 #su root
  3 set -x
  4 cd /usr/local/src
  5 ####################
  6 ###################设置使用mysql服务的用户和组
  7 user_name="mysql"
  8 user_group="mysql"
  9 #######################################
 10 #######################################设置你要安装的mysql版本 ,即你下载的文件
 11 mysqlboost_name="mysql-boost-5.7.22"
 12 mysql_name="mysql-5.7.22"
 13 ######################################
 14 ######################################设置mysql的配置文件
 15 mycnf_file="/etc/my.cnf"
 16 
 17 basedir="/usr/local/mysql"
 18 datadir="/usr/local/mysql/data"
 19 socket="/usr/local/mysql/mysql.sock"
 20 db_file="/usr/local/mysql/data/db_file"
 21 tmpdir="/usr/local/mysql/data/tmpdir"
 22 innodb_ts="/usr/local/mysql/data/innodb_ts"
 23 innodb_log="/usr/local/mysql/data/innodb_log"
 24 bin_log="/usr/local/mysql/data/bin_log"
 25 undo_log="/usr/local/mysql/data/undo_log"
 26 relay_log="/usr/local/mysql/data/relay_log"
 27 error_log="/usr/local/mysql/error_log"
 28 
 29 apt-get update
 30 apt-get -y install make cmake gcc gcc-c++ wget ncurses ncurses-devel openssl openssl-devel libaio libaio-devel bison bison-devel perl perl-devel
 31 
 32 groupadd ${user_group}
 33 useradd  -g ${user_group} ${user_name}
 34 
 35 mkdir -p ${basedir}
 36 mkdir -p ${socket}
 37 mkdir -p ${datadir}
 38 mkdir -p ${boostdir}
 39 mkdir -p ${db_file}
 40 mkdir -p ${tmpdir}
 41 mkdir -p ${innodb_ts}
 42 mkdir -p ${innodb_log}
 43 mkdir -p ${bin_log}
 44 mkdir -p ${undo_log}
 45 mkdir -p ${relay_log}
 46 mkdir -P ${error_log}
 47 touch ${error_log}/error.log
 48 chown -R ${user_name}:${user_group} ${basedir}
 49 
 50 tar -pxzvf ${mysqlboost_name}.tar.gz
 51 cd ${mysql_name}
 52 cmake . -DCMAKE_INSTALL_PREFIX=${basedir} -DMYSQL_DATADIR=${db_file} -DSYSCONFDIR=/etc   -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_EMBEDDED_SERVER=OFF  -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_DEBUG=0 -DWITH_EMBEDDED_SERVER=1 
 53 make && make install
 54 ######################################
 55 ######################################配置my.cnf文件
 56 if [ -f "${mycnf_file}" ];then
 57 echo "exists ${mycnf_file}"
 58 rm -rf ${mycnf_file}
 59 else
 60 touch ${mycnf_file}
 61 chmod 755 ${mycnf_file}
 62 fi
 63 # client
 64 echo "[client]" > ${mycnf_file}
 65 echo "port=3306" >> ${mycnf_file}
 66 echo "default-character-set=utf8" >> ${mycnf_file}
 67 echo "socket=/tmp/mysql.sock" >> ${mycnf_file}
 68 
 69 # server
 70 echo "[mysqld]" >> ${mycnf_file}
 71 ##########################################初次安装,使用skip-grant-tables来跳过授权,当修改好密码后请自行去掉该项
 72 echo "skip-grant-tables" >> ${mycnf_file}
 73 echo "basedir=${basedir}" >> ${mycnf_file}
 74 echo "port=3306" >> ${mycnf_file}
 75 echo "datadir=${db_file}" >> ${mycnf_file}
 76 echo "socket=/tmp/mysql.sock" >> ${mycnf_file}
 77 echo "max_connections=2000" >> ${mycnf_file}
 78 echo "default-storage-engine=INNODB" >> ${mycnf_file}
 79 echo "max_allowed_packet=16M" >> ${mycnf_file}
 80 echo "innodb_buffer_pool_size=128M" >> ${mycnf_file}
 81 echo "explicit_defaults_for_timestamp=true" >> ${mycnf_file}
 82 echo "[mysqld_safe]" >> ${mycnf_file}
 83 echo "log_error=${error_log}/error.log" >> ${mycnf_file}
 84 echo "sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" >> ${mycnf_file}
 85 ##############################################
 86 ##############################################拷贝mysql服务端的启动脚本
 87 if [ -f "/etc/init.d/mysql-server" ];then
 88 echo "/etc/init.d/目录中已经存在mysql-server文件,马上进行先删除,再拷贝"
 89 rm -rf /etc/init.d/mysql-server
 90 cp ${basedir}/support-files/mysql.server  /etc/init.d/mysql-server
 91 else
 92 cp ${basedir}/support-files/mysql.server  /etc/init.d/mysql-server
 93 fi
 94 #############################################
 95 ############################################修改环境变量配置
 96 cat /etc/profile|grep ${basedir}/bin
 97 if [ $? == 0 ];then
 98 echo "PATH 已经存在该路径"
 99 else
100 echo "export PATH=${basedir}/bin:${PATH}" >> /etc/profile
101 fi
102 source /etc/profile
103 chown -R ${user_name}:${user_group} ${basedir}
104 #################################
105 #################################通过配置文件来初始化服务器
106 ${basedir}/bin/mysqld  --defaults-file=/etc/my.cnf --initialize --basedir=${basedir} --datadir=${db_file} --user=mysql
107 
108 ################################
109 ################################启动服务器
110 service mysql-server restart
111 ps -ef |grep mysql
112 ##############################
113 ##############################配置开机自启动

 

 

 

 

 接着需要配置/etc/my.cnf文件,配置如下:

[client]
port=3306
default-character-set=utf8
socket=/tmp/mysql.sock
[mysqld]
#skip-grant-tables
basedir=/usr/local/mysql
port=3306
datadir=/usr/local/mysql/data/db_file
socket=/tmp/mysql.sock
max_connections=2000
default-storage-engine=INNODB
max_allowed_packet=16M
innodb_buffer_pool_size=128M
explicit_defaults_for_timestamp=true
[mysqld_safe]
log_error=/usr/local/mysql/data/error.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

 

然后到/usr/local/mysql/bin目录下执行初始化操作:

 

1  ./mysqld --defaults-file=/etc/my.cnf

 结果如下:

1 root@mi-OptiPlex-7060:/usr/local/mysql/bin# ./mysqld --defaults-file=/etc/my.cnf  --initialize --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/db_file/  --user=mysql
2 2019-09-02T06:37:43.153186Z 0 [Warning] InnoDB: New log files created, LSN=45790
3 2019-09-02T06:37:43.485746Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
4 2019-09-02T06:37:43.672726Z 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: 2b277af4-cd4c-11e9-b986-8cec4bcad6ac.
5 2019-09-02T06:37:43.720865Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
6 2019-09-02T06:37:43.722060Z 1 [Note] A temporary password is generated for root@localhost: 1SjpD:qZ0VrN

 

初始化注意事项:

        1.如果出现如下信息:unknown variable 'defaults-file=/etc/my.cnf',请保证--defaults-file配置选项在最前面。

        2.初始化最后一行是root用户密码(root@localhost: ,5j1Qj_LLAek)如图:

 

 # service mysql-server start

#service mysql-server restart

#service mysql-server stop

# mysql -u root -p

 

 

 

 

 

 

posted @ 2019-09-02 14:02  igoodful  阅读(867)  评论(0编辑  收藏  举报